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

[DowngradePhp80] Handle short circuit on DowngradeNullsafeToTernaryOperatorRector #254

Merged
merged 7 commits into from
Dec 1, 2024
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
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\NullsafeMethodCall\DowngradeNullsafeToTernaryOperatorRector\Fixture;

final class ShortCircuit
{
public function run(bool $param)
{
return $this->clock?->now()->format('U.u');
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp80\Rector\NullsafeMethodCall\DowngradeNullsafeToTernaryOperatorRector\Fixture;

final class ShortCircuit
{
public function run(bool $param)
{
return ($nullsafeVariable1 = $this->clock) ? $nullsafeVariable1->now()->format('U.u') : null;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\NullsafeMethodCall\DowngradeNullsafeToTernaryOperatorRector\Fixture;

final class ShortCircuit2
{
public function run(bool $param)
{
return $this->clock?->now()->format;
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp80\Rector\NullsafeMethodCall\DowngradeNullsafeToTernaryOperatorRector\Fixture;

final class ShortCircuit2
{
public function run(bool $param)
{
return ($nullsafeVariable1 = $this->clock) ? $nullsafeVariable1->now()->format : null;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\NullsafeMethodCall\DowngradeNullsafeToTernaryOperatorRector\Fixture;

final class ShortCircuit3
{
public function run(bool $param)
{
return $this->clock?->now->format;
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp80\Rector\NullsafeMethodCall\DowngradeNullsafeToTernaryOperatorRector\Fixture;

final class ShortCircuit3
{
public function run(bool $param)
{
return ($nullsafeVariable1 = $this->clock) ? $nullsafeVariable1->now->format : null;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\NullsafeMethodCall\DowngradeNullsafeToTernaryOperatorRector\Fixture;

final class ShortCircuit4
{
public function run(bool $param)
{
return $this->clock?->now->format('U.u');
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp80\Rector\NullsafeMethodCall\DowngradeNullsafeToTernaryOperatorRector\Fixture;

final class ShortCircuit4
{
public function run(bool $param)
{
return ($nullsafeVariable1 = $this->clock) ? $nullsafeVariable1->now->format('U.u') : null;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\NullsafeMethodCall\DowngradeNullsafeToTernaryOperatorRector\Fixture;

final class ShortCircuit5
{
public function run(bool $param)
{
return $this->clock?->now()->format;
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp80\Rector\NullsafeMethodCall\DowngradeNullsafeToTernaryOperatorRector\Fixture;

final class ShortCircuit5
{
public function run(bool $param)
{
return ($nullsafeVariable1 = $this->clock) ? $nullsafeVariable1->now()->format : null;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,59 @@ public function getRuleDefinition(): RuleDefinition
*/
public function getNodeTypes(): array
{
return [NullsafeMethodCall::class, NullsafePropertyFetch::class];
return [MethodCall::class, PropertyFetch::class, NullsafeMethodCall::class, NullsafePropertyFetch::class];
}

/**
* @param NullsafeMethodCall|NullsafePropertyFetch $node
* @param MethodCall|NullsafeMethodCall|NullsafePropertyFetch $node
*/
public function refactor(Node $node): Ternary
public function refactor(Node $node): ?Ternary
{
if ($node instanceof MethodCall || $node instanceof PropertyFetch) {
if ($node->var instanceof NullsafeMethodCall || $node->var instanceof NullsafePropertyFetch) {
$nullsafeVariable = $this->createNullsafeVariable();

$assign = new Assign($nullsafeVariable, $node->var->var);
if ($node instanceof MethodCall) {
if ($node->var instanceof NullsafeMethodCall) {
$methodCallOrPropertyFetch = new MethodCall(new MethodCall(
$nullsafeVariable,
$node->var->name,
$node->var->args
), $node->name, $node->args);
return new Ternary($assign, $methodCallOrPropertyFetch, $this->nodeFactory->createNull());
}

$methodCallOrPropertyFetch = new MethodCall(new PropertyFetch(
$nullsafeVariable,
$node->var->name
), $node->name, $node->args);
return new Ternary($assign, $methodCallOrPropertyFetch, $this->nodeFactory->createNull());
}

if ($node->var instanceof NullsafeMethodCall) {
$methodCallOrPropertyFetch = new PropertyFetch(new MethodCall(
$nullsafeVariable,
$node->var->name,
$node->var->args
), $node->name);
return new Ternary($assign, $methodCallOrPropertyFetch, $this->nodeFactory->createNull());
}

$methodCallOrPropertyFetch = new PropertyFetch(new PropertyFetch(
$nullsafeVariable,
$node->var->name
), $node->name);
return new Ternary($assign, $methodCallOrPropertyFetch, $this->nodeFactory->createNull());
}

return null;
}

$nullsafeVariable = $this->createNullsafeVariable();

$methodCallOrPropertyFetch = $node instanceof NullsafeMethodCall
? new MethodCall($nullsafeVariable, $node->name, $node->getArgs())
? new MethodCall($nullsafeVariable, $node->name, $node->args)
: new PropertyFetch($nullsafeVariable, $node->name);

$assign = new Assign($nullsafeVariable, $node->var);
Expand Down
Loading