Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Privatization] Skip with encapsed string part on ChangeReadOnlyVariableWithDefaultValueToConstantRector #1287

Merged
merged 7 commits into from
Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\Privatization\Rector\Class_\ChangeReadOnlyVariableWithDefaultValueToConstantRector\Fixture;

final class SkipWithEncapsedStringPart
{
public function run(int $id): array
{
$data = [
'url' => "/$id.pdf",
];
TomasVotruba marked this conversation as resolved.
Show resolved Hide resolved

return $data;
}
}
13 changes: 13 additions & 0 deletions src/NodeManipulator/VariableManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar;
use PhpParser\Node\Scalar\Encapsed;
use PhpParser\Node\Scalar\EncapsedStringPart;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Core\PhpParser\Comparing\NodeComparator;
Expand Down Expand Up @@ -67,6 +68,10 @@ function (Node $node) use (&$assignsOfArrayToVariable) {
return null;
}

if ($node->expr instanceof Array_ && $this->hasEncapsedStringPart($node->expr)) {
return null;
}

$assignsOfArrayToVariable[] = $node;
}
);
Expand All @@ -86,6 +91,14 @@ public function filterOutChangedVariables(array $assignsOfArrayToVariable, Class
);
}

private function hasEncapsedStringPart(Array_ $array): bool
{
return (bool) $this->betterNodeFinder->findFirst(
$array,
fn (Node $subNode): bool => $subNode instanceof EncapsedStringPart
);
}

private function isTestCaseExpectedVariable(Variable $variable): bool
{
$classLike = $this->betterNodeFinder->findParentType($variable, ClassLike::class);
Expand Down