-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CodeQuality] Adds rules for the with and value helpers (#175)
* Adds rules for the with and value helpers * cs fixes
- Loading branch information
Showing
14 changed files
with
359 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Rector\FuncCall; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\FuncCall; | ||
use PhpParser\Node\Name; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Type\ClosureType; | ||
use PHPStan\Type\MixedType; | ||
use Rector\Rector\AbstractScopeAwareRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantValueCallsRector\RemoveRedundantValueCallsRectorTest | ||
*/ | ||
class RemoveRedundantValueCallsRector extends AbstractScopeAwareRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Removes redundant value helper calls', [ | ||
new CodeSample( | ||
'value(new Object())->something();', | ||
'(new Object())->something();' | ||
), | ||
]); | ||
} | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [FuncCall::class]; | ||
} | ||
|
||
public function refactorWithScope(Node $node, Scope $scope): ?Node | ||
{ | ||
if (! $node instanceof FuncCall) { | ||
return null; | ||
} | ||
|
||
if (! $node->name instanceof Name) { | ||
return null; | ||
} | ||
|
||
if (! $this->isName($node->name, 'value')) { | ||
return null; | ||
} | ||
|
||
$args = $node->getArgs(); | ||
|
||
if (count($args) !== 1) { | ||
return null; | ||
} | ||
|
||
if ($scope->getType($args[0]->value)->isSuperTypeOf(new ClosureType([], new MixedType, true))->no() === false) { | ||
return null; | ||
} | ||
|
||
return $args[0]->value; | ||
} | ||
} |
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,63 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Rector\FuncCall; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\FuncCall; | ||
use PhpParser\Node\Name; | ||
use PHPStan\Analyser\Scope; | ||
use Rector\Rector\AbstractScopeAwareRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantWithCallsRector\RemoveRedundantWithCallsRectorTest | ||
*/ | ||
class RemoveRedundantWithCallsRector extends AbstractScopeAwareRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Removes redundant with helper calls', [ | ||
new CodeSample( | ||
'with(new Object())->something();', | ||
'(new Object())->something();' | ||
), | ||
]); | ||
} | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [FuncCall::class]; | ||
} | ||
|
||
public function refactorWithScope(Node $node, Scope $scope): ?Node | ||
{ | ||
if (! $node instanceof FuncCall) { | ||
return null; | ||
} | ||
|
||
if (! $node->name instanceof Name) { | ||
return null; | ||
} | ||
|
||
if (! $this->isName($node->name, 'with')) { | ||
return null; | ||
} | ||
|
||
$args = $node->getArgs(); | ||
|
||
if (count($args) < 1 || count($args) > 2) { | ||
return null; | ||
} | ||
|
||
if (count($args) === 2) { | ||
$secondArgumentType = $scope->getType($args[1]->value); | ||
|
||
if ($secondArgumentType->isCallable()->no() === false) { | ||
return null; | ||
} | ||
} | ||
|
||
return $args[0]->value; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
tests/Rector/FuncCall/RemoveRedundantValueCallsRector/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,35 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantValueCallsRector\Fixture; | ||
|
||
use RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantValueCallsRector\Source\HelperObject; | ||
|
||
value(new HelperObject())->store(); | ||
|
||
$foo = value(new HelperObject()); | ||
|
||
$user = 'a'; | ||
|
||
value($user)->store(); | ||
|
||
$foo = value($user); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantValueCallsRector\Fixture; | ||
|
||
use RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantValueCallsRector\Source\HelperObject; | ||
|
||
(new HelperObject())->store(); | ||
|
||
$foo = new HelperObject(); | ||
|
||
$user = 'a'; | ||
|
||
$user->store(); | ||
|
||
$foo = $user; | ||
|
||
?> |
11 changes: 11 additions & 0 deletions
11
...Call/RemoveRedundantValueCallsRector/Fixture/skips_object_without_an_obvious_type.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,11 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantValueCallsRector\Fixture; | ||
|
||
value(new UndefinedClass())->store(); | ||
|
||
$foo = value(new UndefinedClass()); | ||
|
||
$user = new UndefinedClass(); | ||
|
||
value($user)->store(); |
31 changes: 31 additions & 0 deletions
31
...s/Rector/FuncCall/RemoveRedundantValueCallsRector/RemoveRedundantValueCallsRectorTest.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\FuncCall\RemoveRedundantValueCallsRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class RemoveRedundantValueCallsRectorTest 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/FuncCall/RemoveRedundantValueCallsRector/Source/HelperObject.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\FuncCall\RemoveRedundantValueCallsRector\Source; | ||
|
||
class HelperObject | ||
{ | ||
public function store(): void | ||
{ | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
tests/Rector/FuncCall/RemoveRedundantValueCallsRector/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\FuncCall\RemoveRedundantValueCallsRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php'); | ||
|
||
$rectorConfig->rule(RemoveRedundantValueCallsRector::class); | ||
}; |
35 changes: 35 additions & 0 deletions
35
tests/Rector/FuncCall/RemoveRedundantWithCallsRector/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,35 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantWithCallsRector\Fixture; | ||
|
||
use RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantWithCallsRector\Source\HelperObject; | ||
|
||
with(new HelperObject())->store(); | ||
|
||
$foo = with(new HelperObject()); | ||
|
||
$user = 'a'; | ||
|
||
with($user)->store(); | ||
|
||
$foo = with($user); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantWithCallsRector\Fixture; | ||
|
||
use RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantWithCallsRector\Source\HelperObject; | ||
|
||
(new HelperObject())->store(); | ||
|
||
$foo = new HelperObject(); | ||
|
||
$user = 'a'; | ||
|
||
$user->store(); | ||
|
||
$foo = $user; | ||
|
||
?> |
9 changes: 9 additions & 0 deletions
9
...RemoveRedundantWithCallsRector/Fixture/skips_calls_with_callables_as_a_second_arg.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,9 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantWithCallsRector\Fixture; | ||
|
||
use RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantWithCallsRector\Source\HelperObject; | ||
|
||
with(new HelperObject(), function () { | ||
|
||
})->store(); |
12 changes: 12 additions & 0 deletions
12
...cCall/RemoveRedundantWithCallsRector/Fixture/skips_object_without_an_obvious_type.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,12 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantWithCallsRector\Fixture; | ||
|
||
with(new UndefinedClass(), new AnotherUndefinedType())->store(); | ||
|
||
$foo = with(new UndefinedClass(), new AnotherUndefinedType()); | ||
|
||
$user = new UndefinedClass(); | ||
$bar = new AnotherUndefinedType(); | ||
|
||
with($user, $bar)->store(); |
31 changes: 31 additions & 0 deletions
31
tests/Rector/FuncCall/RemoveRedundantWithCallsRector/RemoveRedundantWithCallsRectorTest.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\FuncCall\RemoveRedundantWithCallsRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class RemoveRedundantWithCallsRectorTest 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/FuncCall/RemoveRedundantWithCallsRector/Source/HelperObject.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\FuncCall\RemoveRedundantWithCallsRector\Source; | ||
|
||
class HelperObject | ||
{ | ||
public function store(): void | ||
{ | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
tests/Rector/FuncCall/RemoveRedundantWithCallsRector/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\FuncCall\RemoveRedundantWithCallsRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php'); | ||
|
||
$rectorConfig->rule(RemoveRedundantWithCallsRector::class); | ||
}; |