Skip to content

Commit

Permalink
Add YAML usage class constant support
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Sep 7, 2024
1 parent 9c62e4f commit d8ab207
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 2 deletions.
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);
}
}

0 comments on commit d8ab207

Please sign in to comment.