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
52 changes: 35 additions & 17 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -984,31 +984,49 @@ private function specifyTypesForConstantBinaryExpression(
&& in_array(strtolower((string) $exprNode->name), ['count', 'sizeof'], true)
&& $constantType instanceof ConstantIntegerType
) {
$argType = $scope->getType($exprNode->getArgs()[0]->value);

if (count($exprNode->getArgs()) === 1) {
$isNormalCount = TrinaryLogic::createYes();
} else {
$mode = $scope->getType($exprNode->getArgs()[1]->value);
$isNormalCount = (new ConstantIntegerType(COUNT_NORMAL))->isSuperTypeOf($mode)->or($argType->getIterableValueType()->isArray()->negate());
}

if (
$isNormalCount->yes()
&& $argType instanceof UnionType
) {
$result = [];
foreach ($argType->getTypes() as $innerType) {
$arraySize = $innerType->getArraySize();
$isSize = $constantType->isSuperTypeOf($arraySize);
if ($context->truthy()) {
if ($isSize->no()) {
continue;
}
}
if ($context->falsey()) {
if (!$isSize->yes()) {
continue;
}
}

$result[] = $innerType;
}

return $this->create($exprNode->getArgs()[0]->value, TypeCombinator::union(...$result), $context, false, $scope, $rootExpr);
}

if ($context->truthy() || $constantType->getValue() === 0) {
$newContext = $context;
if ($constantType->getValue() === 0) {
$newContext = $newContext->negate();
}

$argType = $scope->getType($exprNode->getArgs()[0]->value);

if ($argType->isArray()->yes()) {
if (count($exprNode->getArgs()) === 1) {
$isNormalCount = true;
} else {
$mode = $scope->getType($exprNode->getArgs()[1]->value);
if (!$mode->isInteger()->yes()) {
return new SpecifiedTypes();
}

$isNormalCount = (new ConstantIntegerType(COUNT_NORMAL))->isSuperTypeOf($mode)->yes();
if (!$isNormalCount) {
$isNormalCount = $argType->getIterableValueType()->isArray()->no();
}
}

$funcTypes = $this->create($exprNode, $constantType, $context, false, $scope, $rootExpr);
if ($isNormalCount && $argType->isList()->yes() && $context->truthy() && $constantType->getValue() < ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT) {
if ($isNormalCount->yes() && $argType->isList()->yes() && $context->truthy() && $constantType->getValue() < ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT) {
$valueTypesBuilder = ConstantArrayTypeBuilder::createEmpty();
$itemType = $argType->getIterableValueType();
for ($i = 0; $i < $constantType->getValue(); $i++) {
Expand Down
111 changes: 111 additions & 0 deletions tests/PHPStan/Analyser/nsrt/narrow-tagged-union.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php declare(strict_types=1);

namespace NarrowTaggedUnion;

use function PHPStan\Testing\assertType;

class HelloWorld
{
/** @param array{string, '', non-empty-string}|array{string, numeric-string} $arr */
public function sayHello(array $arr): void
{
if (count($arr) === 0) {
assertType('*NEVER*', $arr);
} else {
assertType("array{string, '', non-empty-string}|array{string, numeric-string}", $arr);
}
assertType("array{string, '', non-empty-string}|array{string, numeric-string}", $arr);

if (count($arr) === 1) {
assertType('*NEVER*', $arr);
} else {
assertType("array{string, '', non-empty-string}|array{string, numeric-string}", $arr);
}
assertType("array{string, '', non-empty-string}|array{string, numeric-string}", $arr);

if (count($arr) === 2) {
assertType('array{string, numeric-string}', $arr);
} else {
assertType("array{string, '', non-empty-string}", $arr);
}
assertType("array{string, '', non-empty-string}|array{string, numeric-string}", $arr);

if (count($arr) === 3) {
assertType("array{string, '', non-empty-string}", $arr);
} else {
assertType('array{string, numeric-string}', $arr);
}
assertType("array{string, '', non-empty-string}|array{string, numeric-string}", $arr);

if (count($arr, COUNT_NORMAL) === 3) {
assertType("array{string, '', non-empty-string}", $arr);
} else {
assertType('array{string, numeric-string}', $arr);
}
assertType("array{string, '', non-empty-string}|array{string, numeric-string}", $arr);

if (count($arr, COUNT_RECURSIVE) === 3) {
assertType("array{string, '', non-empty-string}", $arr);
} else {
assertType('array{string, numeric-string}', $arr);
}
assertType("array{string, '', non-empty-string}|array{string, numeric-string}", $arr);

if (count($arr) === 4) {
assertType('*NEVER*', $arr);
} else {
assertType("array{string, '', non-empty-string}|array{string, numeric-string}", $arr);
}
assertType("array{string, '', non-empty-string}|array{string, numeric-string}", $arr);

}

/** @param array{string, '', non-empty-string}|array{array<int>, numeric-string} $arr */
public function nestedArrays(array $arr): void
{
// don't narrow when $arr contains recursive arrays
if (count($arr, COUNT_RECURSIVE) === 3) {
assertType("array{array<int>, numeric-string}|array{string, '', non-empty-string}", $arr);
} else {
assertType("array{array<int>, numeric-string}|array{string, '', non-empty-string}", $arr);
}
assertType("array{array<int>, numeric-string}|array{string, '', non-empty-string}", $arr);

if (count($arr, COUNT_NORMAL) === 3) {
assertType("array{string, '', non-empty-string}", $arr);
} else {
assertType("array{array<int>, numeric-string}", $arr);
}
assertType("array{array<int>, numeric-string}|array{string, '', non-empty-string}", $arr);
}

/** @param array{string, '', non-empty-string}|array<int> $arr */
public function mixedArrays(array $arr): void
{
if (count($arr, COUNT_NORMAL) === 3) {
assertType("non-empty-array<int|string>", $arr); // could be array{string, '', non-empty-string}|non-empty-array<int>
} else {
assertType("array<int|string>", $arr); // could be array{string, '', non-empty-string}|array<int>
}
assertType("array<int|string>", $arr); // could be array{string, '', non-empty-string}|array<int>
}

public function arrayIntRangeSize(): void
{
$x = [];
if (rand(0,1)) {
$x[] = 'ab';
}
if (rand(0,1)) {
$x[] = 'xy';
}

if (count($x) === 1) {
assertType("array{'xy'}|array{0: 'ab', 1?: 'xy'}", $x);
} else {
assertType("array{}|array{0: 'ab', 1?: 'xy'}", $x);
}
assertType("array{}|array{'xy'}|array{0: 'ab', 1?: 'xy'}", $x);
}
}

Loading