Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False negative accessCheck failure on non-chained entity queries #497

Merged
merged 5 commits into from
Feb 1, 2023

Conversation

Boegie
Copy link
Contributor

@Boegie Boegie commented Jan 4, 2023

Testcases for #496.

@Boegie
Copy link
Contributor Author

Boegie commented Jan 4, 2023

I would expect no failures here.

Instead I get:

There was 1 failure:

1) mglaman\PHPStanDrupal\Tests\Rules\EntityQueryHasAccessCheckRuleTest::test with data set "bug-496.php" (array('/home/runner/work/phpstan-dru...96.php'), array())
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'
+'15: Relying on entity queries to check access by default is deprecated in drupal:9.2.0 and an error will be thrown from drupal:10.0.0. Call \Drupal\Core\Entity\Query\QueryInterface::accessCheck() with TRUE or FALSE to specify whether access should be checked.
+    💡 See https://www.drupal.org/node/3201242
+20: Relying on entity queries to check access by default is deprecated in drupal:9.2.0 and an error will be thrown from drupal:10.0.0. Call \Drupal\Core\Entity\Query\QueryInterface::accessCheck() with TRUE or FALSE to specify whether access should be checked.
+    💡 See https://www.drupal.org/node/3201242

Note: Both entity queries pass when used chained. so without the $query-bit on each line.

# Conflicts:
#	tests/src/Rules/EntityQueryHasAccessCheckRuleTest.php
@mglaman
Copy link
Owner

mglaman commented Jan 25, 2023

It breaks down due to PHPStan not tracking the side effect of calling accessCheck, which modifies the state of the query.

In \PHPStan\Analyser\NodeScopeResolver::processStmtNode we have:

        } elseif ($stmt instanceof Node\Stmt\Expression) {
            $earlyTerminationExpr = $this->findEarlyTerminatingExpr($stmt->expr, $scope);
            $result = $this->processExprNode($stmt->expr, $scope, $nodeCallback, \PHPStan\Analyser\ExpressionContext::createTopLevel());

Screenshot 2023-01-25 at 3 17 31 PM

$query is in its initial state of not being flagged. But $query->accessCheck(FALSE); has itself considered as returning the query with access check. But this does not update the existing $query resolved type because it's a side effect not a return type.

See what can be done in \PHPStan\Analyser\NodeScopeResolver::processExprNode EDIT: processExprNode is too late, this runs the rules.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants