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
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ parameters:
-
rawMessage: Casting to string something that's already string.
identifier: cast.useless
count: 1
count: 2
path: src/Analyser/MutatingScope.php

-
Expand Down
10 changes: 5 additions & 5 deletions src/Analyser/ExprHandler/BooleanAndHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ public function specifyTypes(TypeSpecifier $typeSpecifier, Scope $scope, Expr $e
if ($context->true()) {
$types = $leftTypes->unionWith($rightTypes);
} else {
$leftNormalized = $leftTypes->normalize($scope);
$rightNormalized = $rightTypes->normalize($rightScope);
$types = $leftNormalized->intersectWith($rightNormalized);
$leftNormalized = $this->conditionalExpressionHolderHelper->toSureTypes($leftTypes, $scope);
$rightNormalized = $this->conditionalExpressionHolderHelper->toSureTypes($rightTypes, $rightScope);
$types = $leftTypes->intersectWith($rightTypes);
$types = $this->conditionalExpressionHolderHelper->augmentDisjunctionTypes($scope, $rightScope, $leftNormalized, $rightNormalized, $expr->left, $expr->right, false, $types);
}
if ($context->false()) {
Expand Down Expand Up @@ -146,10 +146,10 @@ public function specifyTypes(TypeSpecifier $typeSpecifier, Scope $scope, Expr $e
$rightCondTypes = new SpecifiedTypes($truthyRightTypes->getSureNotTypes(), $truthyRightTypes->getSureTypes());
}
}
$result = new SpecifiedTypes(
$result = (new SpecifiedTypes(
$types->getSureTypes(),
$types->getSureNotTypes(),
);
))->withAlternativeTypesOf($types);
if ($types->shouldOverwrite()) {
$result = $result->setAlwaysOverwriteTypes();
}
Expand Down
20 changes: 10 additions & 10 deletions src/Analyser/ExprHandler/BooleanOrHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,16 @@ public function specifyTypes(TypeSpecifier $typeSpecifier, Scope $scope, Expr $e
if (
$scope->getType($expr->left)->toBoolean()->isFalse()->yes()
) {
$types = $rightTypes->normalize($rightScope);
$types = $this->conditionalExpressionHolderHelper->toSureTypes($rightTypes, $rightScope);
} elseif (
$scope->getType($expr->left)->toBoolean()->isTrue()->yes()
|| $scope->getType($expr->right)->toBoolean()->isFalse()->yes()
) {
$types = $leftTypes->normalize($scope);
$types = $this->conditionalExpressionHolderHelper->toSureTypes($leftTypes, $scope);
} else {
$leftNormalized = $leftTypes->normalize($scope);
$rightNormalized = $rightTypes->normalize($rightScope);
$types = $leftNormalized->intersectWith($rightNormalized);
$leftNormalized = $this->conditionalExpressionHolderHelper->toSureTypes($leftTypes, $scope);
$rightNormalized = $this->conditionalExpressionHolderHelper->toSureTypes($rightTypes, $rightScope);
$types = $leftTypes->intersectWith($rightTypes);
$types = $this->augmentBooleanOrTruthyWithConditionalHolders($typeSpecifier, $scope, $rightScope, $expr, $types);
$types = $this->conditionalExpressionHolderHelper->augmentDisjunctionTypes($scope, $rightScope, $leftNormalized, $rightNormalized, $expr->left, $expr->right, true, $types);
}
Expand All @@ -166,10 +166,10 @@ public function specifyTypes(TypeSpecifier $typeSpecifier, Scope $scope, Expr $e
}

if ($context->true()) {
$result = new SpecifiedTypes(
$result = (new SpecifiedTypes(
$types->getSureTypes(),
$types->getSureNotTypes(),
);
))->withAlternativeTypesOf($types);
if ($types->shouldOverwrite()) {
$result = $result->setAlwaysOverwriteTypes();
}
Expand Down Expand Up @@ -243,18 +243,18 @@ private function specifyTypesForFlattenedBooleanOr(
$armSpecifiedTypes = [];
foreach ($arms as $arm) {
$armTypes = $typeSpecifier->specifyTypesInCondition($scope, $arm, $context);
$armSpecifiedTypes[] = $armTypes->normalize($scope);
$armSpecifiedTypes[] = $armTypes;
}

$types = $armSpecifiedTypes[0];
for ($i = 1; $i < count($armSpecifiedTypes); $i++) {
$types = $types->intersectWith($armSpecifiedTypes[$i]);
}

$result = new SpecifiedTypes(
$result = (new SpecifiedTypes(
$types->getSureTypes(),
$types->getSureNotTypes(),
);
))->withAlternativeTypesOf($types);
if ($types->shouldOverwrite()) {
$result = $result->setAlwaysOverwriteTypes();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,13 @@ public function augmentDisjunctionTypes(
}

$existingSureTypes = $types->getSureTypes();
$existingAlternativeTypes = $types->getAlternativeTypes();

$viableCandidates = [];
foreach ($candidateExprs as $exprString => $targetExpr) {
if (isset($existingSureTypes[$exprString])) {
if (isset($existingSureTypes[$exprString]) || isset($existingAlternativeTypes[$exprString])) {
// an alternative-form entry already encodes the either-branch
// union for this expression, deferred to the application point
continue;
}
if (!$scope->hasExpressionType($targetExpr)->yes()) {
Expand Down Expand Up @@ -298,4 +301,33 @@ private function isTrackableExpression(Expr $expr): bool
|| $expr instanceof Expr\StaticPropertyFetch;
}

/**
* The eager form of the old SpecifiedTypes::normalize(): folds sure-not
* entries into sure entries by subtracting from the expression's type on
* the given scope. Only for consumers that need concrete sure types at
* composition time (conditional-holder building, decided operands) -
* merge paths use SpecifiedTypes::intersectWith() and evaluate at the
* application point instead.
*/
public function toSureTypes(SpecifiedTypes $types, Scope $scope): SpecifiedTypes
{
$sureTypes = $types->getSureTypes();

foreach ($types->getSureNotTypes() as $exprString => [$exprNode, $sureNotType]) {
if (!isset($sureTypes[$exprString])) {
$sureTypes[$exprString] = [$exprNode, TypeCombinator::remove($scope->getType($exprNode), $sureNotType)];
continue;
}

$sureTypes[$exprString][1] = TypeCombinator::remove($sureTypes[$exprString][1], $sureNotType);
}

$result = new SpecifiedTypes($sureTypes, []);
if ($types->shouldOverwrite()) {
$result = $result->setAlwaysOverwriteTypes();
}

return $result->setRootExpr($types->getRootExpr());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public function specifyTypesForEqual(Expr\BinaryOp\Equal $expr, Scope $scope, Ty

return $context->true()
? $leftTypes->unionWith($rightTypes)
: $leftTypes->normalize($scope)->intersectWith($rightTypes->normalize($scope));
: $leftTypes->intersectWith($rightTypes);
}

public function specifyTypesForIdentical(Expr\BinaryOp\Identical $expr, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes
Expand Down Expand Up @@ -750,8 +750,8 @@ private function specifyTypesForNormalizedIdentical(Expr\BinaryOp\Identical $exp
}
return $leftTypes->unionWith($rightTypes);
} elseif ($context->false()) {
return $this->typeSpecifier->create($leftExpr, $leftType, $context, $scope)->setRootExpr($expr)->normalize($scope)
->intersectWith($this->typeSpecifier->create($rightExpr, $rightType, $context, $scope)->setRootExpr($expr)->normalize($scope));
return $this->typeSpecifier->create($leftExpr, $leftType, $context, $scope)->setRootExpr($expr)
->intersectWith($this->typeSpecifier->create($rightExpr, $rightType, $context, $scope)->setRootExpr($expr));
}

return (new SpecifiedTypes([], []))->setRootExpr($expr);
Expand Down
2 changes: 1 addition & 1 deletion src/Analyser/ExprHandler/NullsafeMethodCallHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function specifyTypes(TypeSpecifier $typeSpecifier, Scope $scope, Expr $e
)->setRootExpr($expr);

$nullSafeTypes = $typeSpecifier->handleDefaultTruthyOrFalseyContext($context, $expr, $scope);
return $context->true() ? $types->unionWith($nullSafeTypes) : $types->normalize($scope)->intersectWith($nullSafeTypes->normalize($scope));
return $context->true() ? $types->unionWith($nullSafeTypes) : $types->intersectWith($nullSafeTypes);
}

public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
Expand Down
2 changes: 1 addition & 1 deletion src/Analyser/ExprHandler/NullsafePropertyFetchHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function specifyTypes(TypeSpecifier $typeSpecifier, Scope $scope, Expr $e
)->setRootExpr($expr);

$nullSafeTypes = $typeSpecifier->handleDefaultTruthyOrFalseyContext($context, $expr, $scope);
return $context->true() ? $types->unionWith($nullSafeTypes) : $types->normalize($scope)->intersectWith($nullSafeTypes->normalize($scope));
return $context->true() ? $types->unionWith($nullSafeTypes) : $types->intersectWith($nullSafeTypes);
}

public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
Expand Down
47 changes: 46 additions & 1 deletion src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -3308,11 +3308,26 @@ public function filterBySpecifiedTypes(SpecifiedTypes $specifiedTypes): self
{
$typeSpecifications = ScopeOps::buildTypeSpecifications($specifiedTypes->getSureTypes(), $specifiedTypes->getSureNotTypes());

foreach ($specifiedTypes->getAlternativeTypes() as $exprString => [$alternativeExpr, $terms]) {
if (
$alternativeExpr instanceof Node\Scalar
|| $alternativeExpr instanceof Expr\Array_
|| ($alternativeExpr instanceof Expr\UnaryMinus && $alternativeExpr->expr instanceof Node\Scalar)
) {
continue;
}
$typeSpecifications[] = [
'sure' => true,
'exprString' => (string) $exprString,
'expr' => $alternativeExpr,
'terms' => $terms,
];
}

$scope = $this;
$specifiedExpressions = [];
foreach ($typeSpecifications as $typeSpecification) {
$expr = $typeSpecification['expr'];
$type = $typeSpecification['type'];

if ($expr instanceof IssetExpr) {
$issetExpr = $expr;
Expand Down Expand Up @@ -3341,6 +3356,36 @@ public function filterBySpecifiedTypes(SpecifiedTypes $specifiedTypes): self
continue;
}

if (isset($typeSpecification['terms'])) {
// an alternative-form entry: the union over its terms of
// `(sure ?? current) minus subtract`, evaluated here at the
// application point - the deferred descendant of the old
// SpecifiedTypes::normalize()
$evaluate = static function (Type $current) use ($typeSpecification): Type {
$parts = [];
foreach ($typeSpecification['terms'] as [$sure, $subtract]) {
$base = $sure ?? $current;
$parts[] = $subtract !== null ? TypeCombinator::remove($base, $subtract) : $base;
}

return TypeCombinator::union(...$parts);
};
$originalExprType = $scope->getType($expr);
if (!$scope->isComplexUnionType($originalExprType)) {
$nativeType = $scope->getNativeType($expr);
$scope = $scope->specifyExpressionType(
$expr,
TypeCombinator::intersect($evaluate($originalExprType), $originalExprType),
TypeCombinator::intersect($evaluate($nativeType), $nativeType),
TrinaryLogic::createYes(),
);
$specifiedExpressions[$typeSpecification['exprString']] = ExpressionTypeHolder::createYes($expr, $scope->getScopeType($expr));
}

continue;
}

$type = $typeSpecification['type'];
if ($typeSpecification['sure']) {
if ($specifiedTypes->shouldOverwrite()) {
$scope = $scope->assignExpression($expr, $type, $type);
Expand Down
Loading
Loading