Skip to content
Merged
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
8 changes: 4 additions & 4 deletions src/Rules/Drupal/HookEntityOperationCacheabilityRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ public function processNode(Node $node, Scope $scope): array
$hookInstance = $hookAttribute->newInstance();

if ($hookInstance->hook === 'entity_operation') {
if (count($node->params) < 2 || !ParamHelper::isValidParam($node->params[1], 'Drupal\Core\Cache\CacheableMetadata', true, $scope)) {
if (count($node->params) < 2 || !ParamHelper::isValidParam($node->params[1], 'Drupal\Core\Cache\CacheableMetadata', false, $scope)) {
$errors[] = RuleErrorBuilder::message(
sprintf(
'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.',
'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 as the second parameter.',
$classReflection->getName(),
$methodName,
)
Expand All @@ -78,10 +78,10 @@ public function processNode(Node $node, Scope $scope): array
}

if ($hookInstance->hook === 'entity_operation_alter') {
if (count($node->params) < 3 || !ParamHelper::isValidParam($node->params[2], 'Drupal\Core\Cache\CacheableMetadata', true, $scope)) {
if (count($node->params) < 3 || !ParamHelper::isValidParam($node->params[2], 'Drupal\Core\Cache\CacheableMetadata', false, $scope)) {
$errors[] = RuleErrorBuilder::message(
sprintf(
'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.',
'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 as the third parameter.',
$classReflection->getName(),
$methodName,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ public function processNode(Node $node, Scope $scope): array
$hookName = substr_replace($functionName, 'hook', 0, strlen($moduleName));

if ($hookName === 'hook_entity_operation') {
if (count($node->params) < 2 || !ParamHelper::isValidParam($node->params[1], 'Drupal\Core\Cache\CacheableMetadata', true, $scope)) {
if (count($node->params) < 2 || !ParamHelper::isValidParam($node->params[1], 'Drupal\Core\Cache\CacheableMetadata', false, $scope)) {
return [
RuleErrorBuilder::message(
sprintf(
'Function %s() implements hook_entity_operation but is missing the CacheableMetadata parameter added in Drupal 11.3. Update the signature to: %s(\Drupal\Core\Entity\EntityInterface $entity, ?\Drupal\Core\Cache\CacheableMetadata $cacheability = NULL).',
'Function %s() implements hook_entity_operation but is missing the CacheableMetadata parameter added in Drupal 11.3. Update the signature to: %s(\Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Cache\CacheableMetadata $cacheability).',
$functionName,
$functionName,
)
Expand All @@ -69,11 +69,11 @@ public function processNode(Node $node, Scope $scope): array
}

if ($hookName === 'hook_entity_operation_alter') {
if (count($node->params) < 3 || !ParamHelper::isValidParam($node->params[2], 'Drupal\Core\Cache\CacheableMetadata', true, $scope)) {
if (count($node->params) < 3 || !ParamHelper::isValidParam($node->params[2], 'Drupal\Core\Cache\CacheableMetadata', false, $scope)) {
return [
RuleErrorBuilder::message(
sprintf(
'Function %s() implements hook_entity_operation_alter but is missing the CacheableMetadata parameter added in Drupal 11.3. Update the signature to: %s(array &$operations, \Drupal\Core\Entity\EntityInterface $entity, ?\Drupal\Core\Cache\CacheableMetadata $cacheability = NULL).',
'Function %s() implements hook_entity_operation_alter but is missing the CacheableMetadata parameter added in Drupal 11.3. Update the signature to: %s(array &$operations, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Cache\CacheableMetadata $cacheability).',
$functionName,
$functionName,
)
Expand Down
6 changes: 3 additions & 3 deletions tests/src/Rules/HookEntityOperationCacheabilityRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ public function testRule(): void
[__DIR__ . '/data/hook-entity-operation-cacheability.php'],
$isApplicableVersion ? [
[
'Method BadEntityOperationHook::entityOperation() 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.',
'Method BadEntityOperationHook::entityOperation() 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 as the second parameter.',
10,
'See https://www.drupal.org/node/3533080',
],
[
'Method BadEntityOperationAlterHookOneParam::entityOperationAlter() 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.',
'Method BadEntityOperationAlterHookOneParam::entityOperationAlter() 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 as the third parameter.',
20,
'See https://www.drupal.org/node/3533080',
],
[
'Method BadEntityOperationAlterHookTwoParams::entityOperationAlter() 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.',
'Method BadEntityOperationAlterHookTwoParams::entityOperationAlter() 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 as the third parameter.',
29,
'See https://www.drupal.org/node/3533080',
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public function testRule(): void
[__DIR__ . '/data/mymodule.module'],
$isApplicableVersion ? [
[
'Function mymodule_entity_operation() implements hook_entity_operation but is missing the CacheableMetadata parameter added in Drupal 11.3. Update the signature to: mymodule_entity_operation(\Drupal\Core\Entity\EntityInterface $entity, ?\Drupal\Core\Cache\CacheableMetadata $cacheability = NULL).',
'Function mymodule_entity_operation() implements hook_entity_operation but is missing the CacheableMetadata parameter added in Drupal 11.3. Update the signature to: mymodule_entity_operation(\Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Cache\CacheableMetadata $cacheability).',
7,
'See https://www.drupal.org/node/3533080',
],
[
'Function mymodule_entity_operation_alter() implements hook_entity_operation_alter but is missing the CacheableMetadata parameter added in Drupal 11.3. Update the signature to: mymodule_entity_operation_alter(array &$operations, \Drupal\Core\Entity\EntityInterface $entity, ?\Drupal\Core\Cache\CacheableMetadata $cacheability = NULL).',
'Function mymodule_entity_operation_alter() implements hook_entity_operation_alter but is missing the CacheableMetadata parameter added in Drupal 11.3. Update the signature to: mymodule_entity_operation_alter(array &$operations, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Cache\CacheableMetadata $cacheability).',
12,
'See https://www.drupal.org/node/3533080',
],
Expand Down
25 changes: 22 additions & 3 deletions tests/src/Rules/data/hook-entity-operation-cacheability.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,37 @@ public function entityOperationAlter(array &$operations, EntityInterface $entity
}
}

// Good: entity_operation with CacheableMetadata.
// Good: entity_operation with non-nullable CacheableMetadata (canonical form).
class GoodEntityOperationHook
{
#[Hook('entity_operation')]
public function entityOperation(EntityInterface $entity, ?CacheableMetadata $cacheability = null): array
public function entityOperation(EntityInterface $entity, CacheableMetadata $cacheability): array
{
return [];
}
}

// Good: entity_operation_alter with CacheableMetadata.
// Good: entity_operation_alter with non-nullable CacheableMetadata (canonical form).
class GoodEntityOperationAlterHook
{
#[Hook('entity_operation_alter')]
public function entityOperationAlter(array &$operations, EntityInterface $entity, CacheableMetadata $cacheability): void
{
}
}

// Good: nullable CacheableMetadata also accepted.
class GoodEntityOperationHookNullable
{
#[Hook('entity_operation')]
public function entityOperation(EntityInterface $entity, ?CacheableMetadata $cacheability = null): array
{
return [];
}
}

// Good: nullable CacheableMetadata also accepted for alter.
class GoodEntityOperationAlterHookNullable
{
#[Hook('entity_operation_alter')]
public function entityOperationAlter(array &$operations, EntityInterface $entity, ?CacheableMetadata $cacheability = null): void
Expand Down
18 changes: 18 additions & 0 deletions tests/src/Rules/data/mymodule.module
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,21 @@ function mymodule_entity_operation_alter(array &$operations, EntityInterface $en
function other_module_entity_operation(EntityInterface $entity): array {
return [];
}

// Good: non-nullable CacheableMetadata (canonical form).
function mymodule_entity_operation_good(EntityInterface $entity, CacheableMetadata $cacheability): array {
return [];
}

// Good: nullable CacheableMetadata also accepted.
function mymodule_entity_operation_good_nullable(EntityInterface $entity, ?CacheableMetadata $cacheability = null): array {
Comment thread
mglaman marked this conversation as resolved.
return [];
}

// Good: entity_operation_alter with non-nullable CacheableMetadata.
function mymodule_entity_operation_alter_good(array &$operations, EntityInterface $entity, CacheableMetadata $cacheability): void {
}

// Good: entity_operation_alter with nullable CacheableMetadata also accepted.
function mymodule_entity_operation_alter_good_nullable(array &$operations, EntityInterface $entity, ?CacheableMetadata $cacheability = null): void {
}
Loading