Skip to content

Commit

Permalink
[TypeMapper] Use Identifier instead of Name on ObjectWithoutClassType (
Browse files Browse the repository at this point in the history
…#3377)

* [TypeMapper] Use Identifier instead of Name on ObjectWithoutClassType

* [ci-review] Rector Rectify

* remove unnecesary check on ObjectType

* [ci-review] Rector Rectify

* Final touch: comment

* early return for non ObjectType after ObjectWithoutClassType check

* do not try to collect intersection type if the class is not found, as the class data will not complete

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user authored Feb 14, 2023
1 parent dbaf0da commit 56e1633
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;

use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Reflection\ReflectionProvider;
Expand Down Expand Up @@ -82,26 +83,26 @@ public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
foreach ($type->getTypes() as $intersectionedType) {
$resolvedType = $this->phpStanStaticTypeMapper->mapToPhpParserNode($intersectionedType, $typeKind);

if (! $resolvedType instanceof Name) {
continue;
if (! $resolvedType instanceof Name && ! $resolvedType instanceof Identifier) {
return null;
}

$resolvedTypeName = (string) $resolvedType;

/**
* ObjectWithoutClassType can happen when use along with \PHPStan\Type\Accessory\HasMethodType
* Use "object" as returned type
*/
if ($intersectionedType instanceof ObjectWithoutClassType) {
return $resolvedType;
}

/**
* $this->reflectionProvider->hasClass($resolvedTypeName) returns true on iterable type
* this ensure type is ObjectType early
*/
if (! $intersectionedType instanceof ObjectType) {
continue;
return null;
}

if (! $this->reflectionProvider->hasClass($resolvedTypeName)) {
continue;
return null;
}

$intersectionedTypeNodes[] = $resolvedType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;

use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
Expand Down Expand Up @@ -82,6 +82,6 @@ public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
return null;
}

return new Name('object');
return new Identifier('object');
}
}
2 changes: 1 addition & 1 deletion rules/Php80/Rector/FunctionLike/UnionTypesRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ private function changeObjectWithoutClassType(Param $param, UnionType $unionType
return;
}

$param->type = new Name('object');
$param->type = new Identifier('object');
$this->hasChanged = true;
}

Expand Down

0 comments on commit 56e1633

Please sign in to comment.