-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds the ReverseCondiationableMethodCallRector (#245)
* Adds the ReverseCondiationableMethodCallRector * fix docs
- Loading branch information
Showing
9 changed files
with
217 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/Rector/MethodCall/ReverseConditionableMethodCallRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Rector\MethodCall; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Arg; | ||
use PhpParser\Node\Expr\BooleanNot; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Identifier; | ||
use PHPStan\Type\ObjectType; | ||
use Rector\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \RectorLaravel\Tests\Rector\MethodCall\ReverseConditionableMethodCallRector\ReverseConditionableMethodCallRectorTest | ||
*/ | ||
class ReverseConditionableMethodCallRector extends AbstractRector | ||
{ | ||
private const CONDITIONABLE_TRAIT = 'Illuminate\Support\Traits\Conditionable'; | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Reverse conditionable method calls', | ||
[ | ||
new CodeSample(<<<'CODE_SAMPLE' | ||
$conditionable->when(!$condition, function () {}); | ||
CODE_SAMPLE, | ||
<<<'CODE_SAMPLE' | ||
$conditionable->unless($condition, function () {}); | ||
CODE_SAMPLE | ||
), | ||
new CodeSample(<<<'CODE_SAMPLE' | ||
$conditionable->unless(!$condition, function () {}); | ||
CODE_SAMPLE, | ||
<<<'CODE_SAMPLE' | ||
$conditionable->when($condition, function () {}); | ||
CODE_SAMPLE | ||
), | ||
] | ||
); | ||
} | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [MethodCall::class]; | ||
} | ||
|
||
/** | ||
* @param MethodCall $node | ||
*/ | ||
public function refactor(Node $node): ?MethodCall | ||
{ | ||
if (! $this->isObjectType($node->var, new ObjectType(self::CONDITIONABLE_TRAIT))) { | ||
return null; | ||
} | ||
|
||
if (! $this->isNames($node->name, ['when', 'unless'])) { | ||
return null; | ||
} | ||
|
||
if ($node->isFirstClassCallable()) { | ||
return null; | ||
} | ||
|
||
if ($node->getArgs() === []) { | ||
return null; | ||
} | ||
|
||
$arg = $node->getArgs()[0]; | ||
|
||
if (! $node->name instanceof Identifier) { | ||
return null; | ||
} | ||
|
||
if ($arg->value instanceof BooleanNot) { | ||
$node->args[0] = new Arg($arg->value->expr); | ||
$name = $node->name->toString() === 'when' ? 'unless' : 'when'; | ||
|
||
$node->name = new Identifier($name); | ||
|
||
return $node; | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Illuminate\Support\Traits; | ||
|
||
if (trait_exists('Illuminate\Support\Traits\Conditionable')) { | ||
return; | ||
} | ||
|
||
trait Conditionable | ||
{ | ||
public function when($value = null, ?callable $callback = null, ?callable $default = null) | ||
{ | ||
} | ||
|
||
public function unless($value = null, ?callable $callback = null, ?callable $default = null) | ||
{ | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
tests/Rector/MethodCall/ReverseConditionableMethodCallRector/Fixture/fixture.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\MethodCall\ReverseConditionableMethodCallRector\Fixture; | ||
|
||
use RectorLaravel\Tests\Rector\MethodCall\ReverseConditionableMethodCallRector\Source\ConditionableExample; | ||
|
||
$conditionable = new ConditionableExample(); | ||
|
||
$conditionable->when(!true, function () {}); | ||
$conditionable->unless(!false, function () {}); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\MethodCall\ReverseConditionableMethodCallRector\Fixture; | ||
|
||
use RectorLaravel\Tests\Rector\MethodCall\ReverseConditionableMethodCallRector\Source\ConditionableExample; | ||
|
||
$conditionable = new ConditionableExample(); | ||
|
||
$conditionable->unless(true, function () {}); | ||
$conditionable->when(false, function () {}); | ||
|
||
?> |
10 changes: 10 additions & 0 deletions
10
...odCall/ReverseConditionableMethodCallRector/Fixture/skip_non_conditionable_object.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\MethodCall\ReverseConditionableMethodCallRector\Fixture; | ||
|
||
$conditionable = new \stdClass(); | ||
|
||
$conditionable->when(!true, function () {}); | ||
$conditionable->unless(!false, function () {}); | ||
|
||
?> |
31 changes: 31 additions & 0 deletions
31
...hodCall/ReverseConditionableMethodCallRector/ReverseConditionableMethodCallRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RectorLaravel\Tests\Rector\MethodCall\ReverseConditionableMethodCallRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class ReverseConditionableMethodCallRectorTest extends AbstractRectorTestCase | ||
{ | ||
public static function provideData(): Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
#[DataProvider('provideData')] | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
tests/Rector/MethodCall/ReverseConditionableMethodCallRector/Source/ConditionableExample.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\MethodCall\ReverseConditionableMethodCallRector\Source; | ||
|
||
use Illuminate\Support\Traits\Conditionable; | ||
|
||
class ConditionableExample | ||
{ | ||
use Conditionable; | ||
} |
12 changes: 12 additions & 0 deletions
12
tests/Rector/MethodCall/ReverseConditionableMethodCallRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use RectorLaravel\Rector\MethodCall\ReverseConditionableMethodCallRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php'); | ||
|
||
$rectorConfig->rule(ReverseConditionableMethodCallRector::class); | ||
}; |