diff --git a/easy-ci.php b/easy-ci.php index 193d92d5a63..69f713ffe7a 100644 --- a/easy-ci.php +++ b/easy-ci.php @@ -13,6 +13,7 @@ use Rector\DependencyInjection\NodeManipulator\PropertyConstructorInjectionManipulator; use Rector\FileFormatter\Contract\Formatter\FileFormatterInterface; use Rector\FileSystemRector\Parser\FileInfoParser; +use Rector\Naming\Contract\AssignVariableNameResolverInterface; use Rector\Naming\Contract\Guard\ConflictingNameGuardInterface; use Rector\NodeNameResolver\Contract\NodeNameResolverInterface; use Rector\NodeTypeResolver\Contract\NodeTypeResolverInterface; @@ -64,6 +65,7 @@ OutputStyleInterface::class, FileFormatterInterface::class, MethodCallManipulator::class, + AssignVariableNameResolverInterface::class, // fix later - rector-symfony PropertyConstructorInjectionManipulator::class, // used in tests diff --git a/rules/Naming/AssignVariableNameResolver/NewAssignVariableNameResolver.php b/rules/Naming/AssignVariableNameResolver/NewAssignVariableNameResolver.php new file mode 100644 index 00000000000..270febf80c0 --- /dev/null +++ b/rules/Naming/AssignVariableNameResolver/NewAssignVariableNameResolver.php @@ -0,0 +1,40 @@ + + */ +final class NewAssignVariableNameResolver implements AssignVariableNameResolverInterface +{ + public function __construct( + private readonly NodeNameResolver $nodeNameResolver + ) { + } + + public function match(Node $node): bool + { + return $node instanceof New_; + } + + /** + * @param New_ $node + */ + public function resolve(Node $node): string + { + $className = $this->nodeNameResolver->getName($node->class); + if ($className === null) { + throw new NotImplementedYetException(); + } + + return $this->nodeNameResolver->getShortName($className); + } +} diff --git a/rules/Naming/AssignVariableNameResolver/PropertyFetchAssignVariableNameResolver.php b/rules/Naming/AssignVariableNameResolver/PropertyFetchAssignVariableNameResolver.php new file mode 100644 index 00000000000..cdd95ac95ec --- /dev/null +++ b/rules/Naming/AssignVariableNameResolver/PropertyFetchAssignVariableNameResolver.php @@ -0,0 +1,49 @@ + + */ +final class PropertyFetchAssignVariableNameResolver implements AssignVariableNameResolverInterface +{ + public function __construct( + private readonly NodeNameResolver $nodeNameResolver + ) { + } + + public function match(Node $node): bool + { + return $node instanceof PropertyFetch; + } + + /** + * @param PropertyFetch $node + */ + public function resolve(Node $node): string + { + $varName = $this->nodeNameResolver->getName($node->var); + if (! is_string($varName)) { + throw new NotImplementedYetException(); + } + + $propertyName = $this->nodeNameResolver->getName($node->name); + if (! is_string($propertyName)) { + throw new NotImplementedYetException(); + } + + if ($varName === 'this') { + return $propertyName; + } + + return $varName . ucfirst($propertyName); + } +} diff --git a/rules/Naming/Contract/AssignVariableNameResolverInterface.php b/rules/Naming/Contract/AssignVariableNameResolverInterface.php new file mode 100644 index 00000000000..bc96ce8bd8d --- /dev/null +++ b/rules/Naming/Contract/AssignVariableNameResolverInterface.php @@ -0,0 +1,20 @@ +unwrapNode($node); - if ($node instanceof PropertyFetch) { - return $this->resolveFromPropertyFetch($node); + foreach ($this->assignVariableNameResolvers as $assignVariableNameResolver) { + if ($assignVariableNameResolver->match($node)) { + return $assignVariableNameResolver->resolve($node); + } } if ($node !== null && ($node instanceof MethodCall || $node instanceof NullsafeMethodCall || $node instanceof StaticCall)) { return $this->resolveFromMethodCall($node); } - if ($node instanceof New_) { - return $this->resolveFromNew($node); - } - if ($node instanceof FuncCall) { return $this->resolveFromNode($node->name); } @@ -146,25 +147,6 @@ private function resolveBareFromNode(Node $node): ?string return null; } - private function resolveFromPropertyFetch(PropertyFetch $propertyFetch): string - { - $varName = $this->nodeNameResolver->getName($propertyFetch->var); - if (! is_string($varName)) { - throw new NotImplementedYetException(); - } - - $propertyName = $this->nodeNameResolver->getName($propertyFetch->name); - if (! is_string($propertyName)) { - throw new NotImplementedYetException(); - } - - if ($varName === 'this') { - return $propertyName; - } - - return $varName . ucfirst($propertyName); - } - private function resolveFromMethodCall(MethodCall | NullsafeMethodCall | StaticCall $node): ?string { if ($node->name instanceof MethodCall) { @@ -179,16 +161,6 @@ private function resolveFromMethodCall(MethodCall | NullsafeMethodCall | StaticC return $methodName; } - private function resolveFromNew(New_ $new): string - { - $className = $this->nodeNameResolver->getName($new->class); - if ($className === null) { - throw new NotImplementedYetException(); - } - - return $this->nodeNameResolver->getShortName($className); - } - private function unwrapNode(Node $node): ?Node { if ($node instanceof Arg) { diff --git a/rules/Privatization/Naming/ConstantNaming.php b/rules/Privatization/Naming/ConstantNaming.php index f3c7745ebfd..9dbf8cd9466 100644 --- a/rules/Privatization/Naming/ConstantNaming.php +++ b/rules/Privatization/Naming/ConstantNaming.php @@ -36,7 +36,6 @@ public function createFromVariable(Variable $variable): string|null private function createUnderscoreUppercaseString(string $propertyName): string { $propertyNameUnicodeString = new UnicodeString($propertyName); - return $propertyNameUnicodeString->snake() ->upper() ->toString();