[PHP 8.0] Improve NullsafeOperatorRector : Skip no direct usage after if in next statement at last (#4565)

* [PHP 8.0] Improve NullsafeOperatorRector : Skip no direct usage after if

* [ci-review] Apply coding standards

Co-authored-by: rector-bot <tomas@getrector.org>
This commit is contained in:
Abdul Malik Ikhsan 2020-11-09 20:14:57 +07:00 committed by GitHub
parent 44063910e6
commit b4fd52021d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 9 deletions

View File

@ -3,6 +3,10 @@ includes:
- vendor/phpstan/phpstan/conf/bleedingEdge.neon
- vendor/symplify/phpstan-extensions/config/config.neon
# see https://github.com/symplify/coding-standard
- vendor/symplify/coding-standard/config/symplify-rules.neon
- vendor/symplify/coding-standard/packages/cognitive-complexity/config/cognitive-complexity-services.neon
services:
-
class: Symplify\CodingStandard\CognitiveComplexity\Rules\FunctionLikeCognitiveComplexityRule

View File

@ -12,7 +12,6 @@ use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\NullsafeMethodCall;
use PhpParser\Node\Expr\NullsafePropertyFetch;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
@ -132,14 +131,6 @@ CODE_SAMPLE
private function processAssign(Assign $assign, Node $prevNode, Node $nextNode): ?Node
{
if (property_exists($nextNode, 'expr')) {
$prevOfPrevNode = $prevNode->getAttribute(AttributeKey::PREVIOUS_NODE);
if (! $prevOfPrevNode instanceof Stmt || $prevOfPrevNode instanceof If_) {
$this->removeNode($prevNode);
$this->removeNode($nextNode);
}
}
if ($assign instanceof Assign && property_exists(
$assign->expr,
self::NAME
@ -160,6 +151,8 @@ CODE_SAMPLE
$nullSafe = $this->getNullSafeOnPrevAssignIsIf($prevAssign, $nextNode, $nullSafe);
}
$this->removeNode($nextNode);
if ($nextNode instanceof Return_) {
$nextNode->expr = $nullSafe;
return $nextNode;

View File

@ -0,0 +1,30 @@
<?php
namespace Rector\Php80\Tests\Rector\If_\NullsafeOperatorRector\Fixture;
class SkipNoDirectUsafeAfterIfInNext
{
public function f($o)
{
$o2 = $o->mayFail1();
if ($o2 === null) {
return null;
}
$mayFail2 = $o2->mayFail2();
if ($mayFail2 === null) {
return null;
}
$mayFail3 = $mayFail2->mayFail3();
if ($mayFail3 === null) {
return null;
}
echo 'STATEMENT HERE';
return $mayFail3->mayFail4();
}
}
?>