|
3 | 3 | declare (strict_types=1); |
4 | 4 | namespace Rector\NodeTypeResolver; |
5 | 5 |
|
| 6 | +use PhpParser\Node\Name\FullyQualified; |
6 | 7 | use PhpParser\Node; |
7 | 8 | use PhpParser\Node\Expr; |
8 | 9 | use PhpParser\Node\Expr\ArrayDimFetch; |
@@ -281,6 +282,9 @@ public function getFullyQualifiedClassName(TypeWithClassName $typeWithClassName) |
281 | 282 | public function isMethodStaticCallOrClassMethodObjectType(Node $node, ObjectType $objectType) : bool |
282 | 283 | { |
283 | 284 | if ($node instanceof MethodCall || $node instanceof NullsafeMethodCall) { |
| 285 | + if ($this->isEnumTypeMatch($node, $objectType)) { |
| 286 | + return \true; |
| 287 | + } |
284 | 288 | // method call is variable return |
285 | 289 | return $this->isObjectType($node->var, $objectType); |
286 | 290 | } |
@@ -497,4 +501,27 @@ private function resolveNativeTypeWithBuiltinMethodCallFallback(Expr $expr, Scop |
497 | 501 | } |
498 | 502 | return $scope->getNativeType($expr); |
499 | 503 | } |
| 504 | + /** |
| 505 | + * @param \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\NullsafeMethodCall $call |
| 506 | + */ |
| 507 | + private function isEnumTypeMatch($call, ObjectType $objectType) : bool |
| 508 | + { |
| 509 | + if (!$call->var instanceof ClassConstFetch) { |
| 510 | + return \false; |
| 511 | + } |
| 512 | + // possibly enum |
| 513 | + $classConstFetch = $call->var; |
| 514 | + if (!$classConstFetch->class instanceof FullyQualified) { |
| 515 | + return \false; |
| 516 | + } |
| 517 | + $className = $classConstFetch->class->toString(); |
| 518 | + if (!$this->reflectionProvider->hasClass($className)) { |
| 519 | + return \false; |
| 520 | + } |
| 521 | + $classReflection = $this->reflectionProvider->getClass($className); |
| 522 | + if (!$classReflection->isEnum()) { |
| 523 | + return \false; |
| 524 | + } |
| 525 | + return $classReflection->getName() === $objectType->getClassName(); |
| 526 | + } |
500 | 527 | } |
0 commit comments