|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace RectorLaravel\Rector\StaticCall; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Arg; |
| 9 | +use PhpParser\Node\Expr\ArrowFunction; |
| 10 | +use PhpParser\Node\Expr\Closure; |
| 11 | +use PhpParser\Node\Expr\StaticCall; |
| 12 | +use PhpParser\Node\Name\FullyQualified; |
| 13 | +use RectorLaravel\AbstractRector; |
| 14 | +use RectorLaravel\NodeAnalyzer\FacadeAssertionAnalyzer; |
| 15 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 16 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 17 | + |
| 18 | +/** |
| 19 | + * @see \RectorLaravel\Tests\Rector\StaticCall\AssertWithClassStringToTypeHintedClosureRector\AssertWithClassStringToTypeHintedClosureRectorTest |
| 20 | + */ |
| 21 | +final class AssertWithClassStringToTypeHintedClosureRector extends AbstractRector |
| 22 | +{ |
| 23 | + public function __construct( |
| 24 | + private readonly FacadeAssertionAnalyzer $facadeAssertionAnalyzer |
| 25 | + ) {} |
| 26 | + |
| 27 | + public function getRuleDefinition(): RuleDefinition |
| 28 | + { |
| 29 | + return new RuleDefinition( |
| 30 | + 'Changes assert calls to use a type hinted closure.', |
| 31 | + [new CodeSample( |
| 32 | + <<<'CODE_SAMPLE' |
| 33 | +Bus::assertDispatched(OrderCreated::class, function ($job) { |
| 34 | + return true; |
| 35 | +}); |
| 36 | +CODE_SAMPLE, |
| 37 | + <<<'CODE_SAMPLE' |
| 38 | +Bus::assertDispatched(function (OrderCreated $job) { |
| 39 | + return true; |
| 40 | +}); |
| 41 | +CODE_SAMPLE |
| 42 | + )] |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @return array<class-string<Node>> |
| 48 | + */ |
| 49 | + public function getNodeTypes(): array |
| 50 | + { |
| 51 | + return [StaticCall::class]; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @param StaticCall $node |
| 56 | + */ |
| 57 | + public function refactor(Node $node): ?StaticCall |
| 58 | + { |
| 59 | + if (! $this->facadeAssertionAnalyzer->isFacadeAssertion($node)) { |
| 60 | + return null; |
| 61 | + } |
| 62 | + |
| 63 | + if ($node->isFirstClassCallable()) { |
| 64 | + return null; |
| 65 | + } |
| 66 | + |
| 67 | + if (count($node->getArgs()) !== 2) { |
| 68 | + return null; |
| 69 | + } |
| 70 | + |
| 71 | + if (! $node->args[1] instanceof Arg) { |
| 72 | + return null; |
| 73 | + } |
| 74 | + |
| 75 | + $functionLike = $node->args[1]->value; |
| 76 | + |
| 77 | + if ( |
| 78 | + (! $functionLike instanceof Closure |
| 79 | + && ! $functionLike instanceof ArrowFunction) |
| 80 | + || ! isset($functionLike->params[0]) |
| 81 | + ) { |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | + if (! $node->args[0] instanceof Arg) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + $type = $this->getType($node->args[0]->value); |
| 90 | + |
| 91 | + $classString = match (true) { |
| 92 | + /** @phpstan-ignore method.notFound */ |
| 93 | + $type->isClassString()->yes() => $type->getClassStringObjectType()->getClassName(), |
| 94 | + $type->isString()->yes() |
| 95 | + /** @phpstan-ignore method.notFound */ |
| 96 | + && $type->getClassStringObjectType()->isObject()->yes() => $type->getClassStringObjectType()->getClassName(), |
| 97 | + default => null, |
| 98 | + }; |
| 99 | + |
| 100 | + if (! is_string($classString)) { |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + return $this->refactorClosure($node, $functionLike, $classString); |
| 105 | + } |
| 106 | + |
| 107 | + public function refactorClosure(StaticCall $staticCall, Closure|ArrowFunction $closure, string $class): StaticCall |
| 108 | + { |
| 109 | + $closure->params[0]->type = new FullyQualified($class); |
| 110 | + |
| 111 | + $staticCall->args = [ |
| 112 | + new Arg($closure), |
| 113 | + ]; |
| 114 | + |
| 115 | + return $staticCall; |
| 116 | + } |
| 117 | +} |
0 commit comments