diff --git a/phpstan.neon b/phpstan.neon index 36ae3df3..52a516bd 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -34,8 +34,6 @@ parameters: # false positive - '#Access to an undefined property Rector\\Contract\\PhpParser\\Node\\StmtsAwareInterface\:\:\$stmts#' - - '#PhpParser\\Node\\Stmt\\Expression is not generic#' - # more advanced usage, but not always working # see https://github.com/rectorphp/rector-src/actions/runs/11798721617/job/32865546672?pr=6422#step:5:110 - '#Doing instanceof PHPStan\\Type\\.+ is error\-prone and deprecated#' @@ -43,12 +41,5 @@ parameters: - identifier: instanceof.alwaysTrue - - - identifier: argument.type - - identifier: assign.propertyType - - - - message: '#Cannot call method getName\(\) on PHPStan\\Reflection\\ClassReflection\|null#' - path: rules/CodeQuality/Reflection/MethodParametersAndReturnTypesResolver.php diff --git a/rules/AnnotationsToAttributes/Rector/ClassMethod/DataProviderAnnotationToAttributeRector.php b/rules/AnnotationsToAttributes/Rector/ClassMethod/DataProviderAnnotationToAttributeRector.php index 0287fce4..e9bbc351 100644 --- a/rules/AnnotationsToAttributes/Rector/ClassMethod/DataProviderAnnotationToAttributeRector.php +++ b/rules/AnnotationsToAttributes/Rector/ClassMethod/DataProviderAnnotationToAttributeRector.php @@ -143,7 +143,12 @@ public function refactor(Node $node): ?Node $originalAttributeValue = $desiredTagValueNode->value->getOriginalContent(); } - $node->attrGroups[] = $this->createAttributeGroup(strtok($originalAttributeValue, " \t\n\r\0\x0B")); + $originalAttributeValueToken = strtok($originalAttributeValue ?: '', " \t\n\r\0\x0B"); + if ($originalAttributeValueToken === false) { + continue; + } + + $node->attrGroups[] = $this->createAttributeGroup($originalAttributeValueToken); // cleanup $this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $desiredTagValueNode); diff --git a/rules/CodeQuality/NodeAnalyser/AssertMethodAnalyzer.php b/rules/CodeQuality/NodeAnalyser/AssertMethodAnalyzer.php index 7c0ef823..562ccbfa 100644 --- a/rules/CodeQuality/NodeAnalyser/AssertMethodAnalyzer.php +++ b/rules/CodeQuality/NodeAnalyser/AssertMethodAnalyzer.php @@ -73,6 +73,9 @@ public function detectTestCaseCallForStatic(MethodCall $methodCall): bool private function resolveMethodReflection(MethodCall|StaticCall $call): ?ExtendedMethodReflection { $methodName = $this->nodeNameResolver->getName($call->name); + if ($methodName === null) { + return null; + } $classReflection = $this->reflectionResolver->resolveClassReflection($call); if (! $classReflection instanceof ClassReflection) { diff --git a/rules/CodeQuality/NodeAnalyser/NullableObjectAssignCollector.php b/rules/CodeQuality/NodeAnalyser/NullableObjectAssignCollector.php index 5e5f41a7..f4cc804f 100644 --- a/rules/CodeQuality/NodeAnalyser/NullableObjectAssignCollector.php +++ b/rules/CodeQuality/NodeAnalyser/NullableObjectAssignCollector.php @@ -73,6 +73,10 @@ private function collectFromAssign(Assign $assign): ?VariableNameToType } $variableName = $this->nodeNameResolver->getName($assign->var); + if (! is_string($variableName)) { + return null; + } + return new VariableNameToType($variableName, $bareVariableType->getClassName()); } } diff --git a/rules/CodeQuality/Rector/ClassMethod/AddInstanceofAssertForNullableInstanceRector.php b/rules/CodeQuality/Rector/ClassMethod/AddInstanceofAssertForNullableInstanceRector.php index b7704f2e..f24e41c7 100644 --- a/rules/CodeQuality/Rector/ClassMethod/AddInstanceofAssertForNullableInstanceRector.php +++ b/rules/CodeQuality/Rector/ClassMethod/AddInstanceofAssertForNullableInstanceRector.php @@ -197,9 +197,12 @@ private function matchedNullableVariableNameToType( return null; } - $matchedNullableVariableNameToType = $variableNameToTypeCollection->matchByVariableName( - $this->getName($node->var) - ); + $variableName = $this->getName($node->var); + if ($variableName === null) { + return null; + } + + $matchedNullableVariableNameToType = $variableNameToTypeCollection->matchByVariableName($variableName); // is the variable we're interested in? return null; diff --git a/rules/CodeQuality/Rector/ClassMethod/CreateMockToAnonymousClassRector.php b/rules/CodeQuality/Rector/ClassMethod/CreateMockToAnonymousClassRector.php index 8fec01db..1342bc88 100644 --- a/rules/CodeQuality/Rector/ClassMethod/CreateMockToAnonymousClassRector.php +++ b/rules/CodeQuality/Rector/ClassMethod/CreateMockToAnonymousClassRector.php @@ -13,6 +13,7 @@ use PhpParser\Node\Expr\ClassConstFetch; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\New_; +use PhpParser\Node\Name; use PhpParser\Node\Scalar; use PhpParser\Node\Scalar\String_; use PhpParser\Node\Stmt; @@ -200,6 +201,10 @@ private function createAnonymousClass(Arg $firstArg): Class_ throw new NotImplementedYetException(); } + if (! $className instanceof Name) { + throw new NotImplementedYetException(); + } + // must respect PHPStan anonymous internal naming \Rector\NodeTypeResolver\PHPStan\Scope\PHPStanNodeScopeResolver::ANONYMOUS_CLASS_START_REGEX return new Class_('AnonymousClass1234', [ 'extends' => $className, diff --git a/rules/CodeQuality/Rector/Class_/AddReturnTypeToDependedRector.php b/rules/CodeQuality/Rector/Class_/AddReturnTypeToDependedRector.php index 4f357bb3..79e718fb 100644 --- a/rules/CodeQuality/Rector/Class_/AddReturnTypeToDependedRector.php +++ b/rules/CodeQuality/Rector/Class_/AddReturnTypeToDependedRector.php @@ -110,6 +110,9 @@ public function refactor(Node $node): ?Node } $soleReturnExpr = $returns[0]->expr; + if ($soleReturnExpr === null) { + continue; + } // does return a type? $returnedExprType = $this->nodeTypeResolver->getNativeType($soleReturnExpr); diff --git a/rules/CodeQuality/Rector/Class_/PreferPHPUnitSelfCallRector.php b/rules/CodeQuality/Rector/Class_/PreferPHPUnitSelfCallRector.php index 2f6b1a25..d3d45ad5 100644 --- a/rules/CodeQuality/Rector/Class_/PreferPHPUnitSelfCallRector.php +++ b/rules/CodeQuality/Rector/Class_/PreferPHPUnitSelfCallRector.php @@ -88,6 +88,9 @@ public function refactor(Node $node): ?Node } $methodName = $this->getName($node->name); + if ($methodName === null) { + return null; + } $hasChanged = true; return $this->nodeFactory->createStaticCall('self', $methodName, $node->getArgs()); diff --git a/rules/CodeQuality/Rector/Class_/PreferPHPUnitThisCallRector.php b/rules/CodeQuality/Rector/Class_/PreferPHPUnitThisCallRector.php index 51e8e4e8..cde87824 100644 --- a/rules/CodeQuality/Rector/Class_/PreferPHPUnitThisCallRector.php +++ b/rules/CodeQuality/Rector/Class_/PreferPHPUnitThisCallRector.php @@ -98,6 +98,9 @@ public function refactor(Node $node): ?Node } $methodName = $this->getName($node->name); + if ($methodName === null) { + return null; + } $hasChanged = true; return $this->nodeFactory->createMethodCall('this', $methodName, $node->getArgs()); diff --git a/rules/CodeQuality/Rector/Class_/TypeWillReturnCallableArrowFunctionRector.php b/rules/CodeQuality/Rector/Class_/TypeWillReturnCallableArrowFunctionRector.php index e415d9cc..fcd89013 100644 --- a/rules/CodeQuality/Rector/Class_/TypeWillReturnCallableArrowFunctionRector.php +++ b/rules/CodeQuality/Rector/Class_/TypeWillReturnCallableArrowFunctionRector.php @@ -235,8 +235,13 @@ public function refactor(Node $node): ?Class_ } if (! $innerClosure->returnType instanceof Node) { + $returnType = $parameterTypesAndReturnType->getReturnType(); + if (! $returnType instanceof Type) { + return null; + } + $returnTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode( - $parameterTypesAndReturnType->getReturnType(), + $returnType, TypeKind::RETURN ); diff --git a/rules/CodeQuality/Reflection/MethodParametersAndReturnTypesResolver.php b/rules/CodeQuality/Reflection/MethodParametersAndReturnTypesResolver.php index ce968de9..fd31f59b 100644 --- a/rules/CodeQuality/Reflection/MethodParametersAndReturnTypesResolver.php +++ b/rules/CodeQuality/Reflection/MethodParametersAndReturnTypesResolver.php @@ -189,7 +189,7 @@ public function resolveParameterTypes( foreach ($extendedParametersAcceptor->getParameters() as $parameterReflection) { $parameterType = $this->resolveObjectType($parameterReflection->getNativeType()); - if ($parameterType instanceof ObjectType && $currentClassReflection->getName() !== $parameterType->getClassReflection()->getName()) { + if ($parameterType instanceof ObjectType && $currentClassReflection->getName() !== $parameterType->getClassReflection()?->getName()) { $parameterTypes[] = new MixedType(); continue; } @@ -240,7 +240,7 @@ private function resolveReturnType( $returnType = $this->resolveObjectType($extendedParametersAcceptor->getNativeReturnType()); - if ($returnType instanceof ObjectType && $currentClassReflection->getName() !== $returnType->getClassReflection()->getName()) { + if ($returnType instanceof ObjectType && $currentClassReflection->getName() !== $returnType->getClassReflection()?->getName()) { return new MixedType(); } diff --git a/rules/PHPUnit100/Rector/Class_/RemoveNamedArgsInDataProviderRector.php b/rules/PHPUnit100/Rector/Class_/RemoveNamedArgsInDataProviderRector.php index 66ee91e9..3b91c99b 100644 --- a/rules/PHPUnit100/Rector/Class_/RemoveNamedArgsInDataProviderRector.php +++ b/rules/PHPUnit100/Rector/Class_/RemoveNamedArgsInDataProviderRector.php @@ -102,6 +102,10 @@ public function refactor(Node $node): ?Node $expr = $stmt->expr; $arrayChanged = false; if ($expr instanceof Yield_) { + if (! $expr->value instanceof Array_) { + return null; + } + $arrayChanged = $this->handleArray($expr->value); } elseif ($expr instanceof Array_) { $arrayChanged = $this->handleArray($expr); diff --git a/rules/PHPUnit110/Rector/Class_/NamedArgumentForDataProviderRector.php b/rules/PHPUnit110/Rector/Class_/NamedArgumentForDataProviderRector.php index c669a6c7..84f10a7b 100644 --- a/rules/PHPUnit110/Rector/Class_/NamedArgumentForDataProviderRector.php +++ b/rules/PHPUnit110/Rector/Class_/NamedArgumentForDataProviderRector.php @@ -154,7 +154,7 @@ public function getNamedArguments(ClassMethod $classMethod): array } /** - * @param list $stmts + * @param array $stmts * @return array */ public function getResolvedVariables(array $stmts): array