Skip to content

Commit

Permalink
[DX] Replace deprecated danielstjules/stringy with Symfony\String (#1425
Browse files Browse the repository at this point in the history
)

* replacing with Symfony\String

* try removing dim fetch rename
  • Loading branch information
TomasVotruba committed Dec 8, 2021
1 parent b2947fd commit 7053e9a
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 115 deletions.
7 changes: 2 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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"
],
Expand Down
7 changes: 7 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -615,3 +615,10 @@ 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
- rules/Naming/Naming/VariableNaming.php
44 changes: 0 additions & 44 deletions rules/CodingStyle/Naming/ClassNaming.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}
68 changes: 22 additions & 46 deletions rules/Naming/Naming/VariableNaming.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,43 +17,23 @@
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,
) {
}

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,
Expand Down Expand Up @@ -105,7 +84,27 @@ public function resolveFromFuncCallFirstArgumentWithSuffix(
return $this->createCountedValueName($bareName, $scope);
}

public function resolveFromNode(Node $node): ?string
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);
} else {
$variableName = $this->nodeNameResolver->getShortName($variableName);
}

$variableNameUnicodeString = new UnicodeString($variableName);
return $variableNameUnicodeString->camel()
->toString();
}

private function resolveFromNode(Node $node): ?string
{
$nodeType = $this->nodeTypeResolver->getType($node);
return $this->resolveFromNodeAndType($node, $nodeType);
Expand All @@ -115,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);
}
Expand Down Expand Up @@ -151,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);
Expand Down
28 changes: 23 additions & 5 deletions rules/Privatization/Naming/ConstantNaming.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
{
$propertyNameUnicodeString = new UnicodeString($propertyName);

$stringy = new Stringy($propertyName);
return (string) $stringy->underscored()
->toUpperCase();
return $propertyNameUnicodeString->snake()
->upper()
->toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
) {
}

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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();
}
}

0 comments on commit 7053e9a

Please sign in to comment.