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
21 changes: 19 additions & 2 deletions packages/NodeTypeResolver/src/NodeTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
Expand Down Expand Up @@ -297,6 +298,12 @@ public function getNodeStaticType(Node $node): ?Type
return null;
}

if ($node instanceof New_) {
if ($this->isAnonymousClass($node->class)) {
return new ObjectWithoutClassType();
}
}

return $nodeScope->getType($node);
}

Expand Down Expand Up @@ -410,8 +417,7 @@ private function resolveFirstTypes(Node $node): array

// skip anonymous classes, ref https://github.com/rectorphp/rector/issues/1574
if ($node instanceof New_) {
$className = $this->nameResolver->resolve($node->class);
if ($className === null || Strings::contains($className, 'AnonymousClass')) {
if ($this->isAnonymousClass($node->class)) {
return [];
}
}
Expand Down Expand Up @@ -556,4 +562,15 @@ private function getClassNodeProperty(Class_ $class, string $name): ?PropertyPro

return null;
}

private function isAnonymousClass(Node $node): bool
{
if (! $node instanceof Class_) {
return false;
}

$className = $this->nameResolver->resolve($node);

return $className === null || Strings::contains($className, 'AnonymousClass');
}
}
2 changes: 2 additions & 0 deletions packages/NodeTypeResolver/src/StaticTypeToStringResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPStan\Type\IntegerType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
Expand All @@ -26,6 +27,7 @@ public function __construct(CallableCollectorPopulator $callableCollectorPopulat
{
$resolvers = [
IntegerType::class => ['int'],
ObjectWithoutClassType::class => ['object'],
ClosureType::class => ['callable'],
CallableType::class => ['callable'],
FloatType::class => ['float'],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\TypeDeclaration\Tests\Rector\ClassMethod\ReturnTypeDeclarationRector\Fixture;

class ANewClass
{
public function getObject()
{
return new class() {};
}
}

?>
-----
<?php

namespace Rector\TypeDeclaration\Tests\Rector\ClassMethod\ReturnTypeDeclarationRector\Fixture;

class ANewClass
{
public function getObject(): object
{
return new class() {};
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public function test(): void
__DIR__ . '/Fixture/dunglas/BazTrait.php.inc',
__DIR__ . '/Fixture/dunglas/Child.php.inc',
__DIR__ . '/Fixture/dunglas/nullable_types.php.inc',
// anonymous class
__DIR__ . '/Fixture/a_new_class.php.inc',
];

$this->doTestFiles($integrationFiles);
Expand Down