|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace mglaman\PHPStanDrupal\Rules\Drupal; |
| 4 | + |
| 5 | +use Drupal; |
| 6 | +use Drupal\Core\Hook\Attribute\Hook; |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Stmt\ClassMethod; |
| 9 | +use PHPStan\Analyser\Scope; |
| 10 | +use PHPStan\Rules\Rule; |
| 11 | +use PHPStan\Rules\RuleErrorBuilder; |
| 12 | +use function class_exists; |
| 13 | +use function count; |
| 14 | +use function version_compare; |
| 15 | + |
| 16 | +/** |
| 17 | + * Detects OOP hook implementations of hook_entity_operation and |
| 18 | + * hook_entity_operation_alter that are missing the CacheableMetadata |
| 19 | + * parameter added in Drupal 11.3. |
| 20 | + * |
| 21 | + * @implements Rule<ClassMethod> |
| 22 | + */ |
| 23 | +class HookEntityOperationCacheabilityRule implements Rule |
| 24 | +{ |
| 25 | + |
| 26 | + public function getNodeType(): string |
| 27 | + { |
| 28 | + return ClassMethod::class; |
| 29 | + } |
| 30 | + |
| 31 | + public function processNode(Node $node, Scope $scope): array |
| 32 | + { |
| 33 | + if (!class_exists(Hook::class)) { |
| 34 | + return []; |
| 35 | + } |
| 36 | + |
| 37 | + if (version_compare(Drupal::VERSION, '11.3', '<')) { |
| 38 | + return []; |
| 39 | + } |
| 40 | + |
| 41 | + if (!$scope->isInClass()) { |
| 42 | + return []; |
| 43 | + } |
| 44 | + |
| 45 | + $classReflection = $scope->getClassReflection(); |
| 46 | + $nativeClass = $classReflection->getNativeReflection(); |
| 47 | + $methodName = $node->name->toString(); |
| 48 | + |
| 49 | + if (!$nativeClass->hasMethod($methodName)) { |
| 50 | + return []; |
| 51 | + } |
| 52 | + |
| 53 | + $nativeMethod = $nativeClass->getMethod($methodName); |
| 54 | + $hookAttributes = $nativeMethod->getAttributes(Hook::class); |
| 55 | + |
| 56 | + if (count($hookAttributes) === 0) { |
| 57 | + return []; |
| 58 | + } |
| 59 | + |
| 60 | + $errors = []; |
| 61 | + foreach ($hookAttributes as $hookAttribute) { |
| 62 | + /** @var Hook $hookInstance */ |
| 63 | + $hookInstance = $hookAttribute->newInstance(); |
| 64 | + |
| 65 | + if ($hookInstance->hook === 'entity_operation') { |
| 66 | + if (count($node->params) < 2 || !ParamHelper::isValidParam($node->params[1], 'Drupal\Core\Cache\CacheableMetadata', true, $scope)) { |
| 67 | + $errors[] = RuleErrorBuilder::message( |
| 68 | + sprintf( |
| 69 | + 'Method %s::%s() implements hook_entity_operation but is missing the CacheableMetadata parameter added in Drupal 11.3. Update the signature to include ?\Drupal\Core\Cache\CacheableMetadata $cacheability = NULL as the second parameter.', |
| 70 | + $classReflection->getName(), |
| 71 | + $methodName, |
| 72 | + ) |
| 73 | + ) |
| 74 | + ->tip('See https://www.drupal.org/node/3533080') |
| 75 | + ->identifier('drupal.hookEntityOperationMissingCacheabilityParameter') |
| 76 | + ->build(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + if ($hookInstance->hook === 'entity_operation_alter') { |
| 81 | + if (count($node->params) < 3 || !ParamHelper::isValidParam($node->params[2], 'Drupal\Core\Cache\CacheableMetadata', true, $scope)) { |
| 82 | + $errors[] = RuleErrorBuilder::message( |
| 83 | + sprintf( |
| 84 | + 'Method %s::%s() implements hook_entity_operation_alter but is missing the CacheableMetadata parameter added in Drupal 11.3. Update the signature to include ?\Drupal\Core\Cache\CacheableMetadata $cacheability = NULL as the third parameter.', |
| 85 | + $classReflection->getName(), |
| 86 | + $methodName, |
| 87 | + ) |
| 88 | + ) |
| 89 | + ->tip('See https://www.drupal.org/node/3533080') |
| 90 | + ->identifier('drupal.hookEntityOperationAlterMissingCacheabilityParameter') |
| 91 | + ->build(); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return $errors; |
| 97 | + } |
| 98 | +} |
0 commit comments