Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,12 @@ 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#'

-
identifier: instanceof.alwaysTrue

-
identifier: argument.type
Comment on lines -46 to -47
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since argument types were ignored, the root cause for #552 was not detected.


-
identifier: assign.propertyType

-
message: '#Cannot call method getName\(\) on PHPStan\\Reflection\\ClassReflection\|null#'
path: rules/CodeQuality/Reflection/MethodParametersAndReturnTypesResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions rules/CodeQuality/NodeAnalyser/AssertMethodAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


// is the variable we're interested in?
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -200,6 +201,10 @@ private function createAnonymousClass(Arg $firstArg): Class_
throw new NotImplementedYetException();
}

if (! $className instanceof Name) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably need test for $firstArg->value is a String_, not Foo::class, but "Foo", so if it is a String_, use its value, also possibly verify by value via ValueResolver, but that can be later 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, please do this for me.

I did not plan to deep dive into this codebase :)

throw new NotImplementedYetException();
}

// must respect PHPStan anonymous internal naming \Rector\NodeTypeResolver\PHPStan\Scope\PHPStanNodeScopeResolver::ANONYMOUS_CLASS_START_REGEX
return new Class_('AnonymousClass1234', [
'extends' => $className,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public function getNamedArguments(ClassMethod $classMethod): array
}

/**
* @param list<Node\Stmt> $stmts
* @param array<Node\Stmt> $stmts
* @return array<string, Array_>
*/
public function getResolvedVariables(array $stmts): array
Expand Down