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
26 changes: 23 additions & 3 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,26 @@ public function specifyTypesInCondition(
$context->true() ? TypeSpecifierContext::createTruthy() : TypeSpecifierContext::createTruthy()->negate()
);
}

if (
!$context->null()
&& $exprNode instanceof FuncCall
&& count($exprNode->args) === 1
&& $exprNode->name instanceof Name
&& in_array(strtolower((string) $exprNode->name), ['count', 'sizeof'], true)
&& $constantType instanceof ConstantIntegerType
) {
if ($context->truthy() || $constantType->getValue() === 0) {
$newContext = $context;
if ($constantType->getValue() === 0) {
$newContext = $newContext->negate();
}
$argType = $scope->getType($exprNode->args[0]->value);
if ($argType->isArray()->yes()) {
return $this->create($exprNode->args[0]->value, new NonEmptyArrayType(), $newContext, false, $scope);
}
}
}
}

$leftType = $scope->getType($expr->left);
Expand Down Expand Up @@ -404,11 +424,11 @@ public function specifyTypesInCondition(
$expr->left instanceof FuncCall
&& count($expr->left->args) === 1
&& $expr->left->name instanceof Name
&& in_array(strtolower((string) $expr->left->name), ['count', 'strlen'], true)
&& in_array(strtolower((string) $expr->left->name), ['count', 'sizeof', 'strlen'], true)
&& (
!$expr->right instanceof FuncCall
|| !$expr->right->name instanceof Name
|| !in_array(strtolower((string) $expr->right->name), ['count', 'strlen'], true)
|| !in_array(strtolower((string) $expr->right->name), ['count', 'sizeof', 'strlen'], true)
)
) {
$inverseOperator = $expr instanceof Node\Expr\BinaryOp\Smaller
Expand All @@ -429,7 +449,7 @@ public function specifyTypesInCondition(
&& $expr->right instanceof FuncCall
&& count($expr->right->args) === 1
&& $expr->right->name instanceof Name
&& strtolower((string) $expr->right->name) === 'count'
&& in_array(strtolower((string) $expr->right->name), ['count', 'sizeof'], true)
&& (new IntegerType())->isSuperTypeOf($leftType)->yes()
) {
if (
Expand Down
2 changes: 2 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,8 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/filter-var-returns-non-empty-string.php');

yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5529.php');

yield from $this->gatherAssertTypes(__DIR__ . '/data/sizeof.php');
}

/**
Expand Down
47 changes: 47 additions & 0 deletions tests/PHPStan/Analyser/data/sizeof.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace sizeof;

use function PHPStan\Testing\assertType;


class Sizeof
{
/**
* @param int[] $ints
*/
function doFoo1(array $ints): string
{
if (count($ints) <= 0) {
assertType('false', min($ints));
assertType('false', max($ints));
}
}

/**
* @param int[] $ints
*/
function doFoo2(array $ints): string
{
if (sizeof($ints) <= 0) {
assertType('false', min($ints));
assertType('false', max($ints));
}
}

function doFoo3(array $arr): string
{
if (0 != count($arr)) {
assertType('array&nonEmpty', $arr);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is basically deleting an existing test. Please add additional line instead of deleting this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry, I misread the commit history, it's fine.

}
return "";
}

function doFoo4(array $arr): string
{
if (0 != sizeof($arr)) {
assertType('array&nonEmpty', $arr);
}
return "";
}
}