Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add YAML usage class constant support #53

Merged
merged 1 commit into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/Command/PrivatizeConstantsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Rector\SwissKnife\ValueObject\ClassConstant;
use Rector\SwissKnife\ValueObject\ClassConstantFetch\CurrentClassConstantFetch;
use Rector\SwissKnife\ValueObject\VisibilityChangeStats;
use Rector\SwissKnife\YAML\YamlConfigConstantExtractor;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -29,6 +30,7 @@ public function __construct(
private readonly ClassConstantFetchFinder $classConstantFetchFinder,
private readonly ClassConstFinder $classConstFinder,
private readonly TwigTemplateConstantExtractor $twigTemplateConstantExtractor,
private readonly YamlConfigConstantExtractor $yamlConfigConstantExtractor
) {
parent::__construct();
}
Expand All @@ -52,7 +54,7 @@ protected function configure(): void

$this->addOption('debug', null, InputOption::VALUE_NONE, 'Debug output');

$this->setDescription('Make class constants private if not used outside');
$this->setDescription('Make class constants private if not used outside in PHP, Twig and YAML files');
}

/**
Expand All @@ -78,7 +80,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int

// find usage in twig files
$twigClassConstantFetches = $this->twigTemplateConstantExtractor->extractFromDirs($sources);
$classConstantFetches = array_merge($phpClassConstantFetches, $twigClassConstantFetches);
$yamlClassConstantFetches = $this->yamlConfigConstantExtractor->extractFromDirs($sources);

$classConstantFetches = array_merge(
$phpClassConstantFetches,
$twigClassConstantFetches,
$yamlClassConstantFetches
);

$this->symfonyStyle->newLine(2);
$this->symfonyStyle->success(sprintf('Found %d class constant fetches', count($classConstantFetches)));
Expand Down
56 changes: 56 additions & 0 deletions src/YAML/YamlConfigConstantExtractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace Rector\SwissKnife\YAML;

use Nette\Utils\FileSystem;
use Nette\Utils\Strings;
use Rector\SwissKnife\Contract\ClassConstantFetchInterface;
use Rector\SwissKnife\Finder\FilesFinder;
use Rector\SwissKnife\ValueObject\ClassConstantFetch\ExternalClassAccessConstantFetch;

/**
* @see \Rector\SwissKnife\Tests\YAML\YamlConfigConstantExtractor\YamlConfigConstantExtractorTest
*/
final class YamlConfigConstantExtractor
{
/**
* @param string[] $directories
* @return ClassConstantFetchInterface[]
*/
public function extractFromDirs(array $directories): array
{
$yamlFileInfos = FilesFinder::findYamlFiles($directories);

$classConstantFetches = [];
foreach ($yamlFileInfos as $yamlFileInfo) {
$currentClassConstantFetches = $this->findClassConstantFetchesInFile($yamlFileInfo->getRealPath());

$classConstantFetches = array_merge($classConstantFetches, $currentClassConstantFetches);
}

return $classConstantFetches;
}

/**
* @return ClassConstantFetchInterface[]
*/
private function findClassConstantFetchesInFile(string $filePath): array
{
$fileContents = FileSystem::read($filePath);

// find constant fetches in YAML file
$constantMatches = Strings::matchAll($fileContents, '#\b(?<class>[\w\\\]+)::(?<constant>\w+)\b#');

$externalClassAccessConstantFetches = [];
foreach ($constantMatches as $constantMatch) {
$externalClassAccessConstantFetches[] = new ExternalClassAccessConstantFetch(
$constantMatch['class'],
$constantMatch['constant']
);
}

return $externalClassAccessConstantFetches;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Rector\SwissKnife\Tests\YAML\YamlConfigConstantExtractor\Fixture;

final class SomeClassWithConstant
{
public const USE_ME_IN_YAML = 'value';
}
2 changes: 2 additions & 0 deletions tests/YAML/YamlConfigConstantExtractor/Fixture/some_file.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
some_config:
value: <(\Rector\SwissKnife\Tests\YAML\YamlConfigConstantExtractor\Fixture\SomeClassWithConstant::USE_ME_IN_YAML)>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Rector\SwissKnife\Tests\YAML\YamlConfigConstantExtractor;

use Rector\SwissKnife\Tests\AbstractTestCase;
use Rector\SwissKnife\YAML\YamlConfigConstantExtractor;

final class YamlConfigConstantExtractorTest extends AbstractTestCase
{
private YamlConfigConstantExtractor $yamlConfigConstantExtractor;

protected function setUp(): void
{
parent::setUp();
$this->yamlConfigConstantExtractor = $this->make(YamlConfigConstantExtractor::class);
}

public function test(): void
{
$classConstantFetches = $this->yamlConfigConstantExtractor->extractFromDirs([__DIR__ . '/Fixture']);
$this->assertCount(1, $classConstantFetches);
}
}
Loading