Skip to content

Commit 07d14a1

Browse files
committed
Add PHPStan rule to flag same class-type union in param and return
Flags methods like (A|B $node): A|B|null that should use a @template generic to narrow the return to the passed type. Applies the template to the 13 existing matches.
1 parent 672d7fb commit 07d14a1

16 files changed

Lines changed: 267 additions & 0 deletions

File tree

‎phpstan.neon‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ includes:
44
rules:
55
- Rector\Utils\PHPStan\Rule\RegisterRelatedPolyfillRectorRule
66
- Rector\Utils\PHPStan\Rule\NoScopeFetchBeforeCheapGuardRule
7+
- Rector\Utils\PHPStan\Rule\PreferGenericTemplateOnSameUnionTypeRule
78

89
parameters:
910
level: 8

‎rules/CodingStyle/Rector/ClassMethod/FuncGetArgsToVariadicParamRector.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ public function provideMinPhpVersion(): int
109109
return PhpVersionFeature::VARIADIC_PARAM;
110110
}
111111

112+
/**
113+
* @template TFunctionLike of ClassMethod|Function_|Closure
114+
* @param TFunctionLike $node
115+
* @return TFunctionLike|null
116+
*/
112117
private function applyVariadicParams(
113118
ClassMethod|Function_|Closure $node,
114119
string $variableName

‎rules/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,11 @@ private function getValidEnumExpr(Node $node): null|ClassConstFetch|Expr
244244
};
245245
}
246246

247+
/**
248+
* @template TExpr of PropertyFetch|Variable
249+
* @param TExpr $expr
250+
* @return TExpr|null
251+
*/
247252
private function getPropertyFetchOrVariable(PropertyFetch|Variable $expr): null|PropertyFetch|Variable
248253
{
249254
if (! $this->isObjectType($expr, new ObjectType('MyCLabs\Enum\Enum'))) {

‎rules/Privatization/NodeManipulator/VisibilityManipulator.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ public function removeReadonly(Class_|Property|Param $node): void
159159
}
160160
}
161161

162+
/**
163+
* @template TNode of ClassConst|ClassMethod|Property
164+
* @param TNode $node
165+
* @return TNode|null
166+
*/
162167
public function publicize(ClassConst|ClassMethod|Property $node): ClassConst|ClassMethod|Property|null
163168
{
164169
// already non-public

‎rules/Renaming/Rector/MethodCall/RenameMethodRector.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ private function shouldKeepForParentInterface(
157157
);
158158
}
159159

160+
/**
161+
* @template TClassLike of Class_|Trait_|Interface_
162+
* @param TClassLike $classOrInterface
163+
* @return TClassLike|null
164+
*/
160165
private function refactorClass(
161166
Class_|Trait_|Interface_ $classOrInterface,
162167
Scope $scope

‎rules/Renaming/Rector/PropertyFetch/RenamePropertyRector.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ private function isUsingTrait(ClassLike $classLike, string $traitName): bool
128128
return $classReflection->hasTraitUse($traitName);
129129
}
130130

131+
/**
132+
* @template TPropertyFetch of PropertyFetch|StaticPropertyFetch
133+
* @param TPropertyFetch $propertyFetch
134+
* @return TPropertyFetch|null
135+
*/
131136
private function refactorPropertyFetch(
132137
PropertyFetch|StaticPropertyFetch $propertyFetch
133138
): null|PropertyFetch|StaticPropertyFetch {

‎rules/TypeDeclaration/NodeAnalyzer/StrictReturnNewArrayResolver.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public function __construct(
4949
/**
5050
* @return ClassMethod|Function_|null the passed node when an array return type was added, null otherwise
5151
*/
52+
/**
53+
* @template TFunctionLike of ClassMethod|Function_
54+
* @param TFunctionLike $node
55+
* @return TFunctionLike|null
56+
*/
5257
public function resolve(ClassMethod|Function_ $node): ClassMethod|Function_|null
5358
{
5459
// 1. is variable instantiated with array
@@ -90,6 +95,11 @@ public function resolve(ClassMethod|Function_ $node): ClassMethod|Function_|null
9095
return $this->processAddArrayReturnType($node, $returnType);
9196
}
9297

98+
/**
99+
* @template TFunctionLike of ClassMethod|Function_
100+
* @param TFunctionLike $node
101+
* @return TFunctionLike|null
102+
*/
93103
private function processAddArrayReturnType(
94104
ClassMethod|Function_ $node,
95105
Type $returnType

‎rules/TypeDeclaration/NodeManipulator/AddNeverReturnType.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ public function __construct(
3333
) {
3434
}
3535

36+
/**
37+
* @template TFunctionLike of ClassMethod|Function_|Closure
38+
* @param TFunctionLike $node
39+
* @return TFunctionLike|null
40+
*/
3641
public function add(ClassMethod|Function_|Closure $node, Scope $scope): ClassMethod|Function_|Closure|null
3742
{
3843
if ($this->shouldSkip($node, $scope)) {

‎rules/TypeDeclaration/NodeManipulator/AddReturnTypeFromCast.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ public function __construct(
2727
) {
2828
}
2929

30+
/**
31+
* @template TFunctionLike of ClassMethod|Function_
32+
* @param TFunctionLike $functionLike
33+
* @return TFunctionLike|null
34+
*/
3035
public function add(ClassMethod|Function_ $functionLike, Scope $scope): ClassMethod|Function_|null
3136
{
3237
if ($functionLike->returnType instanceof Node) {

‎rules/TypeDeclaration/NodeManipulator/AddReturnTypeFromParam.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public function __construct(
3636
) {
3737
}
3838

39+
/**
40+
* @template TFunctionLike of ClassMethod|Function_
41+
* @param TFunctionLike $functionLike
42+
* @return TFunctionLike|null
43+
*/
3944
public function add(ClassMethod|Function_ $functionLike, Scope $scope): ClassMethod|Function_|null
4045
{
4146
if ($functionLike->stmts === null) {

0 commit comments

Comments
 (0)