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,23 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\BooleanAnd\RepeatedAndNotEqualToNotInArrayRector\Fixture;

final class SkipShortCircuitDependentCall
{
public function run(?string $locale, ?SomeTranslations $translations): bool
{
if (null !== $locale && null !== $translations && null !== $translations->getTranslation($locale)) {
return true;
}

return false;
}
}

final class SomeTranslations
{
public function getTranslation(string $locale): ?string
{
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\BooleanAnd\RepeatedAndNotEqualToNotInArrayRector\Fixture;

final class YodaNullOnVariables
{
public function run($first, $second, $third): bool
{
if (null !== $first && null !== $second && null !== $third) {
return true;
}

return false;
}
}

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\BooleanAnd\RepeatedAndNotEqualToNotInArrayRector\Fixture;

final class YodaNullOnVariables
{
public function run($first, $second, $third): bool
{
if (!in_array(null, [$first, $second, $third], true)) {
return true;
}

return false;
}
}

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

namespace Rector\Tests\CodeQuality\Rector\BooleanOr\RepeatedOrEqualToInArrayRector\Fixture;

final class SkipShortCircuitDependentCall
{
public function run(?string $locale, ?SomeTranslations $translations): bool
{
if (null === $locale || null === $translations || null === $translations->getTranslation($locale)) {
return true;
}

return false;
}
}

final class SomeTranslations
{
public function getTranslation(string $locale): ?string
{
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\BooleanOr\RepeatedOrEqualToInArrayRector\Fixture;

final class YodaNullOnVariables
{
public function run($first, $second, $third): bool
{
if (null === $first || null === $second || null === $third) {
return true;
}

return false;
}
}

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\BooleanOr\RepeatedOrEqualToInArrayRector\Fixture;

final class YodaNullOnVariables
{
public function run($first, $second, $third): bool
{
if (in_array(null, [$first, $second, $third], true)) {
return true;
}

return false;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,26 @@
namespace Rector\CodeQuality\NodeFactory;

use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Variable;
use Rector\CodeQuality\ValueObject\ComparedExprAndValueExpr;
use Rector\NodeAnalyzer\ExprAnalyzer;
use Rector\PhpParser\Comparing\NodeComparator;
use Rector\PhpParser\Node\NodeFactory;

final readonly class InArrayFromRepeatedCompareFactory
{
public function __construct(
private NodeComparator $nodeComparator,
private NodeFactory $nodeFactory
private NodeFactory $nodeFactory,
private ExprAnalyzer $exprAnalyzer
) {
}

/**
* Builds the "$value, [...]" args of an in_array() call from a repeated compare chain,
* once all compared expressions are confirmed equal. Returns null when the chain is too
* short or the compared expressions differ.
* short, the compared expressions differ, or a value expression is not safe to evaluate eagerly.
*
* @param ComparedExprAndValueExpr[] $comparedExprAndValueExprs
* @return Arg[]|null
Expand All @@ -33,7 +37,15 @@ public function createInArrayArgs(array $comparedExprAndValueExprs): ?array

$valueExprs = [];
foreach ($comparedExprAndValueExprs as $comparedExprAndValueExpr) {
$valueExprs[] = $comparedExprAndValueExpr->getValueExpr();
$valueExpr = $comparedExprAndValueExpr->getValueExpr();

// the array literal evaluates every item up front, while the &&/|| chain stops early,
// e.g. null !== $a && null !== $a->get() would call get() on null
if (! $this->isSafeToEvaluateEagerly($valueExpr)) {
return null;
}

$valueExprs[] = $valueExpr;
}

/** @var ComparedExprAndValueExpr $firstComparedExprAndValue */
Expand All @@ -53,4 +65,14 @@ public function createInArrayArgs(array $comparedExprAndValueExprs): ?array

return $this->nodeFactory->createArgs([$firstComparedExprAndValue->getComparedExpr(), $array]);
}

private function isSafeToEvaluateEagerly(Expr $expr): bool
{
// reading a plain variable has no side effect; $$name could evaluate a call
if ($expr instanceof Variable) {
return is_string($expr->name);
}

return ! $this->exprAnalyzer->isDynamicExpr($expr);
}
}
Loading