[DowngradePhp70] Allow property fetch and static property fetch on DowngradeNullCoalesceRector (#540)

Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
Abdul Malik Ikhsan 2021-07-29 14:16:05 +07:00 committed by GitHub
parent cdad000d81
commit 6e73a1f069
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 95 additions and 8 deletions

View File

@ -0,0 +1,27 @@
<?php
namespace Rector\Tests\DowngradePhp70\Rector\Coalesce\DowngradeNullCoalesceRector\Fixture;
class SomePropertyFetch
{
public function run()
{
$username = $this->property ?? 'value';
}
}
?>
-----
<?php
namespace Rector\Tests\DowngradePhp70\Rector\Coalesce\DowngradeNullCoalesceRector\Fixture;
class SomePropertyFetch
{
public function run()
{
$username = isset($this->property) ? $this->property : 'value';
}
}
?>

View File

@ -0,0 +1,27 @@
<?php
namespace Rector\Tests\DowngradePhp70\Rector\Coalesce\DowngradeNullCoalesceRector\Fixture;
class SomeStaticPropertyFetch
{
public function run()
{
$username = self::$property ?? 'value';
}
}
?>
-----
<?php
namespace Rector\Tests\DowngradePhp70\Rector\Coalesce\DowngradeNullCoalesceRector\Fixture;
class SomeStaticPropertyFetch
{
public function run()
{
$username = isset(self::$property) ? self::$property : 'value';
}
}
?>

View File

@ -5,12 +5,11 @@ declare(strict_types=1);
namespace Rector\DowngradePhp70\Rector\Coalesce;
use PhpParser\Node;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\BinaryOp\Coalesce;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\Isset_;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Expr\Variable;
use Rector\Core\NodeAnalyzer\CoalesceAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@ -22,6 +21,10 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
*/
final class DowngradeNullCoalesceRector extends AbstractRector
{
public function __construct(private CoalesceAnalyzer $coalesceAnalyzer)
{
}
/**
* @return array<class-string<Node>>
*/
@ -56,7 +59,7 @@ CODE_SAMPLE
$if = $node->left;
$else = $node->right;
if ($if instanceof Variable || $if instanceof ArrayDimFetch) {
if ($this->coalesceAnalyzer->hasIssetableLeft($node)) {
$cond = new Isset_([$if]);
} else {
$cond = new NotIdentical($if, $this->nodeFactory->createNull());

View File

@ -9,12 +9,10 @@ use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\BinaryOp\Coalesce;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\Isset_;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Throw_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\If_;
use Rector\Core\NodeAnalyzer\CoalesceAnalyzer;
use Rector\Core\NodeManipulator\IfManipulator;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@ -28,7 +26,8 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
final class DowngradeThrowExprRector extends AbstractRector
{
public function __construct(
private IfManipulator $ifManipulator
private IfManipulator $ifManipulator,
private CoalesceAnalyzer $coalesceAnalyzer
) {
}
@ -110,7 +109,7 @@ CODE_SAMPLE
return null;
}
if (! $coalesce->left instanceof Variable && ! $coalesce->left instanceof PropertyFetch && ! $coalesce->left instanceof StaticPropertyFetch) {
if (! $this->coalesceAnalyzer->hasIssetableLeft($coalesce)) {
return null;
}

View File

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace Rector\Core\NodeAnalyzer;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\BinaryOp\Coalesce;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
final class CoalesceAnalyzer
{
/**
* @var array<class-string<Expr>>
*/
private const ISSETABLE_EXPR = [
Variable::class,
ArrayDimFetch::class,
PropertyFetch::class,
StaticPropertyFetch::class,
];
public function hasIssetableLeft(Coalesce $coalesce): bool
{
$leftClass = $coalesce->left::class;
return in_array($leftClass, self::ISSETABLE_EXPR, true);
}
}