From 6b52da24d27aeefec12a17b3232d237df13d9d7b Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 8 Dec 2021 08:56:29 +0300 Subject: [PATCH 1/2] replacing with Symfony\String --- composer.json | 5 +-- phpstan.neon | 6 +++ rules/CodingStyle/Naming/ClassNaming.php | 44 ------------------- rules/Naming/Naming/VariableNaming.php | 39 ++++++++-------- rules/Privatization/Naming/ConstantNaming.php | 28 +++++++++--- ...riableWithDefaultValueToConstantRector.php | 22 +++------- 6 files changed, 58 insertions(+), 86 deletions(-) diff --git a/composer.json b/composer.json index 29f2cd8943a..2bb50057d9c 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,6 @@ "clue/ndjson-react": "^1.2", "composer/semver": "^3.2", "composer/xdebug-handler": "^2.0", - "danielstjules/stringy": "^3.1", "doctrine/inflector": "^2.0", "ergebnis/json-printer": "^3.1", "idiosyncratic/editorconfig": "^0.1.3", @@ -44,6 +43,7 @@ "symfony/deprecation-contracts": "^2.4", "symfony/finder": "^5.4|^6.0", "symfony/process": "^5.4|^6.0", + "symfony/string": "^5.4|^6.0", "symfony/yaml": "^5.4|^6.0", "symplify/astral": "^10.0-beta17", "symplify/autowire-array-parameter": "^10.0-beta17", @@ -152,9 +152,6 @@ }, "extra": { "patches": { - "danielstjules/stringy": [ - "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/danielstjules-stringy-src-stringy-php.patch" - ], "helmich/typo3-typoscript-parser": [ "https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/helmich-typo3-typoscript-parser-parser-tokenstream-php.patch" ], diff --git a/phpstan.neon b/phpstan.neon index 1d56842c77a..607c7734cae 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -615,3 +615,9 @@ parameters: - rules/Arguments/Rector/ClassMethod/ArgumentAdderRector.php - rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeDeclarationRector.php - rules/Transform/Rector/Assign/PropertyFetchToMethodCallRector.php + + # fixed in symplify dev-main + - + message: '#Do not use chained method calls\. Put each on separated lines#' + paths: + - rules/Privatization/Naming/ConstantNaming.php diff --git a/rules/CodingStyle/Naming/ClassNaming.php b/rules/CodingStyle/Naming/ClassNaming.php index 834761d7a9f..be318cc8918 100644 --- a/rules/CodingStyle/Naming/ClassNaming.php +++ b/rules/CodingStyle/Naming/ClassNaming.php @@ -8,19 +8,9 @@ use PhpParser\Node\Identifier; use PhpParser\Node\Name; use PhpParser\Node\Stmt\ClassLike; -use PhpParser\Node\Stmt\Function_; -use Rector\Testing\PHPUnit\StaticPHPUnitEnvironment; -use Stringy\Stringy; -use Symplify\SmartFileSystem\SmartFileInfo; final class ClassNaming { - /** - * @see https://regex101.com/r/8BdrI3/1 - * @var string - */ - private const INPUT_HASH_NAMING_REGEX = '#input_(.*?)_#'; - public function getVariableName(string | Name | Identifier $name): string { $shortName = $this->getShortName($name); @@ -57,38 +47,4 @@ public function getNamespace(string $fullyQualifiedName): ?string return Strings::before($fullyQualifiedName, '\\', -1); } - - public function getNameFromFileInfo(SmartFileInfo $smartFileInfo): string - { - $basenameWithoutSuffix = $smartFileInfo->getBasenameWithoutSuffix(); - - // remove PHPUnit fixture file prefix - if (StaticPHPUnitEnvironment::isPHPUnitRun()) { - $basenameWithoutSuffix = Strings::replace($basenameWithoutSuffix, self::INPUT_HASH_NAMING_REGEX, ''); - } - - $stringy = new Stringy($basenameWithoutSuffix); - return (string) $stringy->upperCamelize(); - } - - /** - * "some_function" → "someFunction" - */ - public function createMethodNameFromFunction(Function_ $function): string - { - $functionName = (string) $function->name; - - $stringy = new Stringy($functionName); - return (string) $stringy->camelize(); - } - - public function replaceSuffix(string $content, string $oldSuffix, string $newSuffix): string - { - if (! \str_ends_with($content, $oldSuffix)) { - return $content . $newSuffix; - } - - $contentWithoutOldSuffix = Strings::substring($content, 0, -Strings::length($oldSuffix)); - return $contentWithoutOldSuffix . $newSuffix; - } } diff --git a/rules/Naming/Naming/VariableNaming.php b/rules/Naming/Naming/VariableNaming.php index 30a59cc35b0..238cffb60c5 100644 --- a/rules/Naming/Naming/VariableNaming.php +++ b/rules/Naming/Naming/VariableNaming.php @@ -28,6 +28,7 @@ use Rector\NodeNameResolver\NodeNameResolver; use Rector\NodeTypeResolver\NodeTypeResolver; use Stringy\Stringy; +use Symfony\Component\String\UnicodeString; final class VariableNaming { @@ -38,23 +39,6 @@ public function __construct( ) { } - public function resolveFromNodeAndType(Node $node, Type $type): ?string - { - $variableName = $this->resolveBareFromNode($node); - if ($variableName === null) { - return null; - } - - // adjust static to specific class - if ($variableName === 'this' && $type instanceof ThisType) { - $shortClassName = $this->nodeNameResolver->getShortName($type->getClassName()); - $variableName = lcfirst($shortClassName); - } - - $stringy = new Stringy($variableName); - return (string) $stringy->camelize(); - } - public function resolveFromNodeWithScopeCountAndFallbackName( Expr $expr, ?Scope $scope, @@ -105,7 +89,26 @@ public function resolveFromFuncCallFirstArgumentWithSuffix( return $this->createCountedValueName($bareName, $scope); } - public function resolveFromNode(Node $node): ?string + private function resolveFromNodeAndType(Node $node, Type $type): ?string + { + $variableName = $this->resolveBareFromNode($node); + if ($variableName === null) { + return null; + } + + // adjust static to specific class + if ($variableName === 'this' && $type instanceof ThisType) { + $shortClassName = $this->nodeNameResolver->getShortName($type->getClassName()); + $variableName = lcfirst($shortClassName); + } else { + $variableName = $this->nodeNameResolver->getShortName($variableName); + } + $variableNameString = new UnicodeString($variableName); + return $variableNameString->camel() + ->toString(); + } + + private function resolveFromNode(Node $node): ?string { $nodeType = $this->nodeTypeResolver->getType($node); return $this->resolveFromNodeAndType($node, $nodeType); diff --git a/rules/Privatization/Naming/ConstantNaming.php b/rules/Privatization/Naming/ConstantNaming.php index 642cd5ef093..c340dd5c171 100644 --- a/rules/Privatization/Naming/ConstantNaming.php +++ b/rules/Privatization/Naming/ConstantNaming.php @@ -4,9 +4,10 @@ namespace Rector\Privatization\Naming; +use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt\PropertyProperty; use Rector\NodeNameResolver\NodeNameResolver; -use Stringy\Stringy; +use Symfony\Component\String\UnicodeString; final class ConstantNaming { @@ -15,12 +16,29 @@ public function __construct( ) { } - public function createFromProperty(PropertyProperty $propertyProperty): string + public function createFromProperty(PropertyProperty|Variable $propertyProperty): string { + /** @var string $propertyName */ $propertyName = $this->nodeNameResolver->getName($propertyProperty); + return $this->createUnderscoreUppercaseString($propertyName); + } + + public function createFromVariable(Variable $variable): string|null + { + $variableName = $this->nodeNameResolver->getName($variable); + if ($variableName === null) { + return null; + } + + return $this->createUnderscoreUppercaseString($variableName); + } + + private function createUnderscoreUppercaseString(string $propertyName): string + { + $propertyNameString = new UnicodeString($propertyName); - $stringy = new Stringy($propertyName); - return (string) $stringy->underscored() - ->toUpperCase(); + return $propertyNameString->snake() + ->upper() + ->toString(); } } diff --git a/rules/Privatization/Rector/Class_/ChangeReadOnlyVariableWithDefaultValueToConstantRector.php b/rules/Privatization/Rector/Class_/ChangeReadOnlyVariableWithDefaultValueToConstantRector.php index d587a11cbb0..25670ed1d20 100644 --- a/rules/Privatization/Rector/Class_/ChangeReadOnlyVariableWithDefaultValueToConstantRector.php +++ b/rules/Privatization/Rector/Class_/ChangeReadOnlyVariableWithDefaultValueToConstantRector.php @@ -20,7 +20,7 @@ use Rector\Core\NodeManipulator\ClassMethodAssignManipulator; use Rector\Core\Rector\AbstractRector; use Rector\PostRector\Collector\PropertyToAddCollector; -use Stringy\Stringy; +use Rector\Privatization\Naming\ConstantNaming; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -32,7 +32,8 @@ final class ChangeReadOnlyVariableWithDefaultValueToConstantRector extends Abstr public function __construct( private readonly ClassMethodAssignManipulator $classMethodAssignManipulator, private readonly VarAnnotationManipulator $varAnnotationManipulator, - private readonly PropertyToAddCollector $propertyToAddCollector + private readonly PropertyToAddCollector $propertyToAddCollector, + private readonly ConstantNaming $constantNaming, ) { } @@ -225,7 +226,10 @@ private function isFoundByRefParam(ClassMethod $classMethod): bool private function createPrivateClassConst(Variable $variable, Expr $expr): ClassConst { - $constantName = $this->createConstantNameFromVariable($variable); + $constantName = $this->constantNaming->createFromVariable($variable); + if ($constantName === null) { + throw new ShouldNotHappenException(); + } $const = new Const_($constantName, $expr); @@ -266,16 +270,4 @@ private function replaceVariableWithClassConstFetch( return new ClassConstFetch(new Name('self'), new Identifier($constantName)); }); } - - private function createConstantNameFromVariable(Variable $variable): string - { - $variableName = $this->getName($variable); - if ($variableName === null) { - throw new ShouldNotHappenException(); - } - - $stringy = new Stringy($variableName); - return (string) $stringy->underscored() - ->toUpperCase(); - } } From ab0148bcb9864b009dbc5b56f68c9ef847460be6 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 8 Dec 2021 09:09:15 +0300 Subject: [PATCH 2/2] try removing dim fetch rename --- composer.json | 2 +- phpstan.neon | 1 + rules/Naming/Naming/VariableNaming.php | 35 +++---------------- rules/Privatization/Naming/ConstantNaming.php | 4 +-- 4 files changed, 8 insertions(+), 34 deletions(-) diff --git a/composer.json b/composer.json index 2bb50057d9c..6553d31139e 100644 --- a/composer.json +++ b/composer.json @@ -69,7 +69,7 @@ "phpstan/phpstan-webmozart-assert": "^1.0", "phpunit/phpunit": "^9.5", "rector/phpstan-rules": "^0.4.15", - "rector/rector-generator": "^0.4.4", + "rector/rector-generator": "^0.4.7", "spatie/enum": "^3.10", "symplify/coding-standard": "^10.0", "symplify/easy-ci": "^10.0", diff --git a/phpstan.neon b/phpstan.neon index 607c7734cae..f303d5574a8 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -621,3 +621,4 @@ parameters: message: '#Do not use chained method calls\. Put each on separated lines#' paths: - rules/Privatization/Naming/ConstantNaming.php + - rules/Naming/Naming/VariableNaming.php diff --git a/rules/Naming/Naming/VariableNaming.php b/rules/Naming/Naming/VariableNaming.php index 238cffb60c5..0646d98c543 100644 --- a/rules/Naming/Naming/VariableNaming.php +++ b/rules/Naming/Naming/VariableNaming.php @@ -8,7 +8,6 @@ use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr; -use PhpParser\Node\Expr\ArrayDimFetch; use PhpParser\Node\Expr\Cast; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\MethodCall; @@ -18,23 +17,19 @@ use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Expr\Ternary; use PhpParser\Node\Name; -use PhpParser\Node\Scalar; use PhpParser\Node\Scalar\String_; use PHPStan\Analyser\Scope; use PHPStan\Type\ThisType; use PHPStan\Type\Type; use Rector\Core\Exception\NotImplementedYetException; -use Rector\Core\PhpParser\Node\Value\ValueResolver; use Rector\NodeNameResolver\NodeNameResolver; use Rector\NodeTypeResolver\NodeTypeResolver; -use Stringy\Stringy; use Symfony\Component\String\UnicodeString; final class VariableNaming { public function __construct( private readonly NodeNameResolver $nodeNameResolver, - private readonly ValueResolver $valueResolver, private readonly NodeTypeResolver $nodeTypeResolver, ) { } @@ -89,7 +84,7 @@ public function resolveFromFuncCallFirstArgumentWithSuffix( return $this->createCountedValueName($bareName, $scope); } - private function resolveFromNodeAndType(Node $node, Type $type): ?string + public function resolveFromNodeAndType(Node $node, Type $type): ?string { $variableName = $this->resolveBareFromNode($node); if ($variableName === null) { @@ -103,8 +98,9 @@ private function resolveFromNodeAndType(Node $node, Type $type): ?string } else { $variableName = $this->nodeNameResolver->getShortName($variableName); } - $variableNameString = new UnicodeString($variableName); - return $variableNameString->camel() + + $variableNameUnicodeString = new UnicodeString($variableName); + return $variableNameUnicodeString->camel() ->toString(); } @@ -118,10 +114,6 @@ private function resolveBareFromNode(Node $node): ?string { $node = $this->unwrapNode($node); - if ($node instanceof ArrayDimFetch) { - return $this->resolveParamNameFromArrayDimFetch($node); - } - if ($node instanceof PropertyFetch) { return $this->resolveFromPropertyFetch($node); } @@ -154,25 +146,6 @@ private function resolveBareFromNode(Node $node): ?string return null; } - private function resolveParamNameFromArrayDimFetch(ArrayDimFetch $arrayDimFetch): ?string - { - while ($arrayDimFetch instanceof ArrayDimFetch) { - if ($arrayDimFetch->dim instanceof Scalar) { - $valueName = $this->nodeNameResolver->getName($arrayDimFetch->var); - $dimName = $this->valueResolver->getValue($arrayDimFetch->dim); - - $stringy = new Stringy($dimName); - $dimName = (string) $stringy->upperCamelize(); - - return $valueName . $dimName; - } - - $arrayDimFetch = $arrayDimFetch->var; - } - - return $this->resolveBareFromNode($arrayDimFetch); - } - private function resolveFromPropertyFetch(PropertyFetch $propertyFetch): string { $varName = $this->nodeNameResolver->getName($propertyFetch->var); diff --git a/rules/Privatization/Naming/ConstantNaming.php b/rules/Privatization/Naming/ConstantNaming.php index c340dd5c171..f3c7745ebfd 100644 --- a/rules/Privatization/Naming/ConstantNaming.php +++ b/rules/Privatization/Naming/ConstantNaming.php @@ -35,9 +35,9 @@ public function createFromVariable(Variable $variable): string|null private function createUnderscoreUppercaseString(string $propertyName): string { - $propertyNameString = new UnicodeString($propertyName); + $propertyNameUnicodeString = new UnicodeString($propertyName); - return $propertyNameString->snake() + return $propertyNameUnicodeString->snake() ->upper() ->toString(); }