From 8e0a5739fc36110f3aed74700cb098ac09fc643d Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 31 May 2024 14:32:13 +0900 Subject: [PATCH] allow multi static calls in one class --- src/Command/PrivatizeConstantsCommand.php | 9 +++-- src/Resolver/StaticClassConstResolver.php | 24 +++++++------ .../Fixture/FileWithStaticConstCalls.php | 15 ++++++++ .../StaticClassConstResolverTest.php | 36 +++++++++++++++++++ 4 files changed, 69 insertions(+), 15 deletions(-) create mode 100644 tests/Resolver/StaticClassConstResolver/Fixture/FileWithStaticConstCalls.php create mode 100644 tests/Resolver/StaticClassConstResolver/StaticClassConstResolverTest.php diff --git a/src/Command/PrivatizeConstantsCommand.php b/src/Command/PrivatizeConstantsCommand.php index 4edae3a99..32390f632 100644 --- a/src/Command/PrivatizeConstantsCommand.php +++ b/src/Command/PrivatizeConstantsCommand.php @@ -6,7 +6,6 @@ use Nette\Utils\FileSystem; use Nette\Utils\Strings; -use Rector\SwissKnife\FileSystem\PathHelper; use Rector\SwissKnife\Finder\PhpFilesFinder; use Rector\SwissKnife\Helpers\ClassNameResolver; use Rector\SwissKnife\PHPStan\ClassConstantResultAnalyser; @@ -75,7 +74,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $this->privatizeClassConstants($phpFileInfos); // special case of self::NAME, that should be protected - their children too - $staticClassConstsMatches = $this->staticClassConstResolver->resolve($phpFileInfos); + $staticClassConstMatches = $this->staticClassConstResolver->resolve($phpFileInfos); $phpstanResult = $this->runPHPStanAnalyse($sources); @@ -94,7 +93,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $this->replacePrivateConstWith($publicClassConstMatch, 'protected const'); } - $this->replaceClassAndChildWithProtected($phpFileInfos, $staticClassConstsMatches); + $this->replaceClassAndChildWithProtected($phpFileInfos, $staticClassConstMatches); if ($publicAndProtectedClassConstants->getPublicCount() !== 0) { $this->symfonyStyle->success( @@ -108,9 +107,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int ); } - if ($staticClassConstsMatches !== []) { + if ($staticClassConstMatches !== []) { $this->symfonyStyle->success( - \sprintf('%d constants made protected for static access', count($staticClassConstsMatches)) + \sprintf('%d constants made protected for static access', count($staticClassConstMatches)) ); } diff --git a/src/Resolver/StaticClassConstResolver.php b/src/Resolver/StaticClassConstResolver.php index 127bb6deb..e0e89aa63 100644 --- a/src/Resolver/StaticClassConstResolver.php +++ b/src/Resolver/StaticClassConstResolver.php @@ -11,6 +11,8 @@ /** * Resolve all "static::SOME_CONST" calls + * + * @see \Rector\SwissKnife\Tests\Resolver\StaticClassConstResolver\StaticClassConstResolverTest */ final class StaticClassConstResolver { @@ -28,17 +30,19 @@ public function resolve(array $phpFileInfos): array { $staticConstMatches = []; foreach ($phpFileInfos as $phpFileInfo) { - $match = Strings::match($phpFileInfo->getContents(), self::STATIC_CONST_CALL_REGEX); - if ($match === null) { - continue; + $matches = Strings::matchAll($phpFileInfo->getContents(), self::STATIC_CONST_CALL_REGEX); + foreach ($matches as $match) { + if ($match === null) { + continue; + } + + $fullyQualifiedClassName = ClassNameResolver::resolveFromFileContents($phpFileInfo->getContents()); + if ($fullyQualifiedClassName === null) { + continue; + } + + $staticConstMatches[] = new ClassConstMatch($fullyQualifiedClassName, $match['constant_name']); } - - $fullyQualifiedClassName = ClassNameResolver::resolveFromFileContents($phpFileInfo->getContents()); - if ($fullyQualifiedClassName === null) { - continue; - } - - $staticConstMatches[] = new ClassConstMatch($fullyQualifiedClassName, $match['constant_name']); } return $staticConstMatches; diff --git a/tests/Resolver/StaticClassConstResolver/Fixture/FileWithStaticConstCalls.php b/tests/Resolver/StaticClassConstResolver/Fixture/FileWithStaticConstCalls.php new file mode 100644 index 000000000..75d7b4eff --- /dev/null +++ b/tests/Resolver/StaticClassConstResolver/Fixture/FileWithStaticConstCalls.php @@ -0,0 +1,15 @@ +staticClassConstResolver = new StaticClassConstResolver(); + } + + public function test(): void + { + $splFileInfo = new SplFileInfo(__DIR__ . '/Fixture/FileWithStaticConstCalls.php', '', ''); + + $classConstMatches = $this->staticClassConstResolver->resolve([$splFileInfo]); + $this->assertCount(2, $classConstMatches); + + $firstClassConstMatch = $classConstMatches[0]; + $this->assertSame(FileWithStaticConstCalls::class, $firstClassConstMatch->getClassName()); + $this->assertSame('ITEM_NAME', $firstClassConstMatch->getConstantName()); + + $secondClassConstMatch = $classConstMatches[1]; + $this->assertSame(FileWithStaticConstCalls::class, $secondClassConstMatch->getClassName()); + $this->assertSame('ITEM_PRICE', $secondClassConstMatch->getConstantName()); + } +}