Skip to content

Commit

Permalink
[Privatization] Skip used by heredoc on ChangeReadOnlyVariableWithDef…
Browse files Browse the repository at this point in the history
…aultValueToConstantRector (#3216)

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Dec 18, 2022
1 parent e35497e commit 135483f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

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

final class SkipUsedByHeredoc
{
public function run()
{
$foo = 'I like pie';

$bar = <<<EOD
And on this day, it was spoken - "$foo"
EOD;
}
}
11 changes: 6 additions & 5 deletions rules/Renaming/Helper/RenameClassCallbackHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\Renaming\Helper;

use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\NodeVisitorAbstract;
use PHPStan\Reflection\ReflectionProvider;
Expand Down Expand Up @@ -53,18 +54,18 @@ public function getOldToNewClassesFromNode(Node $node): array
/**
* @return array<string, string>
*/
public function handleClassLike(ClassLike $node): array
public function handleClassLike(ClassLike $classLike): array
{
$oldToNewClasses = [];
$className = $node->name;
if ($className === null) {
$className = $classLike->name;
if (! $className instanceof Identifier) {
return [];
}

foreach ($this->oldToNewClassCallbacks as $oldToNewClassCallback) {
$newClassName = $oldToNewClassCallback($node, $this->nodeNameResolver, $this->reflectionProvider);
$newClassName = $oldToNewClassCallback($classLike, $this->nodeNameResolver, $this->reflectionProvider);
if ($newClassName !== null) {
$fullyQualifiedClassName = (string) $this->nodeNameResolver->getName($node);
$fullyQualifiedClassName = (string) $this->nodeNameResolver->getName($classLike);
$this->renamedClassesDataCollector->addOldToNewClass($fullyQualifiedClassName, $newClassName);
$oldToNewClasses[$fullyQualifiedClassName] = $newClassName;
}
Expand Down
29 changes: 29 additions & 0 deletions src/NodeManipulator/ClassMethodAssignManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\ClosureUse;
use PhpParser\Node\Expr\FuncCall;
Expand All @@ -14,6 +15,7 @@
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Param;
use PhpParser\Node\Scalar\Encapsed;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Foreach_;
Expand Down Expand Up @@ -66,6 +68,7 @@ public function collectReadyOnlyAssignScalarVariables(ClassMethod $classMethod,
$readOnlyVariableAssigns = $this->filterOutReferencedVariables($readOnlyVariableAssigns, $classMethod);
$readOnlyVariableAssigns = $this->filterOutMultiAssigns($readOnlyVariableAssigns);
$readOnlyVariableAssigns = $this->filterOutForeachVariables($readOnlyVariableAssigns);
$readOnlyVariableAssigns = $this->filterOutUsedByEncapsed($readOnlyVariableAssigns);

/**
* Remove unused variable assign is task of RemoveUnusedVariableAssignRector
Expand Down Expand Up @@ -93,6 +96,32 @@ public function addParameterAndAssignToMethod(
$this->alreadyAddedClassMethodNames[$classMethodHash][] = $name;
}

/**
* @param Assign[] $readOnlyVariableAssigns
* @return Assign[]
*/
private function filterOutUsedByEncapsed(array $readOnlyVariableAssigns): array
{
$callable = function (Assign $readOnlyVariableAssign): bool {
$variable = $readOnlyVariableAssign->var;
return ! (bool) $this->betterNodeFinder->findFirstNext(
$readOnlyVariableAssign,
function (Node $node) use ($variable): bool {
if (! $node instanceof Encapsed) {
return false;
}

return (bool) array_filter(
$node->parts,
fn (Expr $expr): bool => $this->nodeComparator->areNodesEqual($expr, $variable)
);
}
);
};

return array_filter($readOnlyVariableAssigns, $callable);
}

/**
* @param Assign[] $readOnlyVariableAssigns
* @return Assign[]
Expand Down

0 comments on commit 135483f

Please sign in to comment.