savinmikhail /
AddNamedArgumentsRector
| 1 | <?php |
||||
| 2 | |||||
| 3 | declare(strict_types=1); |
||||
| 4 | |||||
| 5 | namespace SavinMikhail\AddNamedArgumentsRector; |
||||
| 6 | |||||
| 7 | use InvalidArgumentException; |
||||
| 8 | use PhpParser\ConstExprEvaluator; |
||||
| 9 | use PhpParser\Node; |
||||
| 10 | use PhpParser\Node\Arg; |
||||
| 11 | use PhpParser\Node\Expr\FuncCall; |
||||
| 12 | use PhpParser\Node\Expr\MethodCall; |
||||
| 13 | use PhpParser\Node\Expr\New_; |
||||
| 14 | use PhpParser\Node\Expr\StaticCall; |
||||
| 15 | use PhpParser\Node\Identifier; |
||||
| 16 | use PHPStan\Reflection\ExtendedParameterReflection; |
||||
|
0 ignored issues
–
show
|
|||||
| 17 | use PHPStan\Reflection\ReflectionProvider; |
||||
|
0 ignored issues
–
show
The type
PHPStan\Reflection\ReflectionProvider was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||
| 18 | use PHPStan\Type\ConstantScalarType; |
||||
|
0 ignored issues
–
show
The type
PHPStan\Type\ConstantScalarType was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||
| 19 | use Rector\Contract\Rector\ConfigurableRectorInterface; |
||||
| 20 | use Rector\NodeNameResolver\NodeNameResolver; |
||||
| 21 | use Rector\NodeTypeResolver\NodeTypeResolver; |
||||
| 22 | use Rector\Rector\AbstractRector; |
||||
| 23 | use Rector\ValueObject\PhpVersion; |
||||
| 24 | use Rector\VersionBonding\Contract\MinPhpVersionInterface; |
||||
| 25 | use RuntimeException; |
||||
| 26 | use SavinMikhail\AddNamedArgumentsRector\Config\ConfigStrategy; |
||||
| 27 | use SavinMikhail\AddNamedArgumentsRector\Config\DefaultStrategy; |
||||
|
0 ignored issues
–
show
The type
SavinMikhail\AddNamedArg...\Config\DefaultStrategy was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||
| 28 | use SavinMikhail\AddNamedArgumentsRector\Reflection\Reflection; |
||||
|
0 ignored issues
–
show
The type
SavinMikhail\AddNamedArg...r\Reflection\Reflection was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||
| 29 | use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
||||
| 30 | use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
||||
| 31 | use Throwable; |
||||
| 32 | use Webmozart\Assert\Assert; |
||||
| 33 | |||||
| 34 | use function constant; |
||||
| 35 | use function count; |
||||
| 36 | use function defined; |
||||
| 37 | |||||
| 38 | /** |
||||
| 39 | * @see AddNamedArgumentsRectorTest |
||||
| 40 | */ |
||||
| 41 | final class AddNamedArgumentsRector extends AbstractRector implements MinPhpVersionInterface, ConfigurableRectorInterface |
||||
| 42 | { |
||||
| 43 | private string $configStrategy = DefaultStrategy::class; |
||||
| 44 | |||||
| 45 | private readonly Reflection $reflectionService; |
||||
| 46 | |||||
| 47 | private readonly ConstExprEvaluator $constExprEvaluator; |
||||
| 48 | |||||
| 49 | 1 | public function __construct( |
|||
| 50 | ReflectionProvider $reflectionProvider, |
||||
| 51 | NodeNameResolver $nodeNameResolver, |
||||
| 52 | NodeTypeResolver $nodeTypeResolver, |
||||
| 53 | ?Reflection $reflectionService = null, |
||||
| 54 | ?ConstExprEvaluator $constExprEvaluator = null, |
||||
| 55 | ) { |
||||
| 56 | 1 | if ($reflectionService === null) { |
|||
| 57 | $reflectionService = new Reflection( |
||||
| 58 | reflectionProvider: $reflectionProvider, |
||||
| 59 | nodeNameResolver: $nodeNameResolver, |
||||
| 60 | nodeTypeResolver: $nodeTypeResolver, |
||||
| 61 | ); |
||||
| 62 | } |
||||
| 63 | 1 | $this->reflectionService = $reflectionService; |
|||
|
0 ignored issues
–
show
|
|||||
| 64 | 1 | $this->constExprEvaluator = $constExprEvaluator ?? new ConstExprEvaluator(static function (string $name) { |
|||
|
0 ignored issues
–
show
|
|||||
| 65 | if (defined($name)) { |
||||
| 66 | return constant($name); |
||||
| 67 | } |
||||
| 68 | |||||
| 69 | throw new RuntimeException("Undefined constant: {$name}"); |
||||
| 70 | 1 | }); |
|||
| 71 | } |
||||
| 72 | |||||
| 73 | public function getRuleDefinition(): RuleDefinition |
||||
| 74 | { |
||||
| 75 | return new RuleDefinition('Convert all arguments to named arguments', codeSamples: [ |
||||
| 76 | new CodeSample( |
||||
| 77 | badCode: '$user->setPassword("123456");', |
||||
| 78 | goodCode: '$user->changePassword(password: "123456");', |
||||
| 79 | ), |
||||
| 80 | ]); |
||||
| 81 | } |
||||
| 82 | |||||
| 83 | 13 | public function getNodeTypes(): array |
|||
| 84 | { |
||||
| 85 | 13 | return [FuncCall::class, StaticCall::class, MethodCall::class, New_::class]; |
|||
| 86 | } |
||||
| 87 | |||||
| 88 | 13 | public function refactor(Node $node): ?Node |
|||
| 89 | { |
||||
| 90 | /** @var FuncCall|StaticCall|MethodCall|New_ $node */ |
||||
| 91 | 13 | $parameters = $this->reflectionService->getParameters(node: $node); |
|||
| 92 | 13 | $classReflection = $this->reflectionService->getClassReflection(node: $node); |
|||
| 93 | |||||
| 94 | 13 | if (!$this->configStrategy::shouldApply($node, $parameters, $classReflection)) { |
|||
| 95 | 6 | return null; |
|||
| 96 | } |
||||
| 97 | |||||
| 98 | 8 | $this->addNamesToArgs(node: $node, parameters: $parameters); |
|||
| 99 | |||||
| 100 | 8 | return $node; |
|||
| 101 | } |
||||
| 102 | |||||
| 103 | /** |
||||
| 104 | * @param ExtendedParameterReflection[] $parameters |
||||
| 105 | */ |
||||
| 106 | 8 | private function addNamesToArgs( |
|||
| 107 | FuncCall|StaticCall|MethodCall|New_ $node, |
||||
| 108 | array $parameters, |
||||
| 109 | ): void { |
||||
| 110 | 8 | $namedArgs = []; |
|||
| 111 | 8 | foreach ($node->args as $index => $arg) { |
|||
| 112 | 8 | $parameter = $parameters[$index] ?? null; |
|||
| 113 | 8 | if ($parameter === null) { |
|||
| 114 | $namedArgs[] = $arg; |
||||
| 115 | |||||
| 116 | continue; |
||||
| 117 | } |
||||
| 118 | |||||
| 119 | 8 | if ($arg->name !== null) { |
|||
|
0 ignored issues
–
show
|
|||||
| 120 | 2 | $namedArgs[] = $arg; |
|||
| 121 | |||||
| 122 | 2 | continue; |
|||
| 123 | } |
||||
| 124 | |||||
| 125 | 7 | if ($this->shouldSkipArg($arg, $parameter)) { |
|||
|
0 ignored issues
–
show
It seems like
$arg can also be of type PhpParser\Node\VariadicPlaceholder; however, parameter $arg of SavinMikhail\AddNamedArg...Rector::shouldSkipArg() does only seem to accept PhpParser\Node\Arg, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 126 | 1 | continue; |
|||
| 127 | } |
||||
| 128 | |||||
| 129 | 7 | $arg->name = new Identifier(name: $parameter->getName()); |
|||
| 130 | 7 | $namedArgs[] = $arg; |
|||
| 131 | } |
||||
| 132 | |||||
| 133 | 8 | $node->args = $namedArgs; |
|||
| 134 | } |
||||
| 135 | |||||
| 136 | 7 | private function shouldSkipArg(Arg $arg, ExtendedParameterReflection $parameter): bool |
|||
| 137 | { |
||||
| 138 | try { |
||||
| 139 | 7 | $defaultValue = $parameter->getDefaultValue(); |
|||
| 140 | } catch (Throwable) { |
||||
| 141 | return false; |
||||
| 142 | } |
||||
| 143 | |||||
| 144 | try { |
||||
| 145 | 7 | $argValue = $this->constExprEvaluator->evaluateDirectly($arg->value); |
|||
| 146 | 2 | } catch (Throwable) { |
|||
| 147 | 2 | return false; |
|||
| 148 | } |
||||
| 149 | |||||
| 150 | 7 | if ($defaultValue instanceof ConstantScalarType) { |
|||
| 151 | 2 | $defaultValue = $defaultValue->getValue(); |
|||
| 152 | } |
||||
| 153 | |||||
| 154 | 7 | return $argValue === $defaultValue; |
|||
| 155 | } |
||||
| 156 | |||||
| 157 | 1 | public function provideMinPhpVersion(): int |
|||
| 158 | { |
||||
| 159 | 1 | return PhpVersion::PHP_80; |
|||
| 160 | } |
||||
| 161 | |||||
| 162 | public function configure(array $configuration): void |
||||
| 163 | { |
||||
| 164 | Assert::lessThan(value: count(value: $configuration), limit: 2, message: 'You can pass only 1 strategy'); |
||||
| 165 | if ($configuration === []) { |
||||
| 166 | return; |
||||
| 167 | } |
||||
| 168 | $strategyClass = $configuration[0]; |
||||
| 169 | |||||
| 170 | if (!class_exists(class: $strategyClass)) { |
||||
| 171 | throw new InvalidArgumentException(message: "Class {$strategyClass} does not exist."); |
||||
| 172 | } |
||||
| 173 | |||||
| 174 | $strategy = new $strategyClass(); |
||||
| 175 | |||||
| 176 | Assert::isInstanceOf(value: $strategy, class: ConfigStrategy::class, message: 'Your strategy must implement ConfigStrategy interface'); |
||||
| 177 | |||||
| 178 | $this->configStrategy = $strategyClass; |
||||
| 179 | } |
||||
| 180 | } |
||||
| 181 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths