|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\PHPUnit\CodeQuality\NodeAnalyser; |
| 6 | + |
| 7 | +use PhpParser\Node\Expr\Assign; |
| 8 | +use PhpParser\Node\Expr\MethodCall; |
| 9 | +use PhpParser\Node\Expr\PropertyFetch; |
| 10 | +use PhpParser\Node\Stmt\ClassMethod; |
| 11 | +use PhpParser\Node\Stmt\Expression; |
| 12 | +use Rector\NodeNameResolver\NodeNameResolver; |
| 13 | +use Rector\PhpParser\Node\Value\ValueResolver; |
| 14 | + |
| 15 | +final readonly class StubPropertyResolver |
| 16 | +{ |
| 17 | + public function __construct( |
| 18 | + private NodeNameResolver $nodeNameResolver, |
| 19 | + private ValueResolver $valueResolver, |
| 20 | + ) { |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * @return array<string, string> |
| 25 | + */ |
| 26 | + public function resolveFromClassMethod(ClassMethod $classMethod): array |
| 27 | + { |
| 28 | + $propertyNamesToStubClasses = []; |
| 29 | + |
| 30 | + foreach ((array) $classMethod->stmts as $stmt) { |
| 31 | + if (! $stmt instanceof Expression) { |
| 32 | + continue; |
| 33 | + } |
| 34 | + |
| 35 | + if (! $stmt->expr instanceof Assign) { |
| 36 | + continue; |
| 37 | + } |
| 38 | + |
| 39 | + $assign = $stmt->expr; |
| 40 | + |
| 41 | + if (! $assign->var instanceof PropertyFetch) { |
| 42 | + continue; |
| 43 | + } |
| 44 | + |
| 45 | + if (! $assign->expr instanceof MethodCall) { |
| 46 | + continue; |
| 47 | + } |
| 48 | + |
| 49 | + $methodCall = $assign->expr; |
| 50 | + if (! $this->nodeNameResolver->isName($methodCall->name, 'createStub')) { |
| 51 | + continue; |
| 52 | + } |
| 53 | + |
| 54 | + $propertyFetch = $assign->var; |
| 55 | + $propertyName = $this->nodeNameResolver->getName($propertyFetch->name); |
| 56 | + |
| 57 | + if (! is_string($propertyName)) { |
| 58 | + continue; |
| 59 | + } |
| 60 | + |
| 61 | + $firstArg = $methodCall->getArgs()[0]; |
| 62 | + $stubbedClassName = $this->valueResolver->getValue($firstArg->value); |
| 63 | + |
| 64 | + $propertyNamesToStubClasses[$propertyName] = $stubbedClassName; |
| 65 | + } |
| 66 | + |
| 67 | + return $propertyNamesToStubClasses; |
| 68 | + } |
| 69 | +} |
0 commit comments