Skip to content

Commit

Permalink
Revert "Fix variable certainty for ArrayDimFetch in falsey isset cont…
Browse files Browse the repository at this point in the history
…ext"

This reverts commit 3de3c85.
  • Loading branch information
ondrejmirtes committed Nov 5, 2023
1 parent a822d1c commit 632582d
Show file tree
Hide file tree
Showing 11 changed files with 17 additions and 379 deletions.
7 changes: 0 additions & 7 deletions src/Analyser/EnsuredNonNullabilityResultExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace PHPStan\Analyser;

use PhpParser\Node\Expr;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Type;

class EnsuredNonNullabilityResultExpression
Expand All @@ -13,7 +12,6 @@ public function __construct(
private Expr $expression,
private Type $originalType,
private Type $originalNativeType,
private TrinaryLogic $certainty,
)
{
}
Expand All @@ -33,9 +31,4 @@ public function getOriginalNativeType(): Type
return $this->originalNativeType;
}

public function getCertainty(): TrinaryLogic
{
return $this->certainty;
}

}
18 changes: 3 additions & 15 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -2341,10 +2341,6 @@ public function isSpecified(Expr $node): bool
/** @api */
public function hasExpressionType(Expr $node): TrinaryLogic
{
if ($node instanceof Variable && is_string($node->name)) {
return $this->hasVariableType($node->name);
}

$exprString = $this->getNodeKey($node);
if (!isset($this->expressionTypes[$exprString])) {
return TrinaryLogic::createNo();
Expand Down Expand Up @@ -3437,7 +3433,7 @@ public function unsetExpression(Expr $expr): self
return $scope->invalidateExpression($expr);
}

public function specifyExpressionType(Expr $expr, Type $type, Type $nativeType, ?TrinaryLogic $certainty = null): self
public function specifyExpressionType(Expr $expr, Type $type, Type $nativeType): self
{
if ($expr instanceof ConstFetch) {
$loweredConstName = strtolower($expr->name->toString());
Expand Down Expand Up @@ -3471,31 +3467,23 @@ public function specifyExpressionType(Expr $expr, Type $type, Type $nativeType,
if ($dimType instanceof ConstantIntegerType) {
$types[] = new StringType();
}

$scope = $scope->specifyExpressionType(
$expr->var,
TypeCombinator::intersect(
TypeCombinator::intersect($exprVarType, TypeCombinator::union(...$types)),
new HasOffsetValueType($dimType, $type),
),
$scope->getNativeType($expr->var),
$certainty,
);
}
}
}

if ($certainty === null) {
$certainty = TrinaryLogic::createYes();
} elseif ($certainty->no()) {
throw new ShouldNotHappenException();
}

$exprString = $this->getNodeKey($expr);
$expressionTypes = $scope->expressionTypes;
$expressionTypes[$exprString] = new ExpressionTypeHolder($expr, $type, $certainty);
$expressionTypes[$exprString] = ExpressionTypeHolder::createYes($expr, $type);
$nativeTypes = $scope->nativeExpressionTypes;
$nativeTypes[$exprString] = new ExpressionTypeHolder($expr, $nativeType, $certainty);
$nativeTypes[$exprString] = ExpressionTypeHolder::createYes($expr, $nativeType);

return $this->scopeFactory->create(
$this->context,
Expand Down
13 changes: 2 additions & 11 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1753,22 +1753,14 @@ private function ensureShallowNonNullability(MutatingScope $scope, Scope $origin
if ($isNull->yes()) {
return new EnsuredNonNullabilityResult($scope, []);
}

// keep certainty
$certainty = TrinaryLogic::createYes();
$hasExpressionType = $originalScope->hasExpressionType($exprToSpecify);
if (!$hasExpressionType->no()) {
$certainty = $hasExpressionType;
}

$exprTypeWithoutNull = TypeCombinator::removeNull($exprType);
if ($exprType->equals($exprTypeWithoutNull)) {
$originalExprType = $originalScope->getType($exprToSpecify);
if (!$originalExprType->equals($exprTypeWithoutNull)) {
$originalNativeType = $originalScope->getNativeType($exprToSpecify);

return new EnsuredNonNullabilityResult($scope, [
new EnsuredNonNullabilityResultExpression($exprToSpecify, $originalExprType, $originalNativeType, $certainty),
new EnsuredNonNullabilityResultExpression($exprToSpecify, $originalExprType, $originalNativeType),
]);
}
return new EnsuredNonNullabilityResult($scope, []);
Expand All @@ -1784,7 +1776,7 @@ private function ensureShallowNonNullability(MutatingScope $scope, Scope $origin
return new EnsuredNonNullabilityResult(
$scope,
[
new EnsuredNonNullabilityResultExpression($exprToSpecify, $exprType, $nativeType, $certainty),
new EnsuredNonNullabilityResultExpression($exprToSpecify, $exprType, $nativeType),
],
);
}
Expand Down Expand Up @@ -1814,7 +1806,6 @@ private function revertNonNullability(MutatingScope $scope, array $specifiedExpr
$specifiedExpressionResult->getExpression(),
$specifiedExpressionResult->getOriginalType(),
$specifiedExpressionResult->getOriginalNativeType(),
$specifiedExpressionResult->getCertainty(),
);
}

Expand Down
32 changes: 12 additions & 20 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -717,14 +717,11 @@ public function specifyTypesInCondition(

$types = null;
foreach ($vars as $var) {
$type = new SpecifiedTypes();

if ($var instanceof Expr\Variable && is_string($var->name)) {
if ($scope->hasVariableType($var->name)->no()) {
return new SpecifiedTypes([], [], false, [], $rootExpr);
}
}

if (
$var instanceof ArrayDimFetch
&& $var->dim !== null
Expand All @@ -741,32 +738,36 @@ public function specifyTypesInCondition(
$scope,
$rootExpr,
);
} else {
$type = new SpecifiedTypes();
}

$type = $type->unionWith(
$this->create($var, new NullType(), TypeSpecifierContext::createFalse(), false, $scope, $rootExpr),
);
} else {
$type = $this->create($var, new NullType(), TypeSpecifierContext::createFalse(), false, $scope, $rootExpr);
}

if (
$var instanceof PropertyFetch
&& $var->name instanceof Node\Identifier
) {
$type = $this->create($var->var, new IntersectionType([
$type = $type->unionWith($this->create($var->var, new IntersectionType([
new ObjectWithoutClassType(),
new HasPropertyType($var->name->toString()),
]), TypeSpecifierContext::createTruthy(), false, $scope, $rootExpr);
]), TypeSpecifierContext::createTruthy(), false, $scope, $rootExpr));
} elseif (
$var instanceof StaticPropertyFetch
&& $var->class instanceof Expr
&& $var->name instanceof Node\VarLikeIdentifier
) {
$type = $this->create($var->class, new IntersectionType([
$type = $type->unionWith($this->create($var->class, new IntersectionType([
new ObjectWithoutClassType(),
new HasPropertyType($var->name->toString()),
]), TypeSpecifierContext::createTruthy(), false, $scope, $rootExpr);
]), TypeSpecifierContext::createTruthy(), false, $scope, $rootExpr));
}

$type = $type->unionWith(
$this->create($var, new NullType(), TypeSpecifierContext::createFalse(), false, $scope, $rootExpr),
);

if ($types === null) {
$types = $type;
} else {
Expand All @@ -791,15 +792,6 @@ public function specifyTypesInCondition(
} elseif (
$expr instanceof Expr\Empty_
) {
if (!$scope instanceof MutatingScope) {
throw new ShouldNotHappenException();
}

$isset = $scope->issetCheck($expr->expr, static fn () => true);
if ($isset === false) {
return new SpecifiedTypes();
}

return $this->specifyTypesInCondition($scope, new BooleanOr(
new Expr\BooleanNot(new Expr\Isset_([$expr->expr])),
new Expr\BooleanNot($expr->expr),
Expand Down
3 changes: 0 additions & 3 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1362,9 +1362,6 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/impure-connection-fns.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-9963.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-9995.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/falsey-isset-certainty.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/falsey-empty-certainty.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7291.php');
}

/**
Expand Down
25 changes: 0 additions & 25 deletions tests/PHPStan/Analyser/data/bug-7291.php

This file was deleted.

69 changes: 0 additions & 69 deletions tests/PHPStan/Analyser/data/falsey-empty-certainty.php

This file was deleted.

Loading

0 comments on commit 632582d

Please sign in to comment.