Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ rules:
- mglaman\PHPStanDrupal\Rules\Drupal\LoadIncludes
- mglaman\PHPStanDrupal\Rules\Drupal\EntityQuery\EntityQueryHasAccessCheckRule
- mglaman\PHPStanDrupal\Rules\Drupal\TestClassesProtectedPropertyModulesRule
- mglaman\PHPStanDrupal\Rules\Drupal\CacheableDependencyRule

conditionalTags:
mglaman\PHPStanDrupal\Rules\Drupal\Tests\TestClassSuffixNameRule:
Expand Down
48 changes: 48 additions & 0 deletions src/Rules/Drupal/CacheableDependencyRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace mglaman\PHPStanDrupal\Rules\Drupal;

use PhpParser\Node;
use PhpParser\NodeFinder;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;

/**
* @implements Rule<Node\Expr\StaticCall>
*/
class CacheableDependencyRule implements Rule
{

public function getNodeType(): string
{
return Node\Expr\MethodCall::class;
}

public function processNode(Node $node, Scope $scope): array
{
$nodeFinder = new NodeFinder();
$method = $nodeFinder->findFirst($class->stmts, static function (Node $node) {
return $node instanceof Node\Stmt\ClassMethod && $node->name->toString() === 'addCacheableDependency';
});
if (!$method instanceof Node\Stmt\ClassMethod) {
return [];
}

$args = $node->getArgs();
$traversableArg = $args[0]->value;
$object = $scope->getType($traversableArg);

// We need to check if isInstanceOf method exists as phpstan returns
// MixedType for unknown objects.
if (method_exists($object, 'isInstanceOf') && $object->isInstanceOf('Drupal\Core\Cache\CacheableDependencyInterface')) {
return [];
}
return [
'Calling addCacheableDependency($object) when $object does not implement CacheableDependencyInterface effectively disables caching and should be avoided.',
];
}
}
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);
}
}
40 changes: 40 additions & 0 deletions tests/src/Rules/CachableDependencyRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?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 [
__DIR__ . '/../../fixtures/drupal/modules/phpstan_fixtures/src/UsesIncorrectCacheableDependency.php',
[
[
'Calling addCacheableDependency($object) when $object does not implement CacheableDependencyInterface effectively disables caching and should be avoided.',
07
],
]
];

}


}
Loading