diff --git a/.gitignore b/.gitignore index 1a5079cb..ae062345 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,6 @@ /tests/fixtures/drupal/sites/simpletest/ .phpunit.result.cache .idea/ + +# Temporary files +phpstan.phar diff --git a/src/Type/EntityQuery/EntityQueryDynamicReturnTypeExtension.php b/src/Type/EntityQuery/EntityQueryDynamicReturnTypeExtension.php index c543e86c..e2cebad9 100644 --- a/src/Type/EntityQuery/EntityQueryDynamicReturnTypeExtension.php +++ b/src/Type/EntityQuery/EntityQueryDynamicReturnTypeExtension.php @@ -9,6 +9,7 @@ use PHPStan\Reflection\ParametersAcceptorSelector; use PHPStan\Type\ArrayType; use PHPStan\Type\DynamicMethodReturnTypeExtension; +use PHPStan\Type\IntegerRangeType; use PHPStan\Type\IntegerType; use PHPStan\Type\ObjectType; use PHPStan\Type\StringType; @@ -63,7 +64,7 @@ public function getTypeFromMethodCall( } if ($varType->isCount()) { return $varType->hasAccessCheck() - ? new IntegerType() + ? IntegerRangeType::createAllGreaterThanOrEqualTo(0) : new EntityQueryExecuteWithoutAccessCheckCountType(); } if ($varType instanceof ConfigEntityQueryType) { diff --git a/src/Type/EntityQuery/EntityQueryExecuteWithoutAccessCheckCountType.php b/src/Type/EntityQuery/EntityQueryExecuteWithoutAccessCheckCountType.php index 8c42c5ea..312934a8 100644 --- a/src/Type/EntityQuery/EntityQueryExecuteWithoutAccessCheckCountType.php +++ b/src/Type/EntityQuery/EntityQueryExecuteWithoutAccessCheckCountType.php @@ -4,9 +4,14 @@ namespace mglaman\PHPStanDrupal\Type\EntityQuery; -use PHPStan\Type\IntegerType; +use PHPStan\Type\IntegerRangeType; -final class EntityQueryExecuteWithoutAccessCheckCountType extends IntegerType +final class EntityQueryExecuteWithoutAccessCheckCountType extends IntegerRangeType { + public function __construct() + { + // Initialize as a non-negative integer range (0 to max) + parent::__construct(0, null); + } } diff --git a/tests/src/Type/data/entity-query-execute.php b/tests/src/Type/data/entity-query-execute.php index ce9a89f0..b828d28d 100644 --- a/tests/src/Type/data/entity-query-execute.php +++ b/tests/src/Type/data/entity-query-execute.php @@ -15,14 +15,14 @@ ->execute() ); assertType( - 'int', + 'int<0, max>', \Drupal::entityTypeManager()->getStorage('node')->getQuery() ->accessCheck(TRUE) ->count() ->execute() ); assertType( - 'int', + 'int<0, max>', \Drupal::entityTypeManager()->getStorage('node')->getQuery() ->count() ->condition('foo', 'bar') @@ -30,7 +30,7 @@ ->execute() ); assertType( - 'int', + 'int<0, max>', \Drupal::entityQuery('node') ->accessCheck(TRUE) ->count() @@ -48,7 +48,7 @@ assertType('array', $query->execute()); $query = \Drupal::entityTypeManager()->getStorage('node')->getQuery() ->accessCheck(TRUE)->count(); -assertType('int', $query->execute()); +assertType('int<0, max>', $query->execute()); assertType( 'array', @@ -62,14 +62,14 @@ ->execute() ); assertType( - 'int', + 'int<0, max>', \Drupal::entityTypeManager()->getStorage('block')->getQuery() ->accessCheck(TRUE) ->count() ->execute() ); assertType( - 'int', + 'int<0, max>', \Drupal::entityQuery('block') ->accessCheck(TRUE) ->count() @@ -87,4 +87,4 @@ assertType('array', $query->execute()); $query = \Drupal::entityTypeManager()->getStorage('block')->getQuery() ->accessCheck(TRUE)->count(); -assertType('int', $query->execute()); +assertType('int<0, max>', $query->execute());