Skip to content

Commit

Permalink
Add missing SpecifiedTypes for count and strlen identical expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
herndlm committed Feb 23, 2022
1 parent bbf14bf commit e504c3f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
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));
break;
}
Expand Down

0 comments on commit e504c3f

Please sign in to comment.