Skip to content

Commit 84a720b

Browse files
authored
[Core][Symfony] Handle crash on get dynamic value ClassConstFetch by method call on ChangeStringCollectionOptionToConstantRector (#2984)
* [Core][Symfony] Handle crash on get dynamic value ClassConstFetch by method call on ChangeStringCollectionOptionToConstantRector * Fixed 🎉 * Fixed 🎉 * Fixed 🎉
1 parent 888c63c commit 84a720b

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

src/PhpParser/Node/Value/ValueResolver.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PhpParser\Node\Expr\BinaryOp\Concat;
1111
use PhpParser\Node\Expr\ClassConstFetch;
1212
use PhpParser\Node\Expr\ConstFetch;
13+
use PhpParser\Node\Name;
1314
use PhpParser\Node\Name\FullyQualified;
1415
use PhpParser\Node\Scalar\MagicConst\Dir;
1516
use PhpParser\Node\Scalar\MagicConst\File;
@@ -185,7 +186,7 @@ private function getConstExprEvaluator(): ConstExprEvaluator
185186
}
186187

187188
// resolve "SomeClass::SOME_CONST"
188-
if ($expr instanceof ClassConstFetch) {
189+
if ($expr instanceof ClassConstFetch && $expr->class instanceof Name) {
189190
return $this->resolveClassConstFetch($expr);
190191
}
191192

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Core\Tests\Issues\Issue7536;
6+
7+
use Iterator;
8+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
9+
10+
/**
11+
* @see https://github.com/rectorphp/rector/issues/7536
12+
*/
13+
final class DynamicExprConstantTest extends AbstractRectorTestCase
14+
{
15+
/**
16+
* @dataProvider provideData()
17+
*/
18+
public function test(string $filePath): void
19+
{
20+
$this->doTestFile($filePath);
21+
}
22+
23+
/**
24+
* @return Iterator<array<string>>
25+
*/
26+
public function provideData(): Iterator
27+
{
28+
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
29+
}
30+
31+
public function provideConfigFilePath(): string
32+
{
33+
return __DIR__ . '/config/configured_rule.php';
34+
}
35+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Rector\Core\Tests\Issues\Issue7536\Fixture;
4+
5+
use Symfony\Component\Form\FormInterface;
6+
7+
class SkipDynamicClassConstFetch
8+
{
9+
private function pocMethod(FormInterface $childType, FormInterface $parentType)
10+
{
11+
$parentType->add(
12+
$childType->methodA(),
13+
$childType->methodB()::class
14+
);
15+
}
16+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\Symfony\Rector\MethodCall\ChangeStringCollectionOptionToConstantRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(ChangeStringCollectionOptionToConstantRector::class);
10+
};

0 commit comments

Comments
 (0)