Skip to content
Closed
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 @@ -10,6 +10,13 @@ final class ReturnAnonymousClass
{
return new class {};
}

public function foo()
{
$data = new class {};

return $data;
}
}

?>
Expand All @@ -26,6 +33,13 @@ final class ReturnAnonymousClass
{
return new class {};
}

public function foo(): object
{
$data = new class {};

return $data;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\ClassMethod;
Expand Down Expand Up @@ -120,7 +121,9 @@ public function refactor(Node $node): ?Node
$returnedNewClassName = $this->strictReturnNewAnalyzer->matchAlwaysReturnVariableNew($node);

if (is_string($returnedNewClassName)) {
$node->returnType = new FullyQualified($returnedNewClassName);
$node->returnType = str_starts_with($returnedNewClassName, 'AnonymousClass')
Copy link
Member

Choose a reason for hiding this comment

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

I think this can be covered by NodeTypeResolver itself, via ClassReflection->isAnonymous(), check in TypeTraverser::map(), see example

return TypeTraverser::map($mainType, function (Type $traversedType, callable $traverseCallback): Type {

Copy link
Member

Choose a reason for hiding this comment

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

use getNativeType() over getType() should works for this.

Copy link
Member

Choose a reason for hiding this comment

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

I created separate PR:

to use native type, which more reliable for this strict type rule.

? new Identifier('object')
: new FullyQualified($returnedNewClassName);

return $node;
}
Expand Down
Loading