phpstan-drupal icon indicating copy to clipboard operation
phpstan-drupal copied to clipboard

Call to method Drupal\Core\Entity\Query\QueryInterface::accessCheck() will always evaluate to true (PHPStan 2)

Open WalkingDexter opened this issue 11 months ago • 7 comments

Bug report

After upgrading to PHPStan 2, errors related to the accessCheck() method occurred. A similar problem was previously solved in #508.

Versions used:

  • phpstan/phpstan 2.1.4
  • mglaman/phpstan-drupal 2.0.1

Code snippet that reproduces the problem

// Error: method.alreadyNarrowedType
\Drupal::entityQuery('node')
  ->accessCheck()
  ->execute();

// Error: method.alreadyNarrowedType
\Drupal::entityQuery('node')
  ->accessCheck()
  ->condition('type', 'article')
  ->execute();

// No error.
\Drupal::entityQuery('node')
  ->condition('type', 'article')
  ->accessCheck()
  ->execute();

// Error: method.alreadyNarrowedType
$query = \Drupal::entityQuery('node');
$query->condition('type', 'article');
$query->accessCheck();
$query->execute();

Public example from GitLab CI: https://git.drupalcode.org/project/simple_sitemap/-/jobs/4303395

WalkingDexter avatar Feb 11 '25 12:02 WalkingDexter

Having the same issue in some projects, I can't replicate it with just testing:

<?php

use function PHPStan\Testing\assertType;

// Error: method.alreadyNarrowedType
$result = \Drupal::entityQuery('node')->accessCheck()->execute();
assertType('array<int, string>', $result);

// Error: method.alreadyNarrowedType
$result = \Drupal::entityQuery('node')->accessCheck()->condition('type', 'article')->execute();
assertType('array<int, string>', $result);

// No error.
$result = \Drupal::entityQuery('node')->condition('type', 'article')->accessCheck()->execute();
assertType('array<int, string>', $result);

// Error: method.alreadyNarrowedType
$query = \Drupal::entityQuery('node');
$query->condition('type', 'article');
$query->accessCheck();
$result = $query->execute();
assertType('array<int, string>', $result);
[email protected]:/var/www/html $ phpunit --filter EntityQueryDynamicReturnTypeExtensionTest
PHPUnit 9.6.22 by Sebastian Bergmann and contributors.

Warning:       No code coverage driver available

................................                                  32 / 32 (100%)

Time: 00:00.510, Memory: 150.50 MB

OK (32 tests, 32 assertions)
  • PHPStan 2.1.5
  • This package from the main branch (version 2.0.1).

This problem started to emerge last week. It seems that the mainstream has some changes affecting entity query, but I couldn't find any.

Niklan avatar Feb 16 '25 12:02 Niklan

Also, I copied the source code from the specific commit where the CI failed: https://git.drupalcode.org/project/simple_sitemap/-/blob/d05f3fb285f020e341feb005a932ea297ad58a4f/src/Entity/EntityHelper.php

Added assertion after 253 line:

    $entity_query = $this->entityTypeManager
      ->getStorage($entity_type_id)
      ->getQuery()
      ->accessCheck(TRUE);
    assertType(QueryInterface::class, $entity_query);

And the test is passing again. We need to find out what changes have been made to the Drupal core that affect it, because they are not reproducible without Drupal itself.

P.s. Also tried to change drupal core fixture to 11.1.2 and 11.x-dev 4c31340, still no issues detected.

Niklan avatar Feb 16 '25 13:02 Niklan

Unfortunately the entire code for marking an entity query as having been flagged for entity access check is super flakey

mglaman avatar Apr 03 '25 20:04 mglaman

Just started seeing this as well

mglaman avatar Jun 17 '25 19:06 mglaman

Another example:

Call to method Drupal\Core\Entity\Query\QueryInterface::accessCheck() with false will always evaluate to true.

I face the error on Drupal 11.2.x branch where PHPStan is v2.1.17 and mglaman/phpstan-drupal v2.0.7, but not on 11.1.x with PHPStan is v1.12.27.

RoSk0 avatar Jun 26 '25 01:06 RoSk0

I am also seeing this as of Drupal 11.2+.

Unfortunately the entire code for marking an entity query as having been flagged for entity access check is super flakey

@mglaman if I were to try digging into this do you have any recommendations where to start?

mstenta avatar Jul 02 '25 22:07 mstenta

Just saw a error with the same error message was resolved in https://github.com/phpstan/phpstan/issues/13291. Is there anything in the diff that can be borrowed here?

(and no, updating PHPStan to the version with above doesnt fix the problem reported here)

dpi avatar Jul 29 '25 05:07 dpi