Skip to content

Commit

Permalink
[DX] Decouple AssignVariableNameResolver (#1427)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Dec 8, 2021
1 parent ee3ca5a commit 2800a83
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 38 deletions.
2 changes: 2 additions & 0 deletions easy-ci.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,6 +65,7 @@
OutputStyleInterface::class,
FileFormatterInterface::class,
MethodCallManipulator::class,
AssignVariableNameResolverInterface::class,
// fix later - rector-symfony
PropertyConstructorInjectionManipulator::class,
// used in tests
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Rector\Naming\AssignVariableNameResolver;

use PhpParser\Node;
use PhpParser\Node\Expr\New_;
use Rector\Core\Exception\NotImplementedYetException;
use Rector\Naming\Contract\AssignVariableNameResolverInterface;
use Rector\NodeNameResolver\NodeNameResolver;

/**
* @implements AssignVariableNameResolverInterface<New_>
*/
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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Rector\Naming\AssignVariableNameResolver;

use PhpParser\Node;
use PhpParser\Node\Expr\PropertyFetch;
use Rector\Core\Exception\NotImplementedYetException;
use Rector\Naming\Contract\AssignVariableNameResolverInterface;
use Rector\NodeNameResolver\NodeNameResolver;

/**
* @implements AssignVariableNameResolverInterface<PropertyFetch>
*/
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);
}
}
20 changes: 20 additions & 0 deletions rules/Naming/Contract/AssignVariableNameResolverInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Rector\Naming\Contract;

use PhpParser\Node;

/**
* @template TNode as Node
*/
interface AssignVariableNameResolverInterface
{
public function match(Node $node): bool;

/**
* @param TNode $node
*/
public function resolve(Node $node): string;
}
46 changes: 9 additions & 37 deletions rules/Naming/Naming/VariableNaming.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
use PhpParser\Node\Expr\Cast;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\NullsafeMethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Name;
Expand All @@ -22,15 +20,20 @@
use PHPStan\Type\ThisType;
use PHPStan\Type\Type;
use Rector\Core\Exception\NotImplementedYetException;
use Rector\Naming\Contract\AssignVariableNameResolverInterface;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Symfony\Component\String\UnicodeString;

final class VariableNaming
{
/**
* @param AssignVariableNameResolverInterface[] $assignVariableNameResolvers
*/
public function __construct(
private readonly NodeNameResolver $nodeNameResolver,
private readonly NodeTypeResolver $nodeTypeResolver,
private readonly array $assignVariableNameResolvers
) {
}

Expand Down Expand Up @@ -114,18 +117,16 @@ private function resolveBareFromNode(Node $node): ?string
{
$node = $this->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);
}
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion rules/Privatization/Naming/ConstantNaming.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 2800a83

Please sign in to comment.