Skip to content
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,39 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\If_\ConsecutiveNullCompareReturnsToNullCoalesceQueueRector\Fixture;

class ReturnNewObject
{
protected ?\stdClass $prop1 = null;
protected ?\stdClass $prop2 = null;

public function getProp(): \stdClass
{
if ($this->prop1 !== null) {
return $this->prop1;
}
if ($this->prop2 !== null) {
return $this->prop2;
}
return new \stdClass();
}
}

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\If_\ConsecutiveNullCompareReturnsToNullCoalesceQueueRector\Fixture;

class ReturnNewObject
{
protected ?\stdClass $prop1 = null;
protected ?\stdClass $prop2 = null;

public function getProp(): \stdClass
{
return ($this->prop1 ?? $this->prop2) ?? new \stdClass();
}
}

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

namespace Rector\Tests\CodeQuality\Rector\If_\ConsecutiveNullCompareReturnsToNullCoalesceQueueRector\Fixture;

class SkipIndirectReturn
{
protected ?\stdClass $prop1 = null;
protected ?\stdClass $prop2 = null;

public function getProp()
{
if ($this->prop1 !== null) {
return $this->prop1;
}
if ($this->prop2 !== null) {
return $this->prop2;
}
echo 'hi';
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\If_\ConsecutiveNullCompareReturnsToNullCoalesceQueueRector\Fixture;

class SkipNoReturn
{
protected ?\stdClass $prop1 = null;
protected ?\stdClass $prop2 = null;

public function getProp()
{
if ($this->prop1 !== null) {
return $this->prop1;
}
if ($this->prop2 !== null) {
return $this->prop2;
}
echo 'hi';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\If_\ConsecutiveNullCompareReturnsToNullCoalesceQueueRector\Fixture;

class SkipNoReturn2
{
protected ?\stdClass $prop1 = null;
protected ?\stdClass $prop2 = null;

public function getProp()
{
if ($this->prop1 !== null) {
return $this->prop1;
}
if ($this->prop2 !== null) {
return $this->prop2;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ public function refactor(Node $node): ?Node
continue;
}

if (! isset($node->stmts[$key + 1])) {
return null;
}

$coalescingExprs[] = $comparedExpr;
$ifKeys[] = $key;
}
Expand All @@ -105,8 +109,9 @@ public function refactor(Node $node): ?Node
}

// remove last return null
$throwExpr = null;
$appendExpr = null;
$hasChanged = false;
$originalStmts = $node->stmts;
foreach ($node->stmts as $key => $stmt) {
if (in_array($key, $ifKeys, true)) {
unset($node->stmts[$key]);
Expand All @@ -121,19 +126,27 @@ public function refactor(Node $node): ?Node

if ($stmt instanceof Throw_) {
unset($node->stmts[$key]);
$throwExpr = new ExprThrow_($stmt->expr);
$appendExpr = new ExprThrow_($stmt->expr);

continue;
}

if (! $this->isReturnNull($stmt)) {
continue;
if ($stmt instanceof Return_ && $stmt->expr instanceof Expr) {
unset($node->stmts[$key]);
$appendExpr = $stmt->expr;

continue;
}

$node->stmts = $originalStmts;
return $node;
}

unset($node->stmts[$key]);
}

$node->stmts[] = $this->createCealesceReturn($coalescingExprs, $throwExpr);
$node->stmts[] = $this->createCealesceReturn($coalescingExprs, $appendExpr);

return $node;
}
Expand All @@ -159,7 +172,7 @@ private function isReturnNull(Stmt $stmt): bool
/**
* @param Expr[] $coalescingExprs
*/
private function createCealesceReturn(array $coalescingExprs, ?Expr $throwExpr): Return_
private function createCealesceReturn(array $coalescingExprs, ?Expr $appendExpr): Return_
{
/** @var Expr $leftExpr */
$leftExpr = array_shift($coalescingExprs);
Expand All @@ -173,8 +186,8 @@ private function createCealesceReturn(array $coalescingExprs, ?Expr $throwExpr):
$coalesce = new Coalesce($coalesce, $coalescingExpr);
}

if ($throwExpr instanceof Expr) {
return new Return_(new Coalesce($coalesce, $throwExpr));
if ($appendExpr instanceof Expr) {
return new Return_(new Coalesce($coalesce, $appendExpr));
}

return new Return_($coalesce);
Expand Down