Skip to content

Commit

Permalink
[Core][Symfony] Handle crash on get dynamic value ClassConstFetch by …
Browse files Browse the repository at this point in the history
…method call on ChangeStringCollectionOptionToConstantRector (#2984)

* [Core][Symfony] Handle crash on get dynamic value ClassConstFetch by method call on ChangeStringCollectionOptionToConstantRector

* Fixed 🎉

* Fixed 🎉

* Fixed 🎉
  • Loading branch information
samsonasik committed Oct 13, 2022
1 parent 888c63c commit 84a720b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/PhpParser/Node/Value/ValueResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\MagicConst\Dir;
use PhpParser\Node\Scalar\MagicConst\File;
Expand Down Expand Up @@ -185,7 +186,7 @@ private function getConstExprEvaluator(): ConstExprEvaluator
}

// resolve "SomeClass::SOME_CONST"
if ($expr instanceof ClassConstFetch) {
if ($expr instanceof ClassConstFetch && $expr->class instanceof Name) {
return $this->resolveClassConstFetch($expr);
}

Expand Down
35 changes: 35 additions & 0 deletions tests/Issues/Issue7536/DynamicExprConstantTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\Issue7536;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

/**
* @see https://github.com/rectorphp/rector/issues/7536
*/
final class DynamicExprConstantTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

/**
* @return Iterator<array<string>>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

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

namespace Rector\Core\Tests\Issues\Issue7536\Fixture;

use Symfony\Component\Form\FormInterface;

class SkipDynamicClassConstFetch
{
private function pocMethod(FormInterface $childType, FormInterface $parentType)
{
$parentType->add(
$childType->methodA(),
$childType->methodB()::class
);
}
}
10 changes: 10 additions & 0 deletions tests/Issues/Issue7536/config/configured_rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Symfony\Rector\MethodCall\ChangeStringCollectionOptionToConstantRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(ChangeStringCollectionOptionToConstantRector::class);
};

0 comments on commit 84a720b

Please sign in to comment.