Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -63,7 +64,7 @@ public function getTypeFromMethodCall(
}
if ($varType->isCount()) {
return $varType->hasAccessCheck()
? new IntegerType()
? IntegerRangeType::createAllGreaterThanOrEqualTo(0)
: new EntityQueryExecuteWithoutAccessCheckCountType();
}
if ($varType instanceof ConfigEntityQueryType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,23 @@

namespace mglaman\PHPStanDrupal\Type\EntityQuery;

use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\Type;
use PHPStan\Type\VerbosityLevel;

final class EntityQueryExecuteWithoutAccessCheckCountType extends IntegerType
{
private Type $rangeType;

public function __construct()
{
parent::__construct();
$this->rangeType = IntegerRangeType::createAllGreaterThanOrEqualTo(0);
}

public function describe(VerbosityLevel $level): string
{
return $this->rangeType->describe($level);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public static function dataFileAsserts(): iterable
yield from self::gatherAssertTypes(__DIR__ . '/../data/entity-query-execute.php');
yield from self::gatherAssertTypes(__DIR__ . '/../data/bug-355-entity-query.php');
yield from self::gatherAssertTypes(__DIR__ . '/../data/bug-522.php');
yield from self::gatherAssertTypes(__DIR__ . '/../data/bug-909.php');

}

Expand Down
12 changes: 6 additions & 6 deletions tests/src/Type/data/bug-355-entity-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
->accessCheck(TRUE);
assertType('array<int, string>', $query->execute());
assertType(
'int',
'int<0, max>',
$typedNodeStorage->getQuery()
->accessCheck(TRUE)
->count()
Expand All @@ -26,7 +26,7 @@
$query = $typedNodeStorage->getQuery()
->accessCheck(TRUE)
->count();
assertType('int', $query->execute());
assertType('int<0, max>', $query->execute());

/** @var \Drupal\node\NodeStorageInterface $anotherTypedNodeStorage */
$anotherTypedNodeStorage = \Drupal::entityTypeManager()->getStorage('node');
Expand All @@ -40,7 +40,7 @@
->accessCheck(TRUE);
assertType('array<int, string>', $query->execute());
assertType(
'int',
'int<0, max>',
$anotherTypedNodeStorage->getQuery()
->accessCheck(TRUE)
->count()
Expand All @@ -49,7 +49,7 @@
$query = $anotherTypedNodeStorage->getQuery()
->accessCheck(TRUE)
->count();
assertType('int', $query->execute());
assertType('int<0, max>', $query->execute());

$instanceOfNodeStorage = \Drupal::entityTypeManager()->getStorage('node');
if ($instanceOfNodeStorage instanceof NodeStorage) {
Expand All @@ -63,7 +63,7 @@
->accessCheck(TRUE);
assertType('array<int, string>', $query->execute());
assertType(
'int',
'int<0, max>',
$instanceOfNodeStorage->getQuery()
->accessCheck(TRUE)
->count()
Expand All @@ -72,5 +72,5 @@
$query = $instanceOfNodeStorage->getQuery()
->accessCheck(TRUE)
->count();
assertType('int', $query->execute());
assertType('int<0, max>', $query->execute());
}
55 changes: 55 additions & 0 deletions tests/src/Type/data/bug-909.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace DrupalEntity;

use function PHPStan\Testing\assertType;

// Test case from issue #909: Entity query count() should return non-negative-int
// Currently this fails because PHPStan returns 'int' instead of 'int<0, max>'
assertType(
'int<0, max>',
\Drupal::entityTypeManager()
->getStorage('example')
->getQuery()
->accessCheck()
->count()
->execute()
);

// Additional test cases to ensure consistency across different entity query patterns
assertType(
'int<0, max>',
\Drupal::entityTypeManager()
->getStorage('node')
->getQuery()
->accessCheck(TRUE)
->count()
->execute()
);

assertType(
'int<0, max>',
\Drupal::entityQuery('node')
->accessCheck(TRUE)
->count()
->execute()
);

assertType(
'int<0, max>',
\Drupal::entityQuery('block')
->count()
->execute()
);

// Test with additional conditions - should still return non-negative-int
assertType(
'int<0, max>',
\Drupal::entityTypeManager()
->getStorage('node')
->getQuery()
->count()
->condition('status', 1)
->accessCheck(TRUE)
->execute()
);
14 changes: 7 additions & 7 deletions tests/src/Type/data/entity-query-execute.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@
->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')
->accessCheck(TRUE)
->execute()
);
assertType(
'int',
'int<0, max>',
\Drupal::entityQuery('node')
->accessCheck(TRUE)
->count()
Expand All @@ -48,7 +48,7 @@
assertType('array<int, string>', $query->execute());
$query = \Drupal::entityTypeManager()->getStorage('node')->getQuery()
->accessCheck(TRUE)->count();
assertType('int', $query->execute());
assertType('int<0, max>', $query->execute());

assertType(
'array<string, string>',
Expand All @@ -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()
Expand All @@ -87,4 +87,4 @@
assertType('array<string, string>', $query->execute());
$query = \Drupal::entityTypeManager()->getStorage('block')->getQuery()
->accessCheck(TRUE)->count();
assertType('int', $query->execute());
assertType('int<0, max>', $query->execute());
Loading