Skip to content

Commit

Permalink
Merge pull request #23 from TomasVotruba/symplify-geter-to-property-r…
Browse files Browse the repository at this point in the history
…ector

Symplify GetterToPropertyRector
  • Loading branch information
Tomáš Votruba authored Sep 3, 2017
2 parents 7082c09 + 3b75286 commit 6196e61
Show file tree
Hide file tree
Showing 16 changed files with 279 additions and 220 deletions.
4 changes: 0 additions & 4 deletions easy-coding-standard.neon
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,9 @@ parameters:
Symplify\CodingStandard\Fixer\Php\ClassStringToClassConstantFixer:
# classes might not exist
- */src/Rector/Contrib/*/*Rector.php
Symplify\CodingStandard\Sniffs\Debug\CommentedOutCodeSniff:
# examples of code to be found
- src/Rector/Contrib/SymfonyExtra/CommandToConstructorInjectionRector.php
SlevomatCodingStandard\Sniffs\Classes\UnusedPrivateElementsSniff:
# will be used soon
- packages/NodeTypeResolver/src/TypeContext.php

PHP_CodeSniffer\Standards\Generic\Sniffs\Files\LineLengthSniff:
# long FQN classes that might not exist
- src/Rector/Contrib/Symfony/FrameworkBundleClassReplacementsRector.php
1 change: 0 additions & 1 deletion src/Builder/Kernel/ServiceFromKernelResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public function resolveServiceClassFromArgument(Arg $argNode, string $kernelClas
return $serviceType;
}


private function resolveServiceClassByNameFromKernel(string $serviceName, string $kernelClass): ?string
{
$container = $this->createContainerFromKernelClass($kernelClass);
Expand Down
45 changes: 45 additions & 0 deletions src/NodeAnalyzer/SymfonyContainerCallsAnalyzer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php declare(strict_types=1);

namespace Rector\NodeAnalyzer;

use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Scalar\String_;

final class SymfonyContainerCallsAnalyzer
{
/**
* Finds $this->get(...);
*/
public function isThisCall(MethodCall $methodCall): bool
{
if ($methodCall->var->name !== 'this' || (string) $methodCall->name !== 'get') {
return false;
}

return $this->hasOneStringArgument($methodCall);
}

/**
* Finds $this->getContainer()->get(...);
*/
public function isGetContainerCall(MethodCall $methodCall): bool
{
if (! $methodCall->var instanceof MethodCall) {
return false;
}

if ((string) $methodCall->var->var->name !== 'this' || (string) $methodCall->name !== 'get') {
return false;
}

return $this->hasOneStringArgument($methodCall);
}

/**
* Finds ('some_service')
*/
private function hasOneStringArgument(MethodCall $methodCall): bool
{
return count($methodCall->args) === 1 && $methodCall->args[0]->value instanceof String_;
}
}
76 changes: 70 additions & 6 deletions src/NodeFactory/NodeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,19 @@

namespace Rector\NodeFactory;

use PhpParser\Node;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\TraitUse;

final class NodeFactory
{
Expand All @@ -12,11 +23,64 @@ final class NodeFactory
*/
public function createLocalPropertyFetch(string $propertyName): PropertyFetch
{
return new PropertyFetch(
new Variable('this', [
'name' => $propertyName,
]),
$propertyName
);
$localVariable = new Variable('this', [
'name' => $propertyName,
]);

return new PropertyFetch($localVariable, $propertyName);
}

public function createNullConstant(): ConstFetch
{
return new ConstFetch(new Name('null'));
}

public function createClassConstant(string $className, string $constantName): ClassConstFetch
{
$classNameNode = new FullyQualified($className);

return new ClassConstFetch($classNameNode, $constantName);
}

public function createClassConstantReference(string $className): ClassConstFetch
{
$nameNode = new FullyQualified($className);

return new ClassConstFetch($nameNode, 'class');
}

public function createMethodCall(string $variableName, string $methodName): MethodCall
{
$varNode = new Variable($variableName);

return new MethodCall($varNode, $methodName);
}

public function createTraitUse(string $traitName): TraitUse
{
$traitNameNode = new FullyQualified($traitName);

return new TraitUse([$traitNameNode]);
}

/**
* @param mixed|Node[] ...$items
*/
public function createArray(...$items): Array_
{
$arrayItems = [];

foreach ($items as $item) {
if ($item instanceof Variable) {
$arrayItems[] = new ArrayItem($item);
} elseif ($item instanceof Identifier) {
$string = new String_((string) $item);
$arrayItems[] = new ArrayItem($string);
}
}

return new Array_($arrayItems, [
'kind' => Array_::KIND_SHORT,
]);
}
}
21 changes: 3 additions & 18 deletions src/Rector/AbstractChangeParentClassRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Rector\Rector;

use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;

abstract class AbstractChangeParentClassRector extends AbstractRector
Expand All @@ -14,35 +14,20 @@ public function isCandidate(Node $node): bool
return false;
}

return $this->getParentClassName($node) === $this->getOldClassName();
return $this->getParentClassName() === $this->getOldClassName();
}

/**
* @param Class_ $node
*/
public function refactor(Node $node): ?Node
{
$node->extends = new Name('\\' . $this->getNewClassName());
$node->extends = new FullyQualified($this->getNewClassName());

return $node;
}

abstract protected function getOldClassName(): string;

abstract protected function getNewClassName(): string;

private function getParentClassName(Class_ $classNode): string
{
if (! $classNode->extends) {
return '';
}

/** @var Name $parentClassName */
$parentClassNameNode = $classNode->extends;

/** @var Node\Name\FullyQualified $fsqName */
$fsqName = $parentClassNameNode->getAttribute('resolvedName');

return $fsqName->toString();
}
}
28 changes: 23 additions & 5 deletions src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ public function beforeTraverse(array $nodes): ?array
return null;
}

protected function getClassName(): string
{
return $this->classNode->namespacedName->toString();
}

/**
* @return null|int|Node
*/
Expand All @@ -54,4 +49,27 @@ public function enterNode(Node $node)

return null;
}

protected function getClassName(): string
{
if ($this->classNode === null) {
return '';
}

return $this->classNode->namespacedName->toString();
}

protected function getParentClassName(): string
{
if ($this->classNode === null) {
return '';
}

$parentClass = $this->classNode->extends;

/** @var Node\Name\FullyQualified $fqnParentClassName */
$fqnParentClassName = $parentClass->getAttribute('resolvedName');

return $fqnParentClassName->toString();
}
}
45 changes: 18 additions & 27 deletions src/Rector/Contrib/Nette/FormCallbackRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,39 @@
namespace Rector\Rector\Contrib\Nette;

use PhpParser\Node;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Scalar\String_;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
use Rector\Contract\Deprecation\DeprecationInterface;
use Rector\Contract\Rector\RectorInterface;
use Rector\Deprecation\SetNames;
use Rector\NodeFactory\NodeFactory;

/**
* Covers https://doc.nette.org/en/2.4/migration-2-4#toc-nette-smartobject.
*/
final class FormCallbackRector extends NodeVisitorAbstract implements DeprecationInterface, RectorInterface
{
/**
* @var string
*/
public const FORM_CLASS = 'Nette\Application\UI\Form';

/**
* @var Node
*/
private $previousNode;

/**
* @var NodeFactory
*/
private $nodeFactory;

public function __construct(NodeFactory $nodeFactory)
{
$this->nodeFactory = $nodeFactory;
}

public function getSetName(): string
{
return SetNames::NETTE;
Expand All @@ -43,7 +56,7 @@ public function enterNode(Node $node)
return null;
}

return $this->createShortArray($node);
return $this->nodeFactory->createArray($node->var, $node->name);
}

$this->previousNode = $node;
Expand Down Expand Up @@ -71,7 +84,7 @@ private function isFormEventAssign(Node $node): bool
return false;
}

if ($node->var->getAttribute('type') !== $this->getDesiredClass()) {
if ($node->var->getAttribute('type') !== self::FORM_CLASS) {
return false;
}

Expand All @@ -82,26 +95,4 @@ private function isFormEventAssign(Node $node): bool

return true;
}

/**
* [$this, 'something']
*/
private function createShortArray(Node $node): Array_
{
return new Array_([
new ArrayItem($node->var),
new ArrayItem(
new String_(
(string) $node->name
)
),
], [
'kind' => Array_::KIND_SHORT,
]);
}

private function getDesiredClass(): string
{
return 'Nette\Application\UI\Form';
}
}
Loading

0 comments on commit 6196e61

Please sign in to comment.