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
8 changes: 6 additions & 2 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,9 @@ public function specifyTypesInCondition(
}
$argType = $scope->getType($exprNode->getArgs()[0]->value);
if ($argType->isArray()->yes()) {
return $this->create($exprNode->getArgs()[0]->value, new NonEmptyArrayType(), $newContext, false, $scope);
$funcTypes = $this->create($exprNode, $constantType, $context, false, $scope);
$valueTypes = $this->create($exprNode->getArgs()[0]->value, new NonEmptyArrayType(), $newContext, false, $scope);
return $funcTypes->unionWith($valueTypes);
}
}
}
Expand All @@ -230,7 +232,9 @@ public function specifyTypesInCondition(
}
$argType = $scope->getType($exprNode->getArgs()[0]->value);
if ($argType instanceof StringType) {
return $this->create($exprNode->getArgs()[0]->value, new AccessoryNonEmptyStringType(), $newContext, false, $scope);
$funcTypes = $this->create($exprNode, $constantType, $context, false, $scope);
$valueTypes = $this->create($exprNode->getArgs()[0]->value, new AccessoryNonEmptyStringType(), $newContext, false, $scope);
return $funcTypes->unionWith($valueTypes);
}
}
}
Expand Down
53 changes: 53 additions & 0 deletions tests/PHPStan/Analyser/TypeSpecifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,59 @@ public function dataCondition(): array
],
[],
],
[
new Expr\BinaryOp\BooleanAnd(
$this->createFunctionCall('is_array', 'foo'),
new Expr\BinaryOp\GreaterOrEqual(
new FuncCall(
new Name('count'),
[new Arg(new Variable('foo'))],
),
new LNumber(2),
),
),
[
'$foo' => 'non-empty-array',
'count($foo)' => 'mixed~int<min, 1>|false|null',
],
[],
],
[
new Expr\BinaryOp\BooleanAnd(
$this->createFunctionCall('is_array', 'foo'),
new Identical(
new FuncCall(
new Name('count'),
[new Arg(new Variable('foo'))],
),
new LNumber(2),
),
),
[
'$foo' => 'non-empty-array',
'count($foo)' => '2',
],
[],
],
[
new Expr\BinaryOp\BooleanAnd(
$this->createFunctionCall('is_string', 'foo'),
new NotIdentical(
new FuncCall(
new Name('strlen'),
[new Arg(new Variable('foo'))],
),
new LNumber(0),
),
),
[
'$foo' => 'non-empty-string',
'strlen($foo)' => '~0',
],
[
'$foo' => '~non-empty-string',
],
],
];
}

Expand Down
2 changes: 2 additions & 0 deletions tests/PHPStan/Analyser/data/bug-2648.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public function doBar(array $list): void
assertType('int<0, max>', count($list));

if (count($list) === 1) {
assertType('1', count($list));
$list[] = false;
assertType('int<1, max>', count($list));
break;
}
Expand Down