-
-
Notifications
You must be signed in to change notification settings - Fork 83
Detect calling addCacheableDependency on non-CacheableDependencyInterface values #913
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
Merged
mglaman
merged 11 commits into
mglaman:main
from
mariano-dagostino:cacheable-dependency
Oct 16, 2025
Merged
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1cb8e83
New rule to detect incorrect use of addCacheableDependency
mariano-dagostino 383c16d
Fix coding standards
mariano-dagostino a70ef94
Fix coding standards
mariano-dagostino 769af3a
Use nodeFinder
mariano-dagostino 6c912d8
Merge branch 'main' into cacheable-dependency
mglaman f38a800
Fixup CacheableDependencyRule
mglaman f3e070b
Use type check and add test cases
mglaman b7218bc
handle different interfaces with addCacheableDependency
mglaman 9b7d4e9
make sure the rule is behind a feature flag
mglaman 046448e
fix a bug on renderer check
mglaman abe945c
remove old file
mglaman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace mglaman\PHPStanDrupal\Rules\Drupal; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Identifier; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Rules\RuleErrorBuilder; | ||
| use PHPStan\Type\ObjectType; | ||
|
|
||
| /** | ||
| * @implements Rule<Node\Expr\MethodCall> | ||
| */ | ||
| class CacheableDependencyRule implements Rule | ||
| { | ||
|
|
||
| public function getNodeType(): string | ||
| { | ||
| return Node\Expr\MethodCall::class; | ||
| } | ||
|
|
||
| public function processNode(Node $node, Scope $scope): array | ||
| { | ||
| if (!$node->name instanceof Identifier || $node->name->toString() !== 'addCacheableDependency') { | ||
| return []; | ||
| } | ||
|
|
||
| $args = $node->getArgs(); | ||
| if (count($args) === 0) { | ||
| return []; | ||
| } | ||
|
|
||
| $dependencyArg = $args[0]->value; | ||
| $object = $scope->getType($dependencyArg); | ||
|
|
||
| $interfaceType = new ObjectType('Drupal\Core\Cache\CacheableDependencyInterface'); | ||
| $implementsInterface = $interfaceType->isSuperTypeOf($object); | ||
|
|
||
| if (!$implementsInterface->no()) { | ||
| return []; | ||
| } | ||
|
|
||
| return [ | ||
| RuleErrorBuilder::message('Calling addCacheableDependency($object) when $object does not implement CacheableDependencyInterface effectively disables caching and should be avoided.') | ||
| ->identifier('cacheable.dependency') | ||
| ->build(), | ||
| ]; | ||
| } | ||
| } |
15 changes: 15 additions & 0 deletions
15
tests/fixtures/drupal/modules/phpstan_fixtures/src/UsesIncorrectCacheableDependency.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <?php | ||
|
|
||
| namespace Drupal\phpstan_fixtures; | ||
|
|
||
| use Drupal\Core\Cache\CacheableMetadata; | ||
|
|
||
| class UsesIncorrectCacheableDependency { | ||
| public function test() { | ||
| $element = []; | ||
| $cacheable_metadata = CacheableMetadata::createFromRenderArray($element); | ||
|
|
||
| $object = new \Stdclass; | ||
| $cacheable_metadata->addCacheableDependency($object); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace mglaman\PHPStanDrupal\Tests\Rules; | ||
|
|
||
| use mglaman\PHPStanDrupal\Rules\Drupal\CacheableDependencyRule; | ||
| use mglaman\PHPStanDrupal\Tests\DrupalRuleTestCase; | ||
|
|
||
| final class CachableDependencyRuleTest extends DrupalRuleTestCase { | ||
|
|
||
| protected function getRule(): \PHPStan\Rules\Rule | ||
| { | ||
| return new CacheableDependencyRule(); | ||
| } | ||
|
|
||
| /** | ||
| * @dataProvider resultData | ||
| * | ||
| * @param list<array{0: string, 1: int, 2?: string|null}> $errorMessages | ||
| */ | ||
| public function testRule(string $path, array $errorMessages): void | ||
| { | ||
| $this->analyse([$path], $errorMessages); | ||
| } | ||
|
|
||
| public static function resultData(): \Generator | ||
| { | ||
| yield 'all test cases' => [ | ||
| __DIR__ . '/data/cacheable-dependency.php', | ||
| [ | ||
| [ | ||
| 'Calling addCacheableDependency($object) when $object does not implement CacheableDependencyInterface effectively disables caching and should be avoided.', | ||
| 13 | ||
| ], | ||
| [ | ||
| 'Calling addCacheableDependency($object) when $object does not implement CacheableDependencyInterface effectively disables caching and should be avoided.', | ||
| 39 | ||
| ], | ||
| [ | ||
| 'Calling addCacheableDependency($object) when $object does not implement CacheableDependencyInterface effectively disables caching and should be avoided.', | ||
| 43 | ||
| ], | ||
| ] | ||
| ]; | ||
| } | ||
|
|
||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| <?php | ||
|
|
||
| namespace Drupal\phpstan_fixtures; | ||
|
|
||
| use Drupal\Core\Cache\CacheableMetadata; | ||
|
|
||
| class UsesIncorrectCacheableDependency { | ||
| public function test() { | ||
| $element = []; | ||
| $cacheable_metadata = CacheableMetadata::createFromRenderArray($element); | ||
|
|
||
| $object = new \Stdclass; | ||
| $cacheable_metadata->addCacheableDependency($object); | ||
| } | ||
| } | ||
|
|
||
| class UsesCorrectCacheableDependency { | ||
| public function test() { | ||
| $element = []; | ||
| $cacheable_metadata = CacheableMetadata::createFromRenderArray($element); | ||
|
|
||
| // This should NOT trigger an error - CacheableMetadata implements CacheableDependencyInterface | ||
| $correct_dependency = new CacheableMetadata(); | ||
| $cacheable_metadata->addCacheableDependency($correct_dependency); | ||
| } | ||
| } | ||
|
|
||
| class MultipleCacheableDependencyCalls { | ||
| public function test() { | ||
| $element = []; | ||
| $cacheable_metadata = CacheableMetadata::createFromRenderArray($element); | ||
|
|
||
| // Correct usage | ||
| $correct_dependency = new CacheableMetadata(); | ||
| $cacheable_metadata->addCacheableDependency($correct_dependency); | ||
|
|
||
| // Incorrect usage - should trigger error | ||
| $object = new \StdClass; | ||
| $cacheable_metadata->addCacheableDependency($object); | ||
|
|
||
| // Another incorrect usage - should trigger error | ||
| $another_object = new \DateTime(); | ||
| $cacheable_metadata->addCacheableDependency($another_object); | ||
| } | ||
| } | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.