Skip to content

Commit 330031f

Browse files
committed
Updated Rector to commit 909090d68bd4a522c7986459c038c7c46a926948
rectorphp/rector-src@909090d [DeadCode] Keep generic @var/@return self<...> narrowing annotations (#8147)
1 parent c531f9b commit 330031f

3 files changed

Lines changed: 25 additions & 4 deletions

File tree

rules/DeadCode/Rector/ClassMethod/RemoveDuplicatedReturnSelfDocblockRector.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
1111
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
1212
use PHPStan\Reflection\ClassReflection;
13+
use PHPStan\Type\Generic\GenericObjectType;
1314
use PHPStan\Type\ObjectType;
1415
use PHPStan\Type\StaticType;
1516
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
@@ -117,6 +118,10 @@ public function refactor(Node $node): ?Node
117118
private function isCurrentObjectReturnDocType(TypeNode $typeNode, ClassMethod $classMethod, ClassReflection $classReflection): bool
118119
{
119120
$docType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType($typeNode, $classMethod);
121+
// generic narrowing, e.g. @return self<TValue, never> is not a plain duplicate of the native self type
122+
if ($docType instanceof GenericObjectType) {
123+
return \false;
124+
}
120125
// covers @return $this and @return static
121126
if ($docType instanceof StaticType) {
122127
return $docType->getClassName() === $classReflection->getName();

rules/DeadCode/Rector/Node/RemoveNonExistingVarAnnotationRector.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use PhpParser\Node\Stmt\Switch_;
2222
use PhpParser\Node\Stmt\While_;
2323
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
24+
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
2425
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
2526
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
2627
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
@@ -138,7 +139,7 @@ public function refactor(Node $node): ?Node
138139
continue;
139140
}
140141
$variableName = ltrim($varTagValueNode->variableName, '$');
141-
if ($variableName === '' && $this->isAllowedEmptyVariableName($stmt)) {
142+
if ($variableName === '' && $this->isAllowedEmptyVariableName($stmt, $varTagValueNode)) {
142143
continue;
143144
}
144145
if ($this->hasVariableName($stmt, $variableName)) {
@@ -214,11 +215,26 @@ private function isObjectShapePseudoType(VarTagValueNode $varTagValueNode): bool
214215
}
215216
return strpos($varTagValueNode->description, '}') !== \false;
216217
}
217-
private function isAllowedEmptyVariableName(Stmt $stmt): bool
218+
private function isAllowedEmptyVariableName(Stmt $stmt, VarTagValueNode $varTagValueNode): bool
218219
{
219220
if ($stmt instanceof Return_ && $stmt->expr instanceof CallLike && !$stmt->expr instanceof New_) {
220221
return \true;
221222
}
223+
// generic/template narrowing over the instantiated class, e.g. /** @var self<TValue, never> */ return new self(...)
224+
if ($stmt instanceof Return_ && $stmt->expr instanceof New_ && $this->isGenericTypeOfNewClass($stmt->expr, $varTagValueNode)) {
225+
return \true;
226+
}
222227
return $stmt instanceof Expression && $stmt->expr instanceof Assign && $stmt->expr->var instanceof Variable;
223228
}
229+
private function isGenericTypeOfNewClass(New_ $new, VarTagValueNode $varTagValueNode): bool
230+
{
231+
if (!$varTagValueNode->type instanceof GenericTypeNode) {
232+
return \false;
233+
}
234+
$newClassName = $this->getName($new->class);
235+
if ($newClassName === null) {
236+
return \false;
237+
}
238+
return strcasecmp($varTagValueNode->type->type->name, $newClassName) === 0;
239+
}
224240
}

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 = '9ca6fae0e4ed28519c8e942971e3e347bf74229b';
22+
public const PACKAGE_VERSION = '909090d68bd4a522c7986459c038c7c46a926948';
2323
/**
2424
* @api
2525
* @var string
2626
*/
27-
public const RELEASE_DATE = '2026-07-05 11:23:59';
27+
public const RELEASE_DATE = '2026-07-05 14:50:36';
2828
/**
2929
* @var int
3030
*/

0 commit comments

Comments
 (0)