Skip to content

Commit

Permalink
allow undefined expression for ObjectWithoutClassType property fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
rajyan committed Apr 18, 2022
1 parent 4cbd669 commit 1db43fc
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
use PHPStan\Type\NeverType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\StaticType;
use PHPStan\Type\StaticTypeFactory;
use PHPStan\Type\StringType;
Expand Down Expand Up @@ -2309,9 +2310,12 @@ function (MutatingScope $scope) use ($expr, $nodeCallback, $context): Expression
static fn (): MutatingScope => $rightResult->getScope()->filterByFalseyValue($expr),
);
} elseif ($expr instanceof Coalesce) {
// backwards compatibility: coalesce doesn't allow dynamic properties
$nonNullabilityResult = $this->ensureNonNullability($scope, $expr->left, false);
if ($expr->left instanceof PropertyFetch || $expr->left instanceof Expr\NullsafePropertyFetch || $expr->left instanceof StaticPropertyFetch) {
// backwards compatibility: coalesce doesn't allow dynamic properties
if ($expr->left instanceof PropertyFetch || $expr->left instanceof Expr\NullsafePropertyFetch) {
$propertyHolderType = $scope->getType($expr->left->var);
$condScope = $nonNullabilityResult->getScope()->setAllowedUndefinedExpression($expr->left, $propertyHolderType->isSuperTypeOf(new ObjectWithoutClassType())->yes());
} elseif ($expr->left instanceof StaticPropertyFetch) {
$condScope = $nonNullabilityResult->getScope()->setAllowedUndefinedExpression($expr->left, false);
} else {
$condScope = $nonNullabilityResult->getScope();
Expand Down

0 comments on commit 1db43fc

Please sign in to comment.