Skip to content

Commit

Permalink
[DowngradePhp74][DowngradePhp80] Apply ternary with method_exists on …
Browse files Browse the repository at this point in the history
…DowngradeReflectionGetTypeRector + DowngradeReflectionGetAttributesRector (#1530)

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Dec 20, 2021
1 parent 2a18c59 commit daa4425
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class SomeClass
{
public function run(\ReflectionProperty $reflectionProperty)
{
if (null) {
if (method_exists($reflectionProperty, 'getType') ? $reflectionProperty->getType() : null) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class SomeClass
{
public function run(\ReflectionClass $reflectionClass)
{
if ([]) {
if (method_exists($reflectionClass, 'getAttributes') ? $reflectionClass->getAttributes() : []) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class WithArguments
{
public function run(\ReflectionClass $reflectionClass)
{
if ([]) {
if (method_exists($reflectionClass, 'getAttributes') ? $reflectionClass->getAttributes('someName', 1) : []) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
namespace Rector\DowngradePhp74\Rector\MethodCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Scalar\String_;
use PHPStan\Type\ObjectType;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand Down Expand Up @@ -80,6 +83,19 @@ public function refactor(Node $node): ?Node
return null;
}

return $this->nodeFactory->createNull();
// avoid infinite loop
$createdByRule = $node->getAttribute(AttributeKey::CREATED_BY_RULE);
if ($createdByRule === self::class) {
return null;
}

$node->setAttribute(AttributeKey::CREATED_BY_RULE, self::class);
$args = [new Arg($node->var), new Arg(new String_('getType'))];

return new Ternary(
$this->nodeFactory->createFuncCall('method_exists', $args),
$node,
$this->nodeFactory->createNull()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
namespace Rector\DowngradePhp80\Rector\MethodCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Scalar\String_;
use PHPStan\Type\ObjectType;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand Down Expand Up @@ -74,6 +78,15 @@ public function refactor(Node $node): ?Node
return null;
}

return new Array_([]);
// avoid infinite loop
$createdByRule = $node->getAttribute(AttributeKey::CREATED_BY_RULE);
if ($createdByRule === self::class) {
return null;
}

$node->setAttribute(AttributeKey::CREATED_BY_RULE, self::class);
$args = [new Arg($node->var), new Arg(new String_('getAttributes'))];

return new Ternary($this->nodeFactory->createFuncCall('method_exists', $args), $node, new Array_([]));
}
}

0 comments on commit daa4425

Please sign in to comment.