Skip to content

Commit 0927484

Browse files
committed
Updated Rector to commit eac1efd2ac2c2fce0ee6ed758d52f57acbf08a27
rectorphp/rector-src@eac1efd [DeadCode] Allow remove @var on expression assign on RemoveUselessVarTagRector (#7871)
1 parent aff94f2 commit 0927484

File tree

6 files changed

+37
-13
lines changed

6 files changed

+37
-13
lines changed

rules/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ public function refactor(Node $node): ?Node
9393
$catch = $stmt->catches[0];
9494
/** @var Variable $catchVar */
9595
$catchVar = $catch->var;
96-
/** @var string $oldVariableName */
9796
$oldVariableName = (string) $this->getName($catchVar);
9897
$typeShortName = $this->resolveVariableName($catch->types[0]);
9998
$newVariableName = $this->resolveNewVariableName($typeShortName);

rules/DeadCode/PhpDoc/DeadVarTagValueNodeAnalyzer.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
namespace Rector\DeadCode\PhpDoc;
55

66
use PhpParser\Node;
7+
use PhpParser\Node\Expr;
8+
use PhpParser\Node\Expr\Assign;
79
use PhpParser\Node\Stmt\ClassConst;
10+
use PhpParser\Node\Stmt\Expression;
811
use PhpParser\Node\Stmt\Property;
912
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
1013
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
@@ -14,6 +17,7 @@
1417
use PHPStan\Type\UnionType;
1518
use Rector\DeadCode\PhpDoc\Guard\TemplateTypeRemovalGuard;
1619
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
20+
use Rector\PHPStan\ScopeFetcher;
1721
use Rector\StaticTypeMapper\StaticTypeMapper;
1822
final class DeadVarTagValueNodeAnalyzer
1923
{
@@ -36,22 +40,43 @@ public function __construct(TypeComparator $typeComparator, StaticTypeMapper $st
3640
$this->templateTypeRemovalGuard = $templateTypeRemovalGuard;
3741
}
3842
/**
39-
* @param \PhpParser\Node\Stmt\Property|\PhpParser\Node\Stmt\ClassConst $property
43+
* @param \PhpParser\Node\Stmt\Property|\PhpParser\Node\Stmt\ClassConst|\PhpParser\Node\Stmt\Expression $node
4044
*/
41-
public function isDead(VarTagValueNode $varTagValueNode, $property): bool
45+
public function isDead(VarTagValueNode $varTagValueNode, $node): bool
4246
{
43-
if (!$property->type instanceof Node) {
47+
if (!$node instanceof Expression && !$node->type instanceof Node) {
4448
return \false;
4549
}
4650
if ($varTagValueNode->description !== '') {
4751
return \false;
4852
}
53+
$targetNode = null;
54+
if ($node instanceof Expression && $node->expr instanceof Assign) {
55+
$targetNode = $node->expr->expr;
56+
} elseif ($node instanceof Property || $node instanceof ClassConst) {
57+
$targetNode = $node->type;
58+
}
59+
// allow Identifier, ComplexType, and Name on Property and ClassConst
60+
if (!$targetNode instanceof Node) {
61+
return \false;
62+
}
4963
if ($varTagValueNode->type instanceof GenericTypeNode) {
5064
return \false;
5165
}
5266
// is strict type superior to doc type? keep strict type only
53-
$propertyType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($property->type);
54-
$docType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType($varTagValueNode->type, $property);
67+
$propertyType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($targetNode);
68+
$docType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType($varTagValueNode->type, $node);
69+
if ($node instanceof Expression) {
70+
$scope = ScopeFetcher::fetch($node);
71+
// only allow Expr on assign expr
72+
if (!$targetNode instanceof Expr) {
73+
return \false;
74+
}
75+
$nativeType = $scope->getNativeType($targetNode);
76+
if (!$docType->equals($nativeType)) {
77+
return \false;
78+
}
79+
}
5580
if (!$this->templateTypeRemovalGuard->isLegal($docType)) {
5681
return \false;
5782
}
@@ -62,7 +87,7 @@ public function isDead(VarTagValueNode $varTagValueNode, $property): bool
6287
// more specific type is already in the property
6388
return $docType->isSuperTypeOf($propertyType)->yes();
6489
}
65-
if ($this->typeComparator->arePhpParserAndPhpStanPhpDocTypesEqual($property->type, $varTagValueNode->type, $property)) {
90+
if ($this->typeComparator->arePhpParserAndPhpStanPhpDocTypesEqual($targetNode, $varTagValueNode->type, $node)) {
6691
return \true;
6792
}
6893
return $docType instanceof UnionType && $this->typeComparator->areTypesEqual(TypeCombinator::removeNull($docType), $propertyType);

rules/DeadCode/PhpDoc/TagRemover/VarTagRemover.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function __construct(DoctrineTypeAnalyzer $doctrineTypeAnalyzer, PhpDocIn
5454
$this->typeComparator = $typeComparator;
5555
}
5656
/**
57-
* @param \PhpParser\Node\Stmt\Property|\PhpParser\Node\Stmt\ClassConst $property
57+
* @param \PhpParser\Node\Stmt\Property|\PhpParser\Node\Stmt\ClassConst|\PhpParser\Node\Stmt\Expression $property
5858
*/
5959
public function removeVarTagIfUseless(PhpDocInfo $phpDocInfo, $property): bool
6060
{

rules/DeadCode/Rector/Property/RemoveUselessVarTagRector.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use PhpParser\Node;
77
use PhpParser\Node\Stmt\ClassConst;
8+
use PhpParser\Node\Stmt\Expression;
89
use PhpParser\Node\Stmt\Property;
910
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
1011
use Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover;
@@ -53,10 +54,10 @@ final class SomeClass
5354
*/
5455
public function getNodeTypes(): array
5556
{
56-
return [Property::class, ClassConst::class];
57+
return [Property::class, ClassConst::class, Expression::class];
5758
}
5859
/**
59-
* @param Property|ClassConst $node
60+
* @param Property|ClassConst|Expression $node
6061
*/
6162
public function refactor(Node $node): ?Node
6263
{

rules/Php55/RegexMatcher.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public function resolvePatternExpressionWithoutEIfFound(Expr $expr)
4949
$delimiter = $delimiter;
5050
break;
5151
}
52-
/** @var string $modifiers */
5352
$modifiers = $this->resolveModifiers((string) Strings::after($pattern, $delimiter, -1));
5453
if (strpos($modifiers, 'e') === \false) {
5554
return null;

src/Application/VersionResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ final class VersionResolver
1919
* @api
2020
* @var string
2121
*/
22-
public const PACKAGE_VERSION = '6d81eab51f2637758ad0fbee47b2b6e3fb6309c8';
22+
public const PACKAGE_VERSION = 'eac1efd2ac2c2fce0ee6ed758d52f57acbf08a27';
2323
/**
2424
* @api
2525
* @var string
2626
*/
27-
public const RELEASE_DATE = '2026-02-01 00:55:31';
27+
public const RELEASE_DATE = '2026-02-01 16:47:01';
2828
/**
2929
* @var int
3030
*/

0 commit comments

Comments
 (0)