Skip to content
Merged
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
105 changes: 56 additions & 49 deletions src/Analyser/NullsafeOperatorHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,80 +4,87 @@

use PhpParser\Node\Expr;
use PHPStan\Type\TypeCombinator;
use function array_reverse;

final class NullsafeOperatorHelper
{

public static function getNullsafeShortcircuitedExprRespectingScope(Scope $scope, Expr $expr): Expr
{
$shortcircuitedExpr = self::getNullsafeShortcircuitedExpr($expr);
if ($shortcircuitedExpr === $expr) {
// No nullsafe operator anywhere in the expression - the result is $expr
// either way, so skip asking the scope for the expression's type.
return $expr;
}

if (!TypeCombinator::containsNull($scope->getType($expr))) {
// We're in most likely in context of a null-safe operator ($scope->moreSpecificType is defined for $expr)
// Modifying the expression would not bring any value or worse ruin the context information
return $expr;
}

return self::getNullsafeShortcircuitedExpr($expr);
return $shortcircuitedExpr;
}

/**
* @internal Use NullsafeOperatorHelper::getNullsafeShortcircuitedExprRespectingScope
*/
public static function getNullsafeShortcircuitedExpr(Expr $expr): Expr
{
if ($expr instanceof Expr\NullsafeMethodCall) {
return new Expr\MethodCall(self::getNullsafeShortcircuitedExpr($expr->var), $expr->name, $expr->args);
}

if ($expr instanceof Expr\MethodCall) {
$var = self::getNullsafeShortcircuitedExpr($expr->var);
if ($expr->var === $var) {
return $expr;
// Collect the chain of chained-access wrappers (outermost first) walking inward.
$chain = [];
$current = $expr;

while (true) {
if (
$current instanceof Expr\NullsafeMethodCall
|| $current instanceof Expr\MethodCall
|| $current instanceof Expr\ArrayDimFetch
|| $current instanceof Expr\NullsafePropertyFetch
|| $current instanceof Expr\PropertyFetch
) {
$chain[] = $current;
$current = $current->var;
} elseif (
($current instanceof Expr\StaticCall || $current instanceof Expr\StaticPropertyFetch)
&& $current->class instanceof Expr
) {
$chain[] = $current;
$current = $current->class;
} else {
break;
}

return new Expr\MethodCall($var, $expr->name, $expr->getArgs());
}

if ($expr instanceof Expr\StaticCall && $expr->class instanceof Expr) {
$class = self::getNullsafeShortcircuitedExpr($expr->class);
if ($expr->class === $class) {
return $expr;
// Rebuild from innermost outward, replacing Nullsafe* nodes as we go.
$result = $current;
$changed = false;

foreach (array_reverse($chain) as $node) {
if ($node instanceof Expr\NullsafeMethodCall) {
$result = new Expr\MethodCall($result, $node->name, $node->args);
$changed = true;
} elseif ($node instanceof Expr\NullsafePropertyFetch) {
$result = new Expr\PropertyFetch($result, $node->name);
$changed = true;
} elseif (!$changed) {
// Nothing has changed yet — keep the original node to preserve identity.
$result = $node;
} elseif ($node instanceof Expr\MethodCall) {
$result = new Expr\MethodCall($result, $node->name, $node->getArgs());
} elseif ($node instanceof Expr\ArrayDimFetch) {
$result = new Expr\ArrayDimFetch($result, $node->dim);
} elseif ($node instanceof Expr\PropertyFetch) {
$result = new Expr\PropertyFetch($result, $node->name);
} elseif ($node instanceof Expr\StaticCall) {
$result = new Expr\StaticCall($result, $node->name, $node->getArgs());
} elseif ($node instanceof Expr\StaticPropertyFetch) {
$result = new Expr\StaticPropertyFetch($result, $node->name);
}

return new Expr\StaticCall($class, $expr->name, $expr->getArgs());
}

if ($expr instanceof Expr\ArrayDimFetch) {
$var = self::getNullsafeShortcircuitedExpr($expr->var);
if ($expr->var === $var) {
return $expr;
}

return new Expr\ArrayDimFetch($var, $expr->dim);
}

if ($expr instanceof Expr\NullsafePropertyFetch) {
return new Expr\PropertyFetch(self::getNullsafeShortcircuitedExpr($expr->var), $expr->name);
}

if ($expr instanceof Expr\PropertyFetch) {
$var = self::getNullsafeShortcircuitedExpr($expr->var);
if ($expr->var === $var) {
return $expr;
}

return new Expr\PropertyFetch($var, $expr->name);
}

if ($expr instanceof Expr\StaticPropertyFetch && $expr->class instanceof Expr) {
$class = self::getNullsafeShortcircuitedExpr($expr->class);
if ($expr->class === $class) {
return $expr;
}

return new Expr\StaticPropertyFetch($class, $expr->name);
}

return $expr;
return $result;
}

}
Loading