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
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,16 @@ public function refactor(Node $node): ?Node
return null;
}

$returnType = $this->returnTypeInferer->inferFunctionLike($node);
if (! $returnType instanceof StaticType && ! $returnType instanceof ObjectType) {
return null;
}

$classReflection = $this->reflectionResolver->resolveClassReflection($node);
if (! $classReflection instanceof ClassReflection) {
return null;
}

$returnType = $this->returnTypeInferer->inferFunctionLike($node);

if ($returnType instanceof StaticType && $returnType->getStaticObjectType()->getClassName() === $classReflection->getName()) {
return $this->processAddReturnSelfOrStatic($node, $classReflection);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ private function resolveReturnPropertyType(ClassMethod $classMethod): array
{
$returns = $this->betterNodeFinder->findReturnsScoped($classMethod);

if (! $this->returnAnalyzer->hasOnlyReturnWithExpr($classMethod, $returns)) {
return [];
}

$propertyTypes = [];

foreach ($returns as $return) {
Expand All @@ -154,10 +158,6 @@ private function resolveReturnPropertyType(ClassMethod $classMethod): array
$propertyTypes[] = $this->nodeTypeResolver->getNativeType($return->expr);
}

if (! $this->returnAnalyzer->hasOnlyReturnWithExpr($classMethod, $returns)) {
return [];
}

return $propertyTypes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\TypeDeclaration\Rector\Class_;

use PhpParser\Comment\Doc;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
Expand Down Expand Up @@ -94,6 +95,11 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
// entity type is resolved from the @extends docblock, bail early when absent
if (!$node->getDocComment() instanceof Doc) {
return null;
}

if (! $this->isObjectType($node, new ObjectType('Doctrine\ORM\EntityRepository'))) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,13 @@ public function refactor(Node $node): ?Node
continue;
}

// constructor assignment can set a different type than the setter/getter → skip
if ($this->isAssignedInConstructor($node, $property)) {
$getterSetterPropertyType = $this->matchGetterSetterIdenticalType($property, $node);
if (! $getterSetterPropertyType instanceof Type) {
continue;
}

$getterSetterPropertyType = $this->matchGetterSetterIdenticalType($property, $node);
if (! $getterSetterPropertyType instanceof Type) {
// constructor assignment can set a different type than the setter/getter → skip
if ($this->isAssignedInConstructor($node, $property)) {
continue;
}

Expand Down
Loading