Skip to content

Commit

Permalink
allow multi static calls in one class
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed May 31, 2024
1 parent c3295b7 commit 8e0a573
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 15 deletions.
9 changes: 4 additions & 5 deletions src/Command/PrivatizeConstantsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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(
Expand All @@ -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))
);
}

Expand Down
24 changes: 14 additions & 10 deletions src/Resolver/StaticClassConstResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

/**
* Resolve all "static::SOME_CONST" calls
*
* @see \Rector\SwissKnife\Tests\Resolver\StaticClassConstResolver\StaticClassConstResolverTest
*/
final class StaticClassConstResolver
{
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\SwissKnife\Tests\Resolver\StaticClassConstResolver\Fixture;

final class FileWithStaticConstCalls
{
public const ITEM_NAME = '...';

public const ITEM_PRICE = 100;

public function run()
{
$hash = static::ITEM_NAME . '_' . static::ITEM_PRICE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Rector\SwissKnife\Tests\Resolver\StaticClassConstResolver;

use PHPUnit\Framework\TestCase;
use Rector\SwissKnife\Resolver\StaticClassConstResolver;
use Rector\SwissKnife\Tests\Resolver\StaticClassConstResolver\Fixture\FileWithStaticConstCalls;
use Symfony\Component\Finder\SplFileInfo;

final class StaticClassConstResolverTest extends TestCase
{
private StaticClassConstResolver $staticClassConstResolver;

protected function setUp(): void
{
$this->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());
}
}

0 comments on commit 8e0a573

Please sign in to comment.