Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* @var \App\View\AppView $this
* @var array<\App\Model\SomeEntity> $sickDays
*/
foreach ($sickDays as $vacation) {

}

?>
-----
<?php

/**
* @var array<\App\Model\SomeEntity> $sickDays
*/
foreach ($sickDays as $vacation) {

}

?>
58 changes: 34 additions & 24 deletions rules/DeadCode/Rector/Node/RemoveNonExistingVarAnnotationRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public function refactor(Node $node): ?Node
$extractValues = [];

foreach ($node->stmts as $key => $stmt) {
$hasChangedStmt = false;
if ($stmt instanceof Expression && $stmt->expr instanceof FuncCall && $this->isName(
$stmt->expr,
'extract'
Expand Down Expand Up @@ -146,38 +147,47 @@ public function refactor(Node $node): ?Node

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($stmt);

$varTagValueNode = $phpDocInfo->getVarTagValueNode();
if (! $varTagValueNode instanceof VarTagValueNode) {
continue;
}
$varTagValueNodes = $phpDocInfo->getPhpDocNode()
->getVarTagValues();

if ($this->isObjectShapePseudoType($varTagValueNode)) {
continue;
}
foreach ($varTagValueNodes as $varTagValueNode) {
if ($this->isObjectShapePseudoType($varTagValueNode)) {
continue;
}

$variableName = ltrim($varTagValueNode->variableName, '$');
$variableName = ltrim($varTagValueNode->variableName, '$');

if ($variableName === '' && $this->isAllowedEmptyVariableName($stmt)) {
continue;
}
if ($variableName === '' && $this->isAllowedEmptyVariableName($stmt)) {
continue;
}

if ($this->hasVariableName($stmt, $variableName)) {
continue;
}
if ($this->hasVariableName($stmt, $variableName)) {
continue;
}

$comments = $node->getComments();
if (isset($comments[1])) {
// skip edge case with double comment, as impossible to resolve by PHPStan doc parser
continue;
}
$comments = $node->getComments();
if (isset($comments[1])) {
// skip edge case with double comment, as impossible to resolve by PHPStan doc parser
continue;
}

if ($this->stmtsManipulator->isVariableUsedInNextStmt($node, $key + 1, $variableName)) {
continue;
if ($this->stmtsManipulator->isVariableUsedInNextStmt($node, $key + 1, $variableName)) {
continue;
}

if ($variableName === '') {
$phpDocInfo->removeByType(VarTagValueNode::class);
} else {
$phpDocInfo->removeByType(VarTagValueNode::class, $variableName);
}

$hasChangedStmt = true;
}

$phpDocInfo->removeByType(VarTagValueNode::class);
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($stmt);
$hasChanged = true;
if ($hasChangedStmt) {
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($stmt);
$hasChanged = true;
}
}

if ($hasChanged) {
Expand Down