Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing SpecifiedTypes for count and strlen identical expressions #1021

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/Analyser/TypeSpecifier.php
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
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
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));
herndlm marked this conversation as resolved.
Show resolved Hide resolved
break;
}
Expand Down