Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes authored and staabm committed Dec 31, 2022
1 parent 7d59e37 commit 7d779e9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
8 changes: 8 additions & 0 deletions phpstan-baseline.neon
Expand Up @@ -15,6 +15,14 @@ parameters:
count: 1
path: src/Analyser/LazyInternalScopeFactory.php

-
message: """
#^Call to deprecated method getAnyArrays\\(\\) of class PHPStan\\\\Type\\\\TypeUtils\\:
Use PHPStan\\\\Type\\\\Type\\:\\:getArrays\\(\\) instead\\.$#
"""
count: 2
path: src/Analyser/MutatingScope.php

-
message: """
#^Call to deprecated method getTypeFromValue\\(\\) of class PHPStan\\\\Type\\\\ConstantTypeHelper\\:
Expand Down
42 changes: 19 additions & 23 deletions src/Analyser/MutatingScope.php
Expand Up @@ -4078,7 +4078,7 @@ public function processClosureScope(
$prevVariableType = $prevScope->getVariableType($variableName);
if (!$variableType->equals($prevVariableType)) {
$variableType = TypeCombinator::union($variableType, $prevVariableType);
$variableType = self::generalizeType($variableType, $prevVariableType);
$variableType = self::generalizeType($variableType, $prevVariableType, 0);
}
}

Expand Down Expand Up @@ -4200,15 +4200,15 @@ private function generalizeVariableTypeHolders(

$variableTypeHolders[$variableExprString] = new ExpressionTypeHolder(
$variableTypeHolder->getExpr(),
self::generalizeType($variableTypeHolder->getType(), $otherVariableTypeHolders[$variableExprString]->getType()),
self::generalizeType($variableTypeHolder->getType(), $otherVariableTypeHolders[$variableExprString]->getType(), 0),
$variableTypeHolder->getCertainty(),
);
}

return $variableTypeHolders;
}

private static function generalizeType(Type $a, Type $b): Type
private static function generalizeType(Type $a, Type $b, int $depth): Type
{
if ($a->equals($b)) {
return $a;
Expand Down Expand Up @@ -4301,6 +4301,7 @@ private static function generalizeType(Type $a, Type $b): Type
self::generalizeType(
$constantArraysA->getOffsetValueType($keyType),
$constantArraysB->getOffsetValueType($keyType),
$depth + 1,
),
!$constantArraysA->hasOffsetValueType($keyType)->and($constantArraysB->hasOffsetValueType($keyType))->negate()->no(),
);
Expand All @@ -4309,8 +4310,8 @@ private static function generalizeType(Type $a, Type $b): Type
$resultTypes[] = $resultArrayBuilder->getArray();
} else {
$resultType = new ArrayType(
TypeCombinator::union(self::generalizeType($constantArraysA->getIterableKeyType(), $constantArraysB->getIterableKeyType())),
TypeCombinator::union(self::generalizeType($constantArraysA->getIterableValueType(), $constantArraysB->getIterableValueType())),
TypeCombinator::union(self::generalizeType($constantArraysA->getIterableKeyType(), $constantArraysB->getIterableKeyType(), $depth + 1)),
TypeCombinator::union(self::generalizeType($constantArraysA->getIterableValueType(), $constantArraysB->getIterableValueType(), $depth + 1)),
);
if ($constantArraysA->isIterableAtLeastOnce()->yes() && $constantArraysB->isIterableAtLeastOnce()->yes()) {
$resultType = TypeCombinator::intersect($resultType, new NonEmptyArrayType());
Expand All @@ -4334,16 +4335,14 @@ private static function generalizeType(Type $a, Type $b): Type

$aValueType = $generalArraysA->getIterableValueType();
$bValueType = $generalArraysB->getIterableValueType();
$aArrays = $aValueType->getArrays();
$bArrays = $bValueType->getArrays();
if (
count($aArrays) === 1
&& $aArrays[0]->isConstantArray()->no()
&& count($bArrays) === 1
&& $bArrays[0]->isConstantArray()->no()
$aValueType->isArray()->yes()
&& $aValueType->isConstantArray()->no()
&& $bValueType->isArray()->yes()
&& $bValueType->isConstantArray()->no()
) {
$aDepth = self::getArrayDepth($aArrays[0]);
$bDepth = self::getArrayDepth($bArrays[0]);
$aDepth = self::getArrayDepth($aValueType) + $depth;
$bDepth = self::getArrayDepth($bValueType) + $depth;
if (
($aDepth > 2 || $bDepth > 2)
&& abs($aDepth - $bDepth) > 0
Expand All @@ -4354,8 +4353,8 @@ private static function generalizeType(Type $a, Type $b): Type
}

$resultType = new ArrayType(
TypeCombinator::union(self::generalizeType($generalArraysA->getIterableKeyType(), $generalArraysB->getIterableKeyType())),
TypeCombinator::union(self::generalizeType($aValueType, $bValueType)),
TypeCombinator::union(self::generalizeType($generalArraysA->getIterableKeyType(), $generalArraysB->getIterableKeyType(), $depth + 1)),
TypeCombinator::union(self::generalizeType($aValueType, $bValueType, $depth + 1)),
);
if ($generalArraysA->isIterableAtLeastOnce()->yes() && $generalArraysB->isIterableAtLeastOnce()->yes()) {
$resultType = TypeCombinator::intersect($resultType, new NonEmptyArrayType());
Expand Down Expand Up @@ -4514,17 +4513,14 @@ private static function generalizeType(Type $a, Type $b): Type
);
}

private static function getArrayDepth(ArrayType $type): int
private static function getArrayDepth(Type $type): int
{
$depth = 0;
while ($type instanceof ArrayType) {
$arrays = TypeUtils::getAnyArrays($type);
while (count($arrays) > 0) {
$temp = $type->getIterableValueType();
$arrays = $temp->getArrays();
if (count($arrays) === 1) {
$type = $arrays[0];
} else {
$type = $temp;
}
$type = $temp;
$arrays = TypeUtils::getAnyArrays($type);
$depth++;
}

Expand Down
5 changes: 3 additions & 2 deletions src/Type/UnionType.php
Expand Up @@ -116,11 +116,12 @@ public function getArrays(): array
{
$arrays = [];
foreach ($this->types as $type) {
if (!$type instanceof ArrayType) {
$innerTypeArrays = $type->getArrays();
if ($innerTypeArrays === []) {
return [];
}

foreach ($type->getArrays() as $array) {
foreach ($innerTypeArrays as $array) {
$arrays[] = $array;
}
}
Expand Down

0 comments on commit 7d779e9

Please sign in to comment.