Skip to content

Commit

Permalink
add test for ClassConstantResultAnalyser, fix regex message
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed May 31, 2024
1 parent 8e0a573 commit cd2fea5
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/Command/PrivatizeConstantsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

// make public first, to avoid override to protected
foreach ($publicAndProtectedClassConstants->getPublicClassConstMatch() as $publicClassConstMatch) {
foreach ($publicAndProtectedClassConstants->getPublicClassConstMatches() as $publicClassConstMatch) {
$this->replacePrivateConstWith($publicClassConstMatch, 'public const');
}

foreach ($publicAndProtectedClassConstants->getProtectedClassConstMatch() as $publicClassConstMatch) {
foreach ($publicAndProtectedClassConstants->getProtectedClassConstMatches() as $publicClassConstMatch) {
$this->replacePrivateConstWith($publicClassConstMatch, 'protected const');
}

Expand Down
14 changes: 12 additions & 2 deletions src/PHPStan/ClassConstantResultAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
use Nette\Utils\Strings;
use Rector\SwissKnife\ValueObject\ClassConstMatch;
use Rector\SwissKnife\ValueObject\PublicAndProtectedClassConstants;
use Webmozart\Assert\Assert;

/**
* @see \Rector\SwissKnife\Tests\PHPStan\ClassConstantResultAnalyserTest
*/
final class ClassConstantResultAnalyser
{
/**
Expand All @@ -18,9 +22,9 @@ final class ClassConstantResultAnalyser
/**
* @var string
* @see https://regex101.com/r/pRzdnw/1
* @see https://regex101.com/r/V1QOPN/1
*/
private const PROTECTED_CONSTANT_MESSAGE_REGEX = '#Access to undefined constant (?<class_name>[\w\\\\]+)::(?<constant_name>.*?)#';
private const PROTECTED_CONSTANT_MESSAGE_REGEX = '#Access to undefined constant (?<class_name>[\w\\\\]+)::(?<constant_name>[\w\_]+)#';

/**
* @param mixed[] $phpstanResult
Expand All @@ -30,8 +34,14 @@ public function analyseResult(array $phpstanResult): PublicAndProtectedClassCons
$publicClassConstMatches = [];
$protectedClassConstMatches = [];

Assert::keyExists($phpstanResult, 'files');

foreach ($phpstanResult['files'] as $fileDetail) {
Assert::keyExists($fileDetail, 'messages');

foreach ($fileDetail['messages'] as $messageError) {
Assert::keyExists($messageError, 'message');

$publicClassConstMatch = $this->matchPublicClassConstMatch($messageError['message']);
if ($publicClassConstMatch instanceof ClassConstMatch) {
$publicClassConstMatches[] = $publicClassConstMatch;
Expand Down
3 changes: 3 additions & 0 deletions src/ValueObject/ClassConstMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use ReflectionClass;
use Stringable;
use Webmozart\Assert\Assert;

final readonly class ClassConstMatch implements Stringable
{
Expand All @@ -16,6 +17,8 @@ public function __construct(
private string $className,
private string $constantName
) {
Assert::notEmpty($constantName);
Assert::notEmpty($className);
}

/**
Expand Down
22 changes: 11 additions & 11 deletions src/ValueObject/PublicAndProtectedClassConstants.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,43 @@
final readonly class PublicAndProtectedClassConstants
{
/**
* @param ClassConstMatch[] $publicClassConstMatch
* @param ClassConstMatch[] $protectedClassConstMatch
* @param ClassConstMatch[] $publicClassConstMatches
* @param ClassConstMatch[] $protectedClassConstMatches
*/
public function __construct(
private array $publicClassConstMatch,
private array $protectedClassConstMatch
private array $publicClassConstMatches,
private array $protectedClassConstMatches
) {
}

/**
* @return ClassConstMatch[]
*/
public function getPublicClassConstMatch(): array
public function getPublicClassConstMatches(): array
{
return $this->publicClassConstMatch;
return $this->publicClassConstMatches;
}

/**
* @return ClassConstMatch[]
*/
public function getProtectedClassConstMatch(): array
public function getProtectedClassConstMatches(): array
{
return $this->protectedClassConstMatch;
return $this->protectedClassConstMatches;
}

public function getProtectedCount(): int
{
return count($this->protectedClassConstMatch);
return count($this->protectedClassConstMatches);
}

public function getPublicCount(): int
{
return count($this->publicClassConstMatch);
return count($this->publicClassConstMatches);
}

public function isEmpty(): bool
{
return $this->publicClassConstMatch === [] && $this->protectedClassConstMatch === [];
return $this->publicClassConstMatches === [] && $this->protectedClassConstMatches === [];
}
}
47 changes: 47 additions & 0 deletions tests/PHPStan/ClassConstantResultAnalyserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Rector\SwissKnife\Tests\PHPStan;

use PHPUnit\Framework\TestCase;
use Rector\SwissKnife\PHPStan\ClassConstantResultAnalyser;
use Rector\SwissKnife\ValueObject\ClassConstMatch;

final class ClassConstantResultAnalyserTest extends TestCase
{
private ClassConstantResultAnalyser $classConstantResultAnalyser;

protected function setUp(): void
{
$this->classConstantResultAnalyser = new ClassConstantResultAnalyser();
}

public function test(): void
{
$phpstanResult = [
'files' => [
'some_file_path.php' => [
'messages' => [
[
'line' => 10,
'message' => 'Access to undefined constant App\\SomeClass::SOME_CONSTANT',
],
],
],
],
];

$publicAndProtectedClassConstants = $this->classConstantResultAnalyser->analyseResult($phpstanResult);

$this->assertFalse($publicAndProtectedClassConstants->isEmpty());
$this->assertSame(0, $publicAndProtectedClassConstants->getPublicCount());
$this->assertSame(1, $publicAndProtectedClassConstants->getProtectedCount());

$onlyProtectedClassConstMatch = $publicAndProtectedClassConstants->getProtectedClassConstMatches()[0];
$this->assertInstanceOf(ClassConstMatch::class, $onlyProtectedClassConstMatch);

// $this->assertSame('SOME_CONST', $onlyProtectedClassConstMatch->getConstantName());
$this->assertSame('App\SomeClass', $onlyProtectedClassConstMatch->getClassName());
}
}

0 comments on commit cd2fea5

Please sign in to comment.