Skip to content

Commit

Permalink
Add failing test case for STOP_TRAVERSAL directly on refactor (#4185)
Browse files Browse the repository at this point in the history
* Add failing test case for STOP_TRAVERSAL directly on refactor

* [ci-review] Rector Rectify

* remove node visitor

* use traverseNodesWithCallable

* [ci-review] Rector Rectify

* cs fix

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Jun 12, 2023
1 parent 69689c7 commit 9141715
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 35 deletions.
26 changes: 0 additions & 26 deletions rules/Php55/NodeVisitor/ClassConstStringValueNodeVisitor.php

This file was deleted.

Expand Up @@ -17,7 +17,6 @@
use Rector\Core\Contract\Rector\AllowEmptyConfigurableRectorInterface;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\Php55\NodeVisitor\ClassConstStringValueNodeVisitor;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -97,7 +96,14 @@ public function refactorWithScope(Node $node, Scope $scope): Concat|ClassConstFe
{
// allow class strings to be part of class const arrays, as probably on purpose
if ($node instanceof ClassConst) {
$this->fillIsUnderClassConstAttribute($node);
$this->traverseNodesWithCallable($node->consts, static function (Node $subNode) {
if ($subNode instanceof String_) {
$subNode->setAttribute(self::IS_UNDER_CLASS_CONST, true);
}

return null;
});

return null;
}

Expand Down Expand Up @@ -155,13 +161,6 @@ public function provideMinPhpVersion(): int
return PhpVersionFeature::CLASSNAME_CONSTANT;
}

private function fillIsUnderClassConstAttribute(ClassConst $classConst): void
{
$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor(new ClassConstStringValueNodeVisitor());
$nodeTraverser->traverse([$classConst]);
}

private function shouldSkip(string $classLikeName): bool
{
// skip short class names, mostly invalid use of strings
Expand Down
@@ -0,0 +1,36 @@
<?php

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

final class InArrayConstantWithUndefinedVariable
{
const SKIP_TYPES = [
'Rector\Tests\Php55\Rector\String_\StringClassNameToClassConstantRector\Source\SomeUser'
];

public function run()
{
echo $undefinedVariableHere;
}
}

?>
-----
<?php

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

final class InArrayConstantWithUndefinedVariable
{
const SKIP_TYPES = [
'Rector\Tests\Php55\Rector\String_\StringClassNameToClassConstantRector\Source\SomeUser'
];

public function run()
{
$undefinedVariableHere = null;
echo $undefinedVariableHere;
}
}

?>
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\StringClassNameConstantDefaultValue;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class StringClassNameConstantDefaultValueTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

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

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
use Rector\Php56\Rector\FunctionLike\AddDefaultValueForUndefinedVariableRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([
StringClassNameToClassConstantRector::class,
AddDefaultValueForUndefinedVariableRector::class,
]);
};

0 comments on commit 9141715

Please sign in to comment.