Skip to content

Commit

Permalink
add CallableNodeTraverserTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jun 4, 2019
1 parent bdb5c39 commit 61a2b35
Show file tree
Hide file tree
Showing 16 changed files with 56 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockManipulator;
use Rector\NodeTypeResolver\PHPStan\Type\TypeToStringResolver;
use Rector\PhpParser\NodeTraverser\CallableNodeTraverser;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
Expand All @@ -23,11 +22,6 @@
*/
final class CompleteDynamicPropertiesRector extends AbstractRector
{
/**
* @var CallableNodeTraverser
*/
private $callableNodeTraverser;

/**
* @var TypeToStringResolver
*/
Expand All @@ -39,11 +33,9 @@ final class CompleteDynamicPropertiesRector extends AbstractRector
private $docBlockManipulator;

public function __construct(
CallableNodeTraverser $callableNodeTraverser,
TypeToStringResolver $typeToStringResolver,
DocBlockManipulator $docBlockManipulator
) {
$this->callableNodeTraverser = $callableNodeTraverser;
$this->typeToStringResolver = $typeToStringResolver;
$this->docBlockManipulator = $docBlockManipulator;
}
Expand Down Expand Up @@ -99,9 +91,7 @@ public function refactor(Node $node): ?Node
$fetchedLocalPropertyNameToTypes = $this->resolveFetchedLocalPropertyNameToTypes($node);

$propertyNames = [];
$this->callableNodeTraverser->traverseNodesWithCallable($node->stmts, function (Node $node) use (
&$propertyNames
) {
$this->traverseNodesWithCallable($node->stmts, function (Node $node) use (&$propertyNames) {
if (! $node instanceof Property) {
return null;
}
Expand Down Expand Up @@ -169,7 +159,7 @@ private function resolveFetchedLocalPropertyNameToTypes(Class_ $class): array
{
$fetchedLocalPropertyNameToTypes = [];

$this->callableNodeTraverser->traverseNodesWithCallable($class->stmts, function (Node $node) use (
$this->traverseNodesWithCallable($class->stmts, function (Node $node) use (
&$fetchedLocalPropertyNameToTypes
) {
if (! $node instanceof PropertyFetch) {
Expand Down
13 changes: 1 addition & 12 deletions packages/CodeQuality/src/Rector/For_/ForToForeachRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use PhpParser\Node\Stmt\Foreach_;
use Rector\Exception\ShouldNotHappenException;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\NodeTraverser\CallableNodeTraverser;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
Expand All @@ -41,16 +40,6 @@ final class ForToForeachRector extends AbstractRector
*/
private $iteratedExpr;

/**
* @var CallableNodeTraverser
*/
private $callableNodeTraverser;

public function __construct(CallableNodeTraverser $callableNodeTraverser)
{
$this->callableNodeTraverser = $callableNodeTraverser;
}

public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Change for() to foreach() where useful', [
Expand Down Expand Up @@ -257,7 +246,7 @@ private function useForeachVariableInStmts(Expr $expr, array $stmts): void
throw new ShouldNotHappenException();
}

$this->callableNodeTraverser->traverseNodesWithCallable($stmts, function (Node $node) use ($expr): ?Expr {
$this->traverseNodesWithCallable($stmts, function (Node $node) use ($expr): ?Expr {
if (! $node instanceof ArrayDimFetch) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use PHPStan\Type\ObjectType;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Node\Manipulator\BinaryOpManipulator;
use Rector\PhpParser\NodeTraverser\CallableNodeTraverser;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
Expand All @@ -29,19 +28,13 @@ final class ForeachToInArrayRector extends AbstractRector
*/
private $comments = [];

/**
* @var CallableNodeTraverser
*/
private $callableNodeTraverser;

/**
* @var BinaryOpManipulator
*/
private $binaryOpManipulator;

public function __construct(CallableNodeTraverser $callableNodeTraverser, BinaryOpManipulator $binaryOpManipulator)
public function __construct(BinaryOpManipulator $binaryOpManipulator)
{
$this->callableNodeTraverser = $callableNodeTraverser;
$this->binaryOpManipulator = $binaryOpManipulator;
}

Expand Down Expand Up @@ -226,7 +219,7 @@ private function createInArrayFunction(Node $node, BinaryOp $binaryOp, Foreach_
*/
private function combineCommentsToNode(Node $originalNode, Node $newNode): void
{
$this->callableNodeTraverser->traverseNodesWithCallable([$originalNode], function (Node $node): void {
$this->traverseNodesWithCallable([$originalNode], function (Node $node): void {
if ($node->hasAttribute('comments')) {
$this->comments = array_merge($this->comments, $node->getComments());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Catch_;
use Rector\CodingStyle\Naming\ClassNaming;
use Rector\PhpParser\NodeTraverser\CallableNodeTraverser;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
Expand All @@ -18,15 +17,9 @@ final class CatchExceptionNameMatchingTypeRector extends AbstractRector
*/
private $classNaming;

/**
* @var CallableNodeTraverser
*/
private $callableNodeTraverser;

public function __construct(ClassNaming $classNaming, CallableNodeTraverser $callableNodeTraverser)
public function __construct(ClassNaming $classNaming)
{
$this->classNaming = $classNaming;
$this->callableNodeTraverser = $callableNodeTraverser;
}

public function getDefinition(): RectorDefinition
Expand Down Expand Up @@ -103,7 +96,7 @@ public function refactor(Node $node): ?Node

private function renameVariableInStmts(Catch_ $catch, string $oldVariableName, string $newVariableName): void
{
$this->callableNodeTraverser->traverseNodesWithCallable($catch->stmts, function (Node $node) use (
$this->traverseNodesWithCallable($catch->stmts, function (Node $node) use (
$oldVariableName,
$newVariableName
): void {
Expand Down
16 changes: 3 additions & 13 deletions packages/CodingStyle/src/Rector/Use_/RemoveUnusedAliasRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Rector\CodingStyle\Naming\ClassNaming;
use Rector\Exception\ShouldNotHappenException;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\NodeTraverser\CallableNodeTraverser;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
Expand All @@ -37,11 +36,6 @@ final class RemoveUnusedAliasRector extends AbstractRector
*/
private $resolvedDocPossibleAliases = [];

/**
* @var CallableNodeTraverser
*/
private $callableNodeTraverser;

/**
* @var ShortNameResolver
*/
Expand All @@ -52,12 +46,8 @@ final class RemoveUnusedAliasRector extends AbstractRector
*/
private $classNaming;

public function __construct(
CallableNodeTraverser $callableNodeTraverser,
ShortNameResolver $shortNameResolver,
ClassNaming $classNaming
) {
$this->callableNodeTraverser = $callableNodeTraverser;
public function __construct(ShortNameResolver $shortNameResolver, ClassNaming $classNaming)
{
$this->shortNameResolver = $shortNameResolver;
$this->classNaming = $classNaming;
}
Expand Down Expand Up @@ -308,7 +298,7 @@ private function resolveTraitUseNames(Node $searchNode): void

private function resolveDocPossibleAliases(Node $searchNode): void
{
$this->callableNodeTraverser->traverseNodesWithCallable([$searchNode], function (Node $node): void {
$this->traverseNodesWithCallable([$searchNode], function (Node $node): void {
if ($node->getDocComment() === null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use Rector\PhpParser\Node\Manipulator\ClassManipulator;
use Rector\PhpParser\NodeTraverser\CallableNodeTraverser;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
Expand All @@ -40,11 +39,6 @@ final class NetteControlToSymfonyControllerRector extends AbstractRector
*/
private $classManipulator;

/**
* @var CallableNodeTraverser
*/
private $callableNodeTraverser;

/**
* @var Expr|null
*/
Expand All @@ -57,11 +51,9 @@ final class NetteControlToSymfonyControllerRector extends AbstractRector

public function __construct(
ClassManipulator $classManipulator,
CallableNodeTraverser $callableNodeTraverser,
string $netteControlClass = 'Nette\Application\UI\Control'
) {
$this->classManipulator = $classManipulator;
$this->callableNodeTraverser = $callableNodeTraverser;
$this->netteControlClass = $netteControlClass;
}

Expand Down Expand Up @@ -163,9 +155,7 @@ private function collectTemplateFileNameAndVariables(ClassMethod $classMethod):
$this->templateFileExpr = null;
$this->templateVariables = [];

$this->callableNodeTraverser->traverseNodesWithCallable((array) $classMethod->stmts, function (
Node $node
): void {
$this->traverseNodesWithCallable((array) $classMethod->stmts, function (Node $node): void {
if ($node instanceof MethodCall && $this->isName($node, 'render')) {
$this->templateFileExpr = $node->args[0]->value;
$this->removeNode($node);
Expand Down
14 changes: 1 addition & 13 deletions packages/Php/src/Rector/Assign/AssignArrayToStringRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\PhpParser\NodeTraverser\CallableNodeTraverser;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
Expand All @@ -36,16 +35,6 @@ final class AssignArrayToStringRector extends AbstractRector
*/
private $emptyStringPropertyNodes = [];

/**
* @var CallableNodeTraverser
*/
private $callableNodeTraverser;

public function __construct(CallableNodeTraverser $callableNodeTraverser)
{
$this->callableNodeTraverser = $callableNodeTraverser;
}

public function getDefinition(): RectorDefinition
{
return new RectorDefinition(
Expand Down Expand Up @@ -115,8 +104,7 @@ public function refactor(Node $node): ?Node
public function beforeTraverse(array $nodes): ?array
{
// collect all known "{anything} = '';" assigns

$this->callableNodeTraverser->traverseNodesWithCallable($nodes, function (Node $node): void {
$this->traverseNodesWithCallable($nodes, function (Node $node): void {
if ($node instanceof PropertyProperty && $node->default && $this->isEmptyStringNode($node->default)) {
$this->emptyStringPropertyNodes[] = $node;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Variable;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\NodeTraverser\CallableNodeTraverser;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
Expand All @@ -19,16 +18,6 @@
*/
final class ParseStrWithResultArgumentRector extends AbstractRector
{
/**
* @var CallableNodeTraverser
*/
private $callableNodeTraverser;

public function __construct(CallableNodeTraverser $callableNodeTraverser)
{
$this->callableNodeTraverser = $callableNodeTraverser;
}

public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Use $result argument in parse_str() function', [
Expand Down Expand Up @@ -77,7 +66,7 @@ public function refactor(Node $node): ?Node
return null;
}

$this->callableNodeTraverser->traverseNodesWithCallable([$nextExpression], function (Node $node) use (
$this->traverseNodesWithCallable([$nextExpression], function (Node $node) use (
$resultVariable
): ?Variable {
if ($node instanceof FuncCall) {
Expand Down
11 changes: 2 additions & 9 deletions packages/Php/src/Rector/FuncCall/PregReplaceEModifierRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use PhpParser\Node\Stmt\Return_;
use PhpParser\Parser;
use Rector\Exception\ShouldNotHappenException;
use Rector\PhpParser\NodeTraverser\CallableNodeTraverser;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
Expand All @@ -32,15 +31,9 @@ final class PregReplaceEModifierRector extends AbstractRector
*/
private $parser;

/**
* @var CallableNodeTraverser
*/
private $callableNodeTraverser;

public function __construct(Parser $parser, CallableNodeTraverser $callableNodeTraverser)
public function __construct(Parser $parser)
{
$this->parser = $parser;
$this->callableNodeTraverser = $callableNodeTraverser;
}

public function getDefinition(): RectorDefinition
Expand Down Expand Up @@ -130,7 +123,7 @@ private function createAnonymousFunctionFromString(Expr $expr): ?Closure

$stmt = $contentNodes[0]->expr;

$this->callableNodeTraverser->traverseNodesWithCallable([$stmt], function (Node $node): Node {
$this->traverseNodesWithCallable([$stmt], function (Node $node): Node {
if (! $node instanceof String_) {
return $node;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use PHPStan\Analyser\Scope;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Php\Exception\BreakScopeException;
use Rector\PhpParser\NodeTraverser\CallableNodeTraverser;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
Expand All @@ -32,16 +31,6 @@ final class AddDefaultValueForUndefinedVariableRector extends AbstractRector
*/
private $undefinedVariables = [];

/**
* @var CallableNodeTraverser
*/
private $callableNodeTraverser;

public function __construct(CallableNodeTraverser $callableNodeTraverser)
{
$this->callableNodeTraverser = $callableNodeTraverser;
}

public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Adds default value for undefined variable', [
Expand Down Expand Up @@ -92,7 +81,7 @@ public function refactor(Node $node): ?Node
$this->undefinedVariables = [];

try {
$this->callableNodeTraverser->traverseNodesWithCallable((array) $node->stmts, function (Node $node) {
$this->traverseNodesWithCallable((array) $node->stmts, function (Node $node) {
// entering new scope - break!
if ($node instanceof FunctionLike) {
throw new BreakScopeException();
Expand Down
Loading

0 comments on commit 61a2b35

Please sign in to comment.