mirror of
https://github.com/rectorphp/rector.git
synced 2024-08-12 19:40:23 +00:00
[CodeQuality] Add AndAssignsToSeparateLinesRector
This commit is contained in:
parent
dfc1435e92
commit
d428d7e15f
@ -29,3 +29,7 @@ services:
|
||||
Rector\CodeQuality\Rector\Ternary\SimplifyDuplicatedTernaryRector: ~
|
||||
Rector\CodeQuality\Rector\Identical\SimplifyBoolIdenticalTrueRector: ~
|
||||
Rector\CodeQuality\Rector\FuncCall\SimplifyRegexPatternRector: ~
|
||||
|
||||
Rector\CodeQuality\Rector\Identical\BooleanNotIdenticalToNotIdenticalRector: ~
|
||||
Rector\CodeQuality\Rector\Array_\CallableThisArrayToAnonymousFunctionRector: ~
|
||||
Rector\CodeQuality\Rector\LogicalAnd\AndAssignsToSeparateLinesRector: ~
|
||||
|
@ -1,4 +1,2 @@
|
||||
services:
|
||||
Rector\Symfony\Rector\BinaryOp\ResponseStatusCodeRector: ~
|
||||
Rector\CodeQuality\Rector\Identical\BooleanNotIdenticalToNotIdenticalRector: ~
|
||||
Rector\CodeQuality\Rector\Array_\CallableThisArrayToAnonymousFunctionRector: ~
|
||||
|
@ -0,0 +1,73 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodeQuality\Rector\LogicalAnd;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\BinaryOp\LogicalAnd;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\RectorDefinition\CodeSample;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
|
||||
/**
|
||||
* @see https://3v4l.org/ji8bX
|
||||
*/
|
||||
final class AndAssignsToSeparateLinesRector extends AbstractRector
|
||||
{
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Split 2 assigns ands to separate line', [
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$tokens = [];
|
||||
$token = 4 and $tokens[] = $token;
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$tokens = [];
|
||||
$token = 4;
|
||||
$tokens[] = $token;
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [LogicalAnd::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param LogicalAnd $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if (! $node->left instanceof Node\Expr\Assign || ! $node->right instanceof Node\Expr\Assign) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if (! $parentNode instanceof Node\Stmt\Expression) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->addNodeAfterNode($node->right, $node);
|
||||
|
||||
return $node->left;
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\LogicalAnd\AndAssignsToSeparateLinesRector;
|
||||
|
||||
use Rector\CodeQuality\Rector\LogicalAnd\AndAssignsToSeparateLinesRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
final class AndAssignsToSeparateLinesRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
public function test(): void
|
||||
{
|
||||
$this->doTestFiles([__DIR__ . '/Fixture/fixture.php.inc', __DIR__ . '/Fixture/keep_in_condition.php.inc']);
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return AndAssignsToSeparateLinesRector::class;
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\LogicalAnd\AndAssignsToSeparateLinesRector\Fixture;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$tokens = [];
|
||||
$token = 4 and $tokens[] = $token;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\LogicalAnd\AndAssignsToSeparateLinesRector\Fixture;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$tokens = [];
|
||||
$token = 4;
|
||||
$tokens[] = $token;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\LogicalAnd\AndAssignsToSeparateLinesRector\Fixture;
|
||||
|
||||
class KeepInCondition
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$tokens = [];
|
||||
if ($token = 4 and $tokens[] = $token) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $tokens;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user