Skip to content

Commit

Permalink
[BCB] Parameter $isList in ConstantArrayType constructor can only…
Browse files Browse the repository at this point in the history
… be `TrinaryLogic`
  • Loading branch information
ondrejmirtes committed Sep 29, 2024
1 parent 45b4a85 commit e75996b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 20 deletions.
1 change: 1 addition & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,4 @@ Instead of `PHPStanTestCase::createBroker()`, call `PHPStanTestCase::createRefle
* Remove `TypeUtils::getEnumCaseObjects()`, use [`Type::getEnumCases()`](https://apiref.phpstan.org/2.0.x/PHPStan.Type.Type.html#_getEnumCases) instead
* Remove `TypeUtils::containsCallable()`, use [`Type::isCallable()`](https://apiref.phpstan.org/2.0.x/PHPStan.Type.Type.html#_isCallable) instead
* Removed `Scope::doNotTreatPhpDocTypesAsCertain()`, use `getNativeType()` instead
* Parameter `$isList` in `ConstantArrayType` constructor can only be `TrinaryLogic`, no longer bool
7 changes: 3 additions & 4 deletions src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
use function count;
use function implode;
use function in_array;
use function is_bool;
use function is_int;
use function is_string;
use function min;
Expand Down Expand Up @@ -108,7 +107,7 @@ public function __construct(
private array $valueTypes,
int|array $nextAutoIndexes = [0],
private array $optionalKeys = [],
bool|TrinaryLogic $isList = false,
?TrinaryLogic $isList = null,
)
{
assert(count($keyTypes) === count($valueTypes));
Expand All @@ -124,8 +123,8 @@ public function __construct(
$isList = TrinaryLogic::createYes();
}

if (is_bool($isList)) {
$isList = TrinaryLogic::createFromBoolean($isList);
if ($isList === null) {
$isList = TrinaryLogic::createNo();
}
$this->isList = $isList;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Type/Php/RegexArrayShapeMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched
if (!$this->containsUnmatchedAsNull($flags, $matchesAll)) {
// positive match has a subject but not any capturing group
$combiType = TypeCombinator::union(
new ConstantArrayType([new ConstantIntegerType(0)], [$this->createSubjectValueType($flags, $matchesAll)], [1], [], true),
new ConstantArrayType([new ConstantIntegerType(0)], [$this->createSubjectValueType($flags, $matchesAll)], [1], [], TrinaryLogic::createYes()),
$combiType,
);
}
Expand Down Expand Up @@ -214,7 +214,7 @@ private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched
)
) {
// positive match has a subject but not any capturing group
$combiTypes[] = new ConstantArrayType([new ConstantIntegerType(0)], [$this->createSubjectValueType($flags, $matchesAll)], [1], [], true);
$combiTypes[] = new ConstantArrayType([new ConstantIntegerType(0)], [$this->createSubjectValueType($flags, $matchesAll)], [1], [], TrinaryLogic::createYes());
}

return TypeCombinator::union(...$combiTypes);
Expand Down
20 changes: 10 additions & 10 deletions tests/PHPStan/Type/Constant/ConstantArrayTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -886,14 +886,14 @@ public function dataValuesArray(): iterable
], [
new ConstantStringType('a'),
new ConstantStringType('b'),
], [20], [], false),
], [20], [], TrinaryLogic::createNo()),
new ConstantArrayType([
new ConstantIntegerType(0),
new ConstantIntegerType(1),
], [
new ConstantStringType('a'),
new ConstantStringType('b'),
], [2], [], true),
], [2], [], TrinaryLogic::createYes()),
];

yield 'optional-1' => [
Expand All @@ -909,7 +909,7 @@ public function dataValuesArray(): iterable
new ConstantStringType('c'),
new ConstantStringType('d'),
new ConstantStringType('e'),
], [15], [1, 3], false),
], [15], [1, 3], TrinaryLogic::createNo()),
new ConstantArrayType([
new ConstantIntegerType(0),
new ConstantIntegerType(1),
Expand All @@ -922,7 +922,7 @@ public function dataValuesArray(): iterable
new UnionType([new ConstantStringType('c'), new ConstantStringType('d'), new ConstantStringType('e')]),
new UnionType([new ConstantStringType('d'), new ConstantStringType('e')]),
new ConstantStringType('e'),
], [3, 4, 5], [3, 4], true),
], [3, 4, 5], [3, 4], TrinaryLogic::createYes()),
];

yield 'optional-2' => [
Expand All @@ -938,7 +938,7 @@ public function dataValuesArray(): iterable
new ConstantStringType('c'),
new ConstantStringType('d'),
new ConstantStringType('e'),
], [15], [0, 2, 4], false),
], [15], [0, 2, 4], TrinaryLogic::createNo()),
new ConstantArrayType([
new ConstantIntegerType(0),
new ConstantIntegerType(1),
Expand All @@ -951,7 +951,7 @@ public function dataValuesArray(): iterable
new UnionType([new ConstantStringType('c'), new ConstantStringType('d'), new ConstantStringType('e')]),
new UnionType([new ConstantStringType('d'), new ConstantStringType('e')]),
new ConstantStringType('e'),
], [2, 3, 4, 5], [2, 3, 4], true),
], [2, 3, 4, 5], [2, 3, 4], TrinaryLogic::createYes()),
];

yield 'optional-at-end-and-list' => [
Expand All @@ -963,7 +963,7 @@ public function dataValuesArray(): iterable
new ConstantStringType('a'),
new ConstantStringType('b'),
new ConstantStringType('c'),
], [11, 12, 13], [1, 2], true),
], [11, 12, 13], [1, 2], TrinaryLogic::createYes()),
new ConstantArrayType([
new ConstantIntegerType(0),
new ConstantIntegerType(1),
Expand All @@ -972,7 +972,7 @@ public function dataValuesArray(): iterable
new ConstantStringType('a'),
new ConstantStringType('b'),
new ConstantStringType('c'),
], [1, 2, 3], [1, 2], true),
], [1, 2, 3], [1, 2], TrinaryLogic::createYes()),
];

yield 'optional-at-end-but-not-list' => [
Expand All @@ -984,7 +984,7 @@ public function dataValuesArray(): iterable
new ConstantStringType('a'),
new ConstantStringType('b'),
new ConstantStringType('c'),
], [11, 12, 13], [1, 2], false),
], [11, 12, 13], [1, 2], TrinaryLogic::createNo()),
new ConstantArrayType([
new ConstantIntegerType(0),
new ConstantIntegerType(1),
Expand All @@ -993,7 +993,7 @@ public function dataValuesArray(): iterable
new ConstantStringType('a'),
new UnionType([new ConstantStringType('b'), new ConstantStringType('c')]),
new ConstantStringType('c'),
], [1, 2, 3], [1, 2], true),
], [1, 2, 3], [1, 2], TrinaryLogic::createYes()),
];
}

Expand Down
9 changes: 5 additions & 4 deletions tests/PHPStan/Type/TypeGetFiniteTypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPStan\Type;

use PHPStan\Testing\PHPStanTestCase;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantIntegerType;
Expand Down Expand Up @@ -96,28 +97,28 @@ public function dataGetFiniteTypes(): iterable
], [
new ConstantBooleanType(true),
new ConstantBooleanType(true),
], 2, [], true),
], 2, [], TrinaryLogic::createYes()),
new ConstantArrayType([
new ConstantIntegerType(0),
new ConstantIntegerType(1),
], [
new ConstantBooleanType(true),
new ConstantBooleanType(false),
], 2, [], true),
], 2, [], TrinaryLogic::createYes()),
new ConstantArrayType([
new ConstantIntegerType(0),
new ConstantIntegerType(1),
], [
new ConstantBooleanType(false),
new ConstantBooleanType(true),
], 2, [], true),
], 2, [], TrinaryLogic::createYes()),
new ConstantArrayType([
new ConstantIntegerType(0),
new ConstantIntegerType(1),
], [
new ConstantBooleanType(false),
new ConstantBooleanType(false),
], 2, [], true),
], 2, [], TrinaryLogic::createYes()),
],
];
}
Expand Down

0 comments on commit e75996b

Please sign in to comment.