-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove null coalesce for methods with default argument (#268)
* basic mvp * Working in all scenarios * All positive test cases * Improvement * Working fully * fixes * simplify the rule configuration * improved * Fix removal of default * Docs and added to CodeQuality set * add env function * use data_get and position argument * cs fix * Update rector_rules_overview.md
- Loading branch information
Showing
10 changed files
with
288 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
136 changes: 136 additions & 0 deletions
136
src/Rector/Coalesce/ApplyDefaultInsteadOfNullCoalesceRector.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,136 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Rector\Coalesce; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Arg; | ||
use PhpParser\Node\Expr\BinaryOp\Coalesce; | ||
use PhpParser\Node\Expr\FuncCall; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PHPStan\Type\ObjectType; | ||
use Rector\Contract\Rector\ConfigurableRectorInterface; | ||
use Rector\Rector\AbstractRector; | ||
use RectorLaravel\ValueObject\ApplyDefaultInsteadOfNullCoalesce; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
use Webmozart\Assert\Assert; | ||
|
||
/** | ||
* @see \RectorLaravel\Tests\Rector\Coalesce\ApplyDefaultInsteadOfNullCoalesceRector\ApplyDefaultInsteadOfNullCoalesceRectorTest | ||
*/ | ||
final class ApplyDefaultInsteadOfNullCoalesceRector extends AbstractRector implements ConfigurableRectorInterface | ||
{ | ||
/** | ||
* @var ApplyDefaultInsteadOfNullCoalesce[] | ||
*/ | ||
private array $applyDefaultWith; | ||
|
||
public function __construct() | ||
{ | ||
$this->applyDefaultWith = self::defaultLaravelMethods(); | ||
} | ||
|
||
/** | ||
* @return ApplyDefaultInsteadOfNullCoalesce[] | ||
*/ | ||
public static function defaultLaravelMethods(): array | ||
{ | ||
return [ | ||
new ApplyDefaultInsteadOfNullCoalesce('config'), | ||
new ApplyDefaultInsteadOfNullCoalesce('env'), | ||
new ApplyDefaultInsteadOfNullCoalesce('data_get', argumentPosition: 2), | ||
new ApplyDefaultInsteadOfNullCoalesce('input', new ObjectType('Illuminate\Http\Request')), | ||
new ApplyDefaultInsteadOfNullCoalesce('get', new ObjectType('Illuminate\Support\Env')), | ||
]; | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Apply default instead of null coalesce', | ||
[ | ||
new ConfiguredCodeSample( | ||
<<<'CODE_SAMPLE' | ||
custom_helper('app.name') ?? 'Laravel'; | ||
CODE_SAMPLE, | ||
<<<'CODE_SAMPLE' | ||
custom_helper('app.name', 'Laravel'); | ||
CODE_SAMPLE | ||
, | ||
[ | ||
...self::defaultLaravelMethods(), | ||
new ApplyDefaultInsteadOfNullCoalesce('custom_helper'), | ||
], | ||
), | ||
] | ||
); | ||
} | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [Coalesce::class]; | ||
} | ||
|
||
/** | ||
* @param Coalesce $node | ||
*/ | ||
public function refactor(Node $node): MethodCall|StaticCall|FuncCall|null | ||
{ | ||
if (! $node->left instanceof FuncCall && | ||
! $node->left instanceof MethodCall && | ||
! $node->left instanceof StaticCall | ||
) { | ||
return null; | ||
} | ||
|
||
if ($node->left->isFirstClassCallable()) { | ||
return null; | ||
} | ||
|
||
$call = $node->left; | ||
|
||
foreach ($this->applyDefaultWith as $applyDefaultWith) { | ||
$valid = false; | ||
|
||
$objectType = $call->var ?? $call->class ?? null; | ||
|
||
if ( | ||
$applyDefaultWith->getObjectType() instanceof ObjectType && | ||
$objectType !== null && | ||
$this->isObjectType( | ||
$objectType, | ||
$applyDefaultWith->getObjectType()) && | ||
$this->isName($call->name, $applyDefaultWith->getMethodName()) | ||
) { | ||
$valid = true; | ||
} elseif ( | ||
$applyDefaultWith->getObjectType() === null && | ||
$this->isName($call->name, $applyDefaultWith->getMethodName()) | ||
) { | ||
$valid = true; | ||
} | ||
|
||
if (! $valid) { | ||
continue; | ||
} | ||
|
||
if (count($call->args) === $applyDefaultWith->getArgumentPosition()) { | ||
if ($this->getType($node->right)->isNull()->yes()) { | ||
return $call; | ||
} | ||
$call->args[count($call->args)] = new Arg($node->right); | ||
|
||
return $call; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function configure(array $configuration): void | ||
{ | ||
Assert::allIsInstanceOf($configuration, ApplyDefaultInsteadOfNullCoalesce::class); | ||
$this->applyDefaultWith = $configuration; | ||
} | ||
} |
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,27 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\ValueObject; | ||
|
||
use PHPStan\Type\ObjectType; | ||
|
||
final readonly class ApplyDefaultInsteadOfNullCoalesce | ||
{ | ||
public function __construct(private string $methodName, private ?ObjectType $objectType = null, private int $argumentPosition = 1) | ||
{ | ||
} | ||
|
||
public function getObjectType(): ?ObjectType | ||
{ | ||
return $this->objectType; | ||
} | ||
|
||
public function getMethodName(): string | ||
{ | ||
return $this->methodName; | ||
} | ||
|
||
public function getArgumentPosition(): int | ||
{ | ||
return $this->argumentPosition; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...e/ApplyDefaultInsteadOfNullCoalesceRector/ApplyDefaultInsteadOfNullCoalesceRectorTest.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\Coalesce\ApplyDefaultInsteadOfNullCoalesceRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class ApplyDefaultInsteadOfNullCoalesceRectorTest 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'; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...pplyDefaultInsteadOfNullCoalesceRector/Fixture/applies_based_on_argument_position.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,15 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\Coalesce\ApplyDefaultInsteadOfNullCoalesceRector\Fixture; | ||
|
||
data_get([], 'key') ?? false; | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\Coalesce\ApplyDefaultInsteadOfNullCoalesceRector\Fixture; | ||
|
||
data_get([], 'key', false); | ||
|
||
?> |
25 changes: 25 additions & 0 deletions
25
tests/Rector/Coalesce/ApplyDefaultInsteadOfNullCoalesceRector/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\Coalesce\ApplyDefaultInsteadOfNullCoalesceRector\Fixture; | ||
|
||
config('app.name') ?? false; | ||
config('app.name') ?? null; | ||
|
||
(new \Illuminate\Http\Request())->input('value') ?? ''; | ||
|
||
\Illuminate\Support\Env::get('APP_NAME') ?? ''; | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\Coalesce\ApplyDefaultInsteadOfNullCoalesceRector\Fixture; | ||
|
||
config('app.name', false); | ||
config('app.name'); | ||
|
||
(new \Illuminate\Http\Request())->input('value', ''); | ||
|
||
\Illuminate\Support\Env::get('APP_NAME', ''); | ||
|
||
?> |
11 changes: 11 additions & 0 deletions
11
...Coalesce/ApplyDefaultInsteadOfNullCoalesceRector/Fixture/skip_already_set_default.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\Coalesce\ApplyDefaultInsteadOfNullCoalesceRector\Fixture; | ||
|
||
config('app.name', false) ?? false; | ||
|
||
(new \Illuminate\Http\Request())->input('value', '') ?? ''; | ||
|
||
\Illuminate\Support\Env::get('APP_NAME', '') ?? ''; | ||
|
||
?> |
13 changes: 13 additions & 0 deletions
13
.../Coalesce/ApplyDefaultInsteadOfNullCoalesceRector/Fixture/skip_non_matching_cases.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,13 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\Coalesce\ApplyDefaultInsteadOfNullCoalesceRector\Fixture; | ||
|
||
config1('app.name') ?? false; | ||
|
||
(new \Illuminate\Http\Request())->inputOf('value') ?? ''; | ||
(new \Illuminate\Http\RequestElse())->input('value') ?? ''; | ||
|
||
\Illuminate\Support\Env::getAgain('APP_NAME') ?? ''; | ||
\Illuminate\Support\EnvUtil::get('APP_NAME') ?? ''; | ||
|
||
?> |
12 changes: 12 additions & 0 deletions
12
tests/Rector/Coalesce/ApplyDefaultInsteadOfNullCoalesceRector/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\Coalesce\ApplyDefaultInsteadOfNullCoalesceRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php'); | ||
|
||
$rectorConfig->rule(ApplyDefaultInsteadOfNullCoalesceRector::class); | ||
}; |