Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
59 changes: 59 additions & 0 deletions rules/Php71/Rector/Assign/AssignArrayToStringRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,61 @@ private function isReAssignedAsArray(Assign $assign, string $variableName, Varia
return false;
}

/**
* The variable is filled as a string later on, so it has to stay a string here as well
*/
private function isReAssignedAsString(
Variable $variable,
Namespace_|FileNode|ClassMethod|Function_|Closure $node
): bool {
if ($node->stmts === null) {
return false;
}

$variableName = $this->getName($variable);
if ($variableName === null) {
return false;
}

$isReAssignedAsString = false;

$this->traverseNodesWithCallable($node->stmts, function (Node $node) use (
$variable,
$variableName,
&$isReAssignedAsString
): ?int {
if (! $node instanceof Assign) {
return null;
}

if (! $node->var instanceof Variable) {
return null;
}

if (! $this->isName($node->var, $variableName)) {
return null;
}

if ($node->var->getStartTokenPos() <= $variable->getStartTokenPos()) {
return null;
}

// an empty string assign is a candidate for the very same re-type, so it is no proof of a string
if ($this->isEmptyString($node->expr)) {
return null;
}

if (! $this->nodeTypeResolver->getNativeType($node->expr)->isString()->yes()) {
return null;
}

$isReAssignedAsString = true;
return NodeVisitor::STOP_TRAVERSAL;
});

return $isReAssignedAsString;
}

private function refactorAssign(
Assign $assign,
Namespace_|FileNode|ClassMethod|Function_|Closure $node
Expand All @@ -258,6 +313,10 @@ private function refactorAssign(
return null;
}

if ($this->isReAssignedAsString($assign->var, $node)) {
return null;
}

$variableAssignArrayDimFetches = $this->findSameNamedVariableAssigns($assign->var, $node);

$shouldRetype = false;
Expand Down
20 changes: 6 additions & 14 deletions tests/Set/SetManager/SetManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function testMatchComposerTriggered(): void
$setManager = $this->createSetManagerWithProjectDirectory(getcwd());

$twigComposerTriggeredSet = $setManager->matchComposerTriggered(SetGroup::TWIG);
$this->assertCount(8, $twigComposerTriggeredSet);
$this->assertCount(1, $twigComposerTriggeredSet);
}

/**
Expand All @@ -42,21 +42,13 @@ public function testByVersion(string $projectDirectory, array $expectedSets): vo
*/
public static function provideInstalledTwigData(): Iterator
{
// here we cannot used features coming up in 2.4, as we only have 2.0
yield [
__DIR__ . '/Fixture/project-twig-20',
[realpath(TwigSetList::COMPOSER_BASED), realpath(TwigSetList::TWIG_20)],
];
// the composer-based set covers every Twig version, as each rule inside it is bound
// to the exact twig/twig version it needs
yield [__DIR__ . '/Fixture/project-twig-20', [realpath(TwigSetList::COMPOSER_BASED)]];

yield [
__DIR__ . '/Fixture/project-twig-24',
[realpath(TwigSetList::COMPOSER_BASED), realpath(TwigSetList::TWIG_20), realpath(TwigSetList::TWIG_24)],
];
yield [__DIR__ . '/Fixture/project-twig-24', [realpath(TwigSetList::COMPOSER_BASED)]];

yield [
__DIR__ . '/Fixture/project-twig-127',
[realpath(TwigSetList::COMPOSER_BASED), realpath(TwigSetList::TWIG_112), realpath(TwigSetList::TWIG_127)],
];
yield [__DIR__ . '/Fixture/project-twig-127', [realpath(TwigSetList::COMPOSER_BASED)]];
}

private function createSetManagerWithProjectDirectory(string $projectDirectory): SetManager
Expand Down
Loading