Skip to content

Commit

Permalink
[Php70] Do not take side effect on IfToSpaceshipRector (#1696)
Browse files Browse the repository at this point in the history
* [Php70] Do not take side effect on IfToSpaceshipRector

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* Fixed 🎉

* [ci-review] Rector Rectify

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Jan 18, 2022
1 parent 9d781bb commit 71719b5
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Rector\Tests\Php70\Rector\If_\IfToSpaceshipRector\SideEffect;

class SideEffect {

public static function foo( $param ) {
if ( 0 === strpos( $param, '/*' ) ) {
$foo = 1;
}

$foo = 2;
return $foo;
}

private static function rsort( $a, $b ) {
if ( $a['bar'] === $b['bar'] ) {
return 0;
} else {
return ( $a['bar'] > $b['bar'] ) ? -1 : 1;
}
}

}

?>
-----
<?php

namespace Rector\Tests\Php70\Rector\If_\IfToSpaceshipRector\SideEffect;

class SideEffect {

public static function foo( $param ) {
if ( 0 === strpos( $param, '/*' ) ) {
$foo = 1;
}

$foo = 2;
return $foo;
}

private static function rsort( $a, $b ) {
return $a['bar'] <=> $b['bar'];
}

}

?>
16 changes: 9 additions & 7 deletions rules/Php70/Rector/If_/IfToSpaceshipRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public function provideMinPhpVersion(): int

private function processReturnSpaceship(Expr $firstValue, Expr $secondValue): Return_
{
if ($this->nextNode !== null) {
if ($this->nextNode instanceof Return_) {
$this->removeNode($this->nextNode);
}

Expand All @@ -169,11 +169,11 @@ private function matchOnEqualFirstValueAndSecondValue(If_ $if): void
if ($if->else !== null) {
$this->processElse($if->else);
} else {
$this->nextNode = $if->getAttribute(AttributeKey::NEXT_NODE);
if ($this->nextNode instanceof Return_ && $this->nextNode->expr instanceof Ternary) {
$nextNode = $if->getAttribute(AttributeKey::NEXT_NODE);
if ($nextNode instanceof Return_ && $nextNode->expr instanceof Ternary) {
/** @var Ternary $ternary */
$ternary = $this->nextNode->expr;
$this->processTernary($ternary);
$ternary = $nextNode->expr;
$this->processTernary($ternary, $nextNode);
}
}
}
Expand Down Expand Up @@ -224,11 +224,11 @@ private function processElse(Else_ $else): void
/** @var Return_ $returnNode */
$returnNode = $else->stmts[0];
if ($returnNode->expr instanceof Ternary) {
$this->processTernary($returnNode->expr);
$this->processTernary($returnNode->expr, null);
}
}

private function processTernary(Ternary $ternary): void
private function processTernary(Ternary $ternary, ?Return_ $return): void
{
if ($ternary->cond instanceof Smaller) {
$this->firstValue = $ternary->cond->left;
Expand All @@ -239,6 +239,7 @@ private function processTernary(Ternary $ternary): void
}

$this->onGreater = $this->valueResolver->getValue($ternary->else);
$this->nextNode = $return;
} elseif ($ternary->cond instanceof Greater) {
$this->firstValue = $ternary->cond->right;
$this->secondValue = $ternary->cond->left;
Expand All @@ -248,6 +249,7 @@ private function processTernary(Ternary $ternary): void
}

$this->onSmaller = $this->valueResolver->getValue($ternary->else);
$this->nextNode = $return;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Rector\Core\Rector\AbstractRector;
use Rector\Privatization\NodeFactory\ClassConstantFactory;
use Rector\Privatization\NodeReplacer\PropertyFetchWithConstFetchReplacer;
use Symplify\PHPStanRules\Reflection\PropertyAnalyzer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand Down Expand Up @@ -135,10 +134,6 @@ private function shouldSkip(Property $property): bool
return true;
}

if ($this->isObjectType($classLike, new ObjectType('PHP_CodeSniffer\Sniffs\Sniff'))) {
return true;
}

return false;
return $this->isObjectType($classLike, new ObjectType('PHP_CodeSniffer\Sniffs\Sniff'));
}
}

0 comments on commit 71719b5

Please sign in to comment.