Skip to content

Commit

Permalink
disallow undefined property in coalesce for backwards compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
rajyan committed Apr 3, 2022
1 parent a4f853c commit 59d89a5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/Analyser/MutatingScope.php
Expand Up @@ -4152,7 +4152,9 @@ public function setAllowedUndefinedExpression(Expr $expr, bool $isAllowed): self
{
$exprString = $this->getNodeKey($expr);
$currentlyAllowedUndefinedExpressions = $this->currentlyAllowedUndefinedExpressions;
$currentlyAllowedUndefinedExpressions[$exprString] = $isAllowed;
if (!array_key_exists($exprString, $currentlyAllowedUndefinedExpressions)) {
$currentlyAllowedUndefinedExpressions[$exprString] = $isAllowed;
}

return $this->scopeFactory->create(
$this->context,
Expand Down
7 changes: 6 additions & 1 deletion src/Analyser/NodeScopeResolver.php
Expand Up @@ -2290,7 +2290,12 @@ function (MutatingScope $scope) use ($expr, $nodeCallback, $context): Expression
);
} elseif ($expr instanceof Coalesce) {
$nonNullabilityResult = $this->ensureNonNullability($scope, $expr->left, false);
$scope = $this->lookForEnterAllowedUndefinedVariable($nonNullabilityResult->getScope(), $expr->left, true);
if ($expr->left instanceof PropertyFetch || $expr->left instanceof Expr\NullsafePropertyFetch || $expr->left instanceof StaticPropertyFetch) {
$scope = $nonNullabilityResult->getScope()->setAllowedUndefinedExpression($expr->left, false);
} else {
$scope = $nonNullabilityResult->getScope();
}
$scope = $this->lookForEnterAllowedUndefinedVariable($scope, $expr->left, true);
$result = $this->processExprNode($expr->left, $scope, $nodeCallback, $context->enterDeep());
$hasYield = $result->hasYield();
$throwPoints = $result->getThrowPoints();
Expand Down

0 comments on commit 59d89a5

Please sign in to comment.