Skip to content

Commit 948ddb1

Browse files
authored
Fix argument.type PHPStan errors (#553)
* Fix invalid argument type PHPStan errors * fix cs * remove ignore * fix * cs * run rector * cs * fix another ignored error * Update phpstan.neon
1 parent 2ac1809 commit 948ddb1

13 files changed

+46
-17
lines changed

phpstan.neon

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,12 @@ parameters:
3434
# false positive
3535
- '#Access to an undefined property Rector\\Contract\\PhpParser\\Node\\StmtsAwareInterface\:\:\$stmts#'
3636

37-
- '#PhpParser\\Node\\Stmt\\Expression is not generic#'
38-
3937
# more advanced usage, but not always working
4038
# see https://github.com/rectorphp/rector-src/actions/runs/11798721617/job/32865546672?pr=6422#step:5:110
4139
- '#Doing instanceof PHPStan\\Type\\.+ is error\-prone and deprecated#'
4240

4341
-
4442
identifier: instanceof.alwaysTrue
4543

46-
-
47-
identifier: argument.type
48-
4944
-
5045
identifier: assign.propertyType
51-
52-
-
53-
message: '#Cannot call method getName\(\) on PHPStan\\Reflection\\ClassReflection\|null#'
54-
path: rules/CodeQuality/Reflection/MethodParametersAndReturnTypesResolver.php

rules/AnnotationsToAttributes/Rector/ClassMethod/DataProviderAnnotationToAttributeRector.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,12 @@ public function refactor(Node $node): ?Node
143143
$originalAttributeValue = $desiredTagValueNode->value->getOriginalContent();
144144
}
145145

146-
$node->attrGroups[] = $this->createAttributeGroup(strtok($originalAttributeValue, " \t\n\r\0\x0B"));
146+
$originalAttributeValueToken = strtok($originalAttributeValue ?: '', " \t\n\r\0\x0B");
147+
if ($originalAttributeValueToken === false) {
148+
continue;
149+
}
150+
151+
$node->attrGroups[] = $this->createAttributeGroup($originalAttributeValueToken);
147152

148153
// cleanup
149154
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $desiredTagValueNode);

rules/CodeQuality/NodeAnalyser/AssertMethodAnalyzer.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ public function detectTestCaseCallForStatic(MethodCall $methodCall): bool
7373
private function resolveMethodReflection(MethodCall|StaticCall $call): ?ExtendedMethodReflection
7474
{
7575
$methodName = $this->nodeNameResolver->getName($call->name);
76+
if ($methodName === null) {
77+
return null;
78+
}
7679

7780
$classReflection = $this->reflectionResolver->resolveClassReflection($call);
7881
if (! $classReflection instanceof ClassReflection) {

rules/CodeQuality/NodeAnalyser/NullableObjectAssignCollector.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ private function collectFromAssign(Assign $assign): ?VariableNameToType
7373
}
7474

7575
$variableName = $this->nodeNameResolver->getName($assign->var);
76+
if (! is_string($variableName)) {
77+
return null;
78+
}
79+
7680
return new VariableNameToType($variableName, $bareVariableType->getClassName());
7781
}
7882
}

rules/CodeQuality/Rector/ClassMethod/AddInstanceofAssertForNullableInstanceRector.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,12 @@ private function matchedNullableVariableNameToType(
197197
return null;
198198
}
199199

200-
$matchedNullableVariableNameToType = $variableNameToTypeCollection->matchByVariableName(
201-
$this->getName($node->var)
202-
);
200+
$variableName = $this->getName($node->var);
201+
if ($variableName === null) {
202+
return null;
203+
}
204+
205+
$matchedNullableVariableNameToType = $variableNameToTypeCollection->matchByVariableName($variableName);
203206

204207
// is the variable we're interested in?
205208
return null;

rules/CodeQuality/Rector/ClassMethod/CreateMockToAnonymousClassRector.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PhpParser\Node\Expr\ClassConstFetch;
1414
use PhpParser\Node\Expr\MethodCall;
1515
use PhpParser\Node\Expr\New_;
16+
use PhpParser\Node\Name;
1617
use PhpParser\Node\Scalar;
1718
use PhpParser\Node\Scalar\String_;
1819
use PhpParser\Node\Stmt;
@@ -200,6 +201,10 @@ private function createAnonymousClass(Arg $firstArg): Class_
200201
throw new NotImplementedYetException();
201202
}
202203

204+
if (! $className instanceof Name) {
205+
throw new NotImplementedYetException();
206+
}
207+
203208
// must respect PHPStan anonymous internal naming \Rector\NodeTypeResolver\PHPStan\Scope\PHPStanNodeScopeResolver::ANONYMOUS_CLASS_START_REGEX
204209
return new Class_('AnonymousClass1234', [
205210
'extends' => $className,

rules/CodeQuality/Rector/Class_/AddReturnTypeToDependedRector.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ public function refactor(Node $node): ?Node
110110
}
111111

112112
$soleReturnExpr = $returns[0]->expr;
113+
if ($soleReturnExpr === null) {
114+
continue;
115+
}
113116

114117
// does return a type?
115118
$returnedExprType = $this->nodeTypeResolver->getNativeType($soleReturnExpr);

rules/CodeQuality/Rector/Class_/PreferPHPUnitSelfCallRector.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ public function refactor(Node $node): ?Node
8888
}
8989

9090
$methodName = $this->getName($node->name);
91+
if ($methodName === null) {
92+
return null;
93+
}
9194

9295
$hasChanged = true;
9396
return $this->nodeFactory->createStaticCall('self', $methodName, $node->getArgs());

rules/CodeQuality/Rector/Class_/PreferPHPUnitThisCallRector.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ public function refactor(Node $node): ?Node
9898
}
9999

100100
$methodName = $this->getName($node->name);
101+
if ($methodName === null) {
102+
return null;
103+
}
101104

102105
$hasChanged = true;
103106
return $this->nodeFactory->createMethodCall('this', $methodName, $node->getArgs());

rules/CodeQuality/Rector/Class_/TypeWillReturnCallableArrowFunctionRector.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,13 @@ public function refactor(Node $node): ?Class_
235235
}
236236

237237
if (! $innerClosure->returnType instanceof Node) {
238+
$returnType = $parameterTypesAndReturnType->getReturnType();
239+
if (! $returnType instanceof Type) {
240+
return null;
241+
}
242+
238243
$returnTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode(
239-
$parameterTypesAndReturnType->getReturnType(),
244+
$returnType,
240245
TypeKind::RETURN
241246
);
242247

0 commit comments

Comments
 (0)