Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/MergeIsCandidateRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,11 @@ private function createClassConstFetchFromClassName(string $className): ClassCon
private function replaceReturnFalseWithReturnNull(ClassMethod $classMethod): void
{
$this->callbackNodeTraverser->traverseNodesWithCallable([$classMethod], function (Node $node): ?Node {
if (!$node instanceof Return_ || !$node->expr instanceof ConstFetch) {
if (! $node instanceof Return_ || ! $node->expr instanceof ConstFetch) {
return null;
}

if ((string) $node->expr->name === 'false') {
if ($this->isFalse($node->expr)) {
return new Return_(new ConstFetch(new Name('null')));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (! $this->isInArrayFunction($node)) {
if (! $this->isName($node, 'in_array')) {
return null;
}

Expand All @@ -41,33 +41,20 @@ public function refactor(Node $node): ?Node
return null;
}

/** @var Name $functionName */
$functionName = $secondArgument->name;
if ($functionName->toString() !== 'array_keys') {
if (! $this->isName($secondArgument, 'array_keys')) {
return null;
}

if (count($secondArgument->args) > 1) {
return null;
}

[$key, $array] = $node->args;

$array = $array->value->args[0];

$node->args = [$key, $array];

$node->name = new Name('array_key_exists');
$node->args = [$key, $array];

return $node;
}

private function isInArrayFunction(FuncCall $funcCallNode): bool
{
$funcCallName = $funcCallNode->name;
if (! $funcCallName instanceof Name) {
return false;
}

return $funcCallName->toString() === 'in_array';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use PhpParser\Node\Expr\BinaryOp\SmallerOrEqual;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Identifier;
use Rector\Exception\NotImplementedException;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
Expand Down Expand Up @@ -76,21 +75,14 @@ public function refactor(Node $node): ?Node
return null;
}

/** @var Identifier $ifExpressionName */
$ifExpressionName = $ifExpression->name;

/** @var Identifier $elseExpressionName */
$elseExpressionName = $elseExpression->name;

$ifValue = $ifExpressionName->toLowerString();
$elseValue = $elseExpressionName->toLowerString();
if (in_array('null', [$ifValue, $elseValue], true)) {
if ($this->isNull($ifExpression) || $this->isNull($elseExpression)) {
return null;
}

/** @var BinaryOp $binaryOperation */
$binaryOperation = $node->cond;

if ($ifValue === 'true' && $elseValue === 'false') {
if ($this->isTrue($ifExpression) && $this->isFalse($elseExpression)) {
return $binaryOperation;
}

Expand Down
25 changes: 11 additions & 14 deletions packages/Doctrine/src/Rector/AliasToClassRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Scalar\String_;
use Rector\Node\NodeFactory;
use Rector\NodeAnalyzer\MethodArgumentAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
Expand All @@ -23,22 +23,13 @@ final class AliasToClassRector extends AbstractRector
*/
private $nodeFactory;

/**
* @var MethodArgumentAnalyzer
*/
private $methodArgumentAnalyzer;

/**
* @param string[] $aliasesToNamespaces
*/
public function __construct(
array $aliasesToNamespaces,
MethodArgumentAnalyzer $methodArgumentAnalyzer,
NodeFactory $nodeFactory
) {
public function __construct(array $aliasesToNamespaces, NodeFactory $nodeFactory)
{
$this->aliasesToNamespaces = $aliasesToNamespaces;
$this->nodeFactory = $nodeFactory;
$this->methodArgumentAnalyzer = $methodArgumentAnalyzer;
}

public function getDefinition(): RectorDefinition
Expand Down Expand Up @@ -75,11 +66,17 @@ public function refactor(Node $node): ?Node
return null;
}

if (! $this->methodArgumentAnalyzer->isMethodNthArgumentString($node, 1)) {
if (! isset($node->args[0])) {
return null;
}

if (! $node->args[0]->value instanceof String_) {
return null;
}

if (! $this->isAliasWithConfiguredEntity($node->args[0]->value->value)) {
/** @var String_ $stringNode */
$stringNode = $node->args[0]->value;
if (! $this->isAliasWithConfiguredEntity($stringNode->value)) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Scalar\LNumber;
use Rector\Builder\IdentifierRenamer;
use Rector\Rector\AbstractPHPUnitRector;
Expand Down Expand Up @@ -102,10 +101,7 @@ private function resolveOldCondition(Node $node): int
}

if ($node instanceof ConstFetch) {
/** @var Identifier $constFetchName */
$constFetchName = $node->name;

return $constFetchName->toLowerString() === 'true' ? 1 : 0;
return $this->isTrue($node) ? 1 : 0;
}
}

Expand Down
3 changes: 1 addition & 2 deletions packages/PhpParser/src/Rector/RemoveNodeRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ public function refactor(Node $node): ?Node
return null;
}

$value = $node->expr->name->toString();
if ($value !== 'false') {
if (! $this->isFalse($node->expr)) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php declare(strict_types=1);

namespace Rector\PhpParser\Tests\Rector\RemoveNodeRector\Wrong;

class MyAbstractVisitor extends \PhpParser\NodeVisitorAbstract
{

}

class SomeVisitor extends MyAbstractVisitor
class Visitor3 extends MyAbstractVisitor
{
public function leaveNode(\PhpParser\Node $node)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

namespace Rector\PhpParser\Tests\Rector\RemoveNodeRector\Wrong;

class Visitor4 extends \PhpParser\NodeVisitorAbstract
{
public function leaveNode(\PhpParser\Node $node)
{
return \PhpParser\NodeTraverser::REMOVE_NODE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public function provideWrongToFixedFiles(): Iterator
{
yield [__DIR__ . '/Wrong/wrong.php.inc', __DIR__ . '/Correct/correct.php.inc'];
yield [__DIR__ . '/Wrong/wrong2.php.inc', __DIR__ . '/Correct/correct2.php.inc'];
yield [__DIR__ . '/Wrong/wrong3.php.inc', __DIR__ . '/Correct/correct3.php.inc'];
yield [__DIR__ . '/Wrong/wrong4.php.inc', __DIR__ . '/Correct/correct4.php.inc'];
}

protected function provideConfig(): string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php declare(strict_types=1);

namespace Rector\PhpParser\Tests\Rector\RemoveNodeRector\Wrong;

class MyAbstractVisitor extends \PhpParser\NodeVisitorAbstract
{

}

class SomeVisitor extends MyAbstractVisitor
class Visitor3 extends MyAbstractVisitor
{
public function leaveNode(\PhpParser\Node $node)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

namespace Rector\PhpParser\Tests\Rector\RemoveNodeRector\Wrong;

class Visitor4 extends \PhpParser\NodeVisitorAbstract
{
public function leaveNode(\PhpParser\Node $node)
{
return FALSE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (! Strings::startsWith($node->name->toString(), 'SS_')) {
$constantName = $this->getName($node);

if (! Strings::startsWith($constantName, 'SS_')) {
return null;
}

$staticCallNode = new StaticCall(new FullyQualified('Environment'), 'getEnv');
$staticCallNode->args[] = new Arg(new String_($node->name->toString()));
$staticCallNode->args[] = new Arg(new String_($constantName));

return $staticCallNode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,19 @@
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use Rector\Builder\IdentifierRenamer;
use Rector\NodeAnalyzer\MethodArgumentAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;

final class ReplaceCreateMethodWithoutReviewerRector extends AbstractRector
{
/**
* @var MethodArgumentAnalyzer
*/
private $methodArgumentAnalyzer;

/**
* @var IdentifierRenamer
*/
private $identifierRenamer;

public function __construct(
MethodArgumentAnalyzer $methodArgumentAnalyzer,
IdentifierRenamer $identifierRenamer
) {
$this->methodArgumentAnalyzer = $methodArgumentAnalyzer;
public function __construct(IdentifierRenamer $identifierRenamer)
{
$this->identifierRenamer = $identifierRenamer;
}

Expand Down Expand Up @@ -60,15 +51,13 @@ public function refactor(Node $node): ?Node
return null;
}

if ($this->methodArgumentAnalyzer->hasMethodNthArgument($node, 2)
&& ! $this->methodArgumentAnalyzer->isMethodNthArgumentNull($node, 2)
) {
if (isset($node->args[1]) && ! $this->isNull($node->args[1]->value)) {
return null;
}

$this->identifierRenamer->renameNode($node, 'createForSubject');

if ($this->methodArgumentAnalyzer->hasMethodNthArgument($node, 2)) {
if (isset($node->args[1])) {
Copy link
Copy Markdown
Member Author

@TomasVotruba TomasVotruba Oct 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To keep it consistent with $node->params, $node->stmts, $node->args where keys are from 0. The start with 1 is confusing then.

$node->args = [array_shift($node->args)];
}

Expand Down
11 changes: 2 additions & 9 deletions packages/Symfony/src/Rector/Controller/RedirectToRouteRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,13 @@
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use Rector\Node\MethodCallNodeFactory;
use Rector\NodeAnalyzer\MethodArgumentAnalyzer;
use Rector\NodeTypeResolver\Node\Attribute;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;

final class RedirectToRouteRector extends AbstractRector
{
/**
* @var MethodArgumentAnalyzer
*/
private $methodArgumentAnalyzer;

/**
* @var MethodCallNodeFactory
*/
Expand All @@ -29,11 +23,9 @@ final class RedirectToRouteRector extends AbstractRector
private $controllerClass;

public function __construct(
MethodArgumentAnalyzer $methodArgumentAnalyzer,
MethodCallNodeFactory $methodCallNodeFactory,
string $controllerClass = 'Symfony\Bundle\FrameworkBundle\Controller\Controller'
) {
$this->methodArgumentAnalyzer = $methodArgumentAnalyzer;
$this->methodCallNodeFactory = $methodCallNodeFactory;
$this->controllerClass = $controllerClass;
}
Expand Down Expand Up @@ -68,7 +60,8 @@ public function refactor(Node $node): ?Node
if (! $this->isName($node, 'redirect')) {
return null;
}
if (! $this->methodArgumentAnalyzer->hasMethodNthArgument($node, 1)) {

if (! isset($node->args[0])) {
return null;
}

Expand Down
Loading