Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DowngradePhp74][DowngradePhp80] Apply ternary with method_exists on DowngradeReflectionGetTypeRector + DowngradeReflectionGetAttributesRector #1530

Merged
merged 6 commits into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -7,6 +7,7 @@
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\StaticCall;
use Rector\Core\Rector\AbstractRector;
use Rector\DowngradePhp73\Tokenizer\FollowedByCommaAnalyzer;
Expand Down Expand Up @@ -65,11 +66,11 @@ public function __construct(string $value)
*/
public function getNodeTypes(): array
{
return [FuncCall::class, MethodCall::class, StaticCall::class, Node\Expr\New_::class];
return [FuncCall::class, MethodCall::class, StaticCall::class, New_::class];
}

/**
* @param FuncCall|MethodCall|StaticCall $node
* @param FuncCall|MethodCall|StaticCall|New_ $node
*/
public function refactor(Node $node): ?Node
{
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_([]));
}
}