Skip to content

Commit

Permalink
[StaticReflection] Fix unintended behavior in PHP 8.1 and later (#3340)
Browse files Browse the repository at this point in the history
  • Loading branch information
zeriyoshi committed Feb 6, 2023
1 parent 4c76526 commit 256571c
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
7 changes: 7 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,13 @@ parameters:
count: 2
path: src/Rector/AbstractRector.php

-
message: """
#Function "class_exists\\(\\)" cannot be used/left in the code\\: use ReflectionProvider\\->has\\*\\(\\) instead#
"""
count: 1
path: src/StaticReflection/SourceLocator/RenamedClassesSourceLocator.php

-
message: """
#^Fetching class constant class of deprecated class Rector\\\\TypeDeclaration\\\\Rector\\\\FunctionLike\\\\ReturnTypeDeclarationRector\\:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Renaming\Rector\Name\RenameClassRector\FixtureRenameMissingParent;

use Missing\ParentClass;

class MissingParent extends ParentClass
{
public function foo(): void
{
}
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Tests\Renaming\Rector\Name\RenameClassRector\FixtureRenameMissingParent;

use Missing\ParentClass;

class MissingParent extends \Renamed\Missing\ParentWithMethod
{
public function foo(): void
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Renaming\Rector\Name\RenameClassRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Renaming\Rector\Name\RenameClassRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

/**
* @see RenameClassRector
*/
final class RenameMissingParentTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/FixtureRenameMissingParent');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/rename_missing_parent.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Renaming\Rector\Name\RenameClassRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig
->ruleWithConfiguration(RenameClassRector::class, [
'Missing\\ParentClass' => 'Renamed\\Missing\\ParentWithMethod',
]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier):
continue;
}

/* Use ReflectionProvider causes infinite loop */
if (!class_exists($oldClass)) {
continue;
}

return $this->createFakeReflectionClassFromClassName($oldClass);
}

Expand Down

0 comments on commit 256571c

Please sign in to comment.