|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\Symfony\Configs\Rector\Closure; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Expr; |
| 9 | +use PhpParser\Node\Expr\Closure; |
| 10 | +use PhpParser\Node\Expr\MethodCall; |
| 11 | +use Rector\Rector\AbstractRector; |
| 12 | +use Rector\Symfony\Configs\NodeDecorator\ServiceDefaultsCallClosureDecorator; |
| 13 | +use Rector\Symfony\NodeAnalyzer\SymfonyPhpClosureDetector; |
| 14 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 15 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 16 | + |
| 17 | +/** |
| 18 | + * @see \Rector\Symfony\Tests\Configs\Rector\Closure\FromServicePublicToDefaultsPublicRector\FromServicePublicToDefaultsPublicRectorTest |
| 19 | + */ |
| 20 | +final class FromServicePublicToDefaultsPublicRector extends AbstractRector |
| 21 | +{ |
| 22 | + public function __construct( |
| 23 | + private readonly SymfonyPhpClosureDetector $symfonyPhpClosureDetector, |
| 24 | + private readonly ServiceDefaultsCallClosureDecorator $serviceDefaultsCallClosureDecorator |
| 25 | + ) { |
| 26 | + } |
| 27 | + |
| 28 | + public function getRuleDefinition(): RuleDefinition |
| 29 | + { |
| 30 | + return new RuleDefinition( |
| 31 | + 'Instead of per service public() call, use it once in defaults()', |
| 32 | + [ |
| 33 | + new CodeSample( |
| 34 | + <<<'CODE_SAMPLE' |
| 35 | +use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; |
| 36 | +
|
| 37 | +return static function (ContainerConfigurator $containerConfigurator): void { |
| 38 | + $services = $containerConfigurator->services(); |
| 39 | +
|
| 40 | + $services->set(SomeCommand::class) |
| 41 | + ->public(); |
| 42 | +
|
| 43 | + $services->set(AnotherCommand::class) |
| 44 | + ->public(); |
| 45 | +
|
| 46 | + $services->set(NextCommand::class) |
| 47 | + ->public(); |
| 48 | +}; |
| 49 | +CODE_SAMPLE |
| 50 | + |
| 51 | + , |
| 52 | + <<<'CODE_SAMPLE' |
| 53 | +use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; |
| 54 | +
|
| 55 | +return static function (ContainerConfigurator $containerConfigurator): void { |
| 56 | + $services = $containerConfigurator->services(); |
| 57 | +
|
| 58 | + $services->defaults()->public(); |
| 59 | +
|
| 60 | + $services->set(SomeCommand::class); |
| 61 | + $services->set(AnotherCommand::class); |
| 62 | + $services->set(NextCommand::class); |
| 63 | +}; |
| 64 | +CODE_SAMPLE |
| 65 | + ), |
| 66 | + |
| 67 | + ] |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @return array<class-string<Node>> |
| 73 | + */ |
| 74 | + public function getNodeTypes(): array |
| 75 | + { |
| 76 | + return [Closure::class]; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @param Closure $node |
| 81 | + */ |
| 82 | + public function refactor(Node $node): ?Node |
| 83 | + { |
| 84 | + if (! $this->symfonyPhpClosureDetector->detect($node)) { |
| 85 | + return null; |
| 86 | + } |
| 87 | + |
| 88 | + $hasDefaultsPublic = $this->symfonyPhpClosureDetector->hasDefaultsConfigured($node, 'public'); |
| 89 | + |
| 90 | + $hasChanged = false; |
| 91 | + |
| 92 | + $this->traverseNodesWithCallable($node->stmts, function (Node $node) use (&$hasChanged): ?Expr { |
| 93 | + if (! $node instanceof MethodCall) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
| 97 | + if (! $this->isName($node->name, 'public') && $node->getArgs() === []) { |
| 98 | + return null; |
| 99 | + } |
| 100 | + |
| 101 | + $hasChanged = true; |
| 102 | + |
| 103 | + return $node->var; |
| 104 | + }); |
| 105 | + |
| 106 | + if ($hasChanged === false) { |
| 107 | + return null; |
| 108 | + } |
| 109 | + |
| 110 | + if ($hasDefaultsPublic === false) { |
| 111 | + $this->serviceDefaultsCallClosureDecorator->decorate($node, 'public'); |
| 112 | + } |
| 113 | + |
| 114 | + return $node; |
| 115 | + } |
| 116 | +} |
0 commit comments