From 2a0e11a64ff8efa03dd5ba1ccdc5e5b47cbd52d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Herre=C3=B1o?= Date: Fri, 21 May 2021 13:07:38 -0500 Subject: [PATCH 1/2] Implement rule for node_load_multiple() --- rector_examples/node_load_multiple.php | 29 +++++++++++++ .../Deprecation/Base/EntityLoadBase.php | 23 +++++----- .../Deprecation/NodeLoadMultipleRector.php | 42 +++++++++++++++++++ 3 files changed, 84 insertions(+), 10 deletions(-) create mode 100644 rector_examples/node_load_multiple.php create mode 100644 src/Rector/Deprecation/NodeLoadMultipleRector.php diff --git a/rector_examples/node_load_multiple.php b/rector_examples/node_load_multiple.php new file mode 100644 index 00000000..22ff439d --- /dev/null +++ b/rector_examples/node_load_multiple.php @@ -0,0 +1,29 @@ +entityType . '_load'; } + $method_name_multiple = $method_name . '_multiple'; /** @var Node\Expr\FuncCall $node */ - if ($this->getName($node->name) === $method_name) { + if (in_array($this->getName($node->name), [$method_name, $method_name_multiple])) { + $is_multiple = $this->getName($node->name) === $method_name_multiple; + // We are doing this here, because we know we have access to arguments since we have already checked the method name. if ($is_rector_rule_entity_load) { // Since we have one more argument, all the array keys are one greater. @@ -76,8 +79,8 @@ public function refactor(Node $node): ?Node $entity_type = new Node\Arg(new Node\Scalar\String_($this->entityType)); } - /* @var Node\Arg $entity_id. */ - $entity_id = $node->args[0 + $argument_offset]; + /* @var Node\Arg $entity_ids. */ + $entity_ids = $node->args[0 + $argument_offset]; $name = new Node\Name\FullyQualified('Drupal'); @@ -95,9 +98,9 @@ public function refactor(Node $node): ?Node $getStorage_node = new Node\Expr\MethodCall($var, $getStorage_method_name, [$entity_type]); // Create the simple version of the entity load. - $load_method_name = new Node\Identifier('load'); + $load_method_name = new Node\Identifier($is_multiple ? 'loadMultiple' : 'load'); - $new_node = new Node\Expr\MethodCall($getStorage_node, $load_method_name, [$entity_id]); + $new_node = new Node\Expr\MethodCall($getStorage_node, $load_method_name, [$entity_ids]); // We need to account for the `reset` option which adds a method to the chain. // We will replace the original method with a ternary to evaluate and provide both options. @@ -109,14 +112,14 @@ public function refactor(Node $node): ?Node $resetCache_method_name = new Node\Identifier('resetCache'); - $reset_args = [ + $reset_args = $is_multiple ? + $entity_ids : // This creates a new argument that wraps the entity ID in an array. - new Node\Arg(new Node\Expr\Array_([new Node\Expr\ArrayItem($entity_id->value)])), - ]; + new Node\Arg(new Node\Expr\Array_([new Node\Expr\ArrayItem($entity_ids->value)])); - $entity_load_reset_node = new Node\Expr\MethodCall($getStorage_node, $resetCache_method_name, $reset_args); + $entity_load_reset_node = new Node\Expr\MethodCall($getStorage_node, $resetCache_method_name, [$reset_args]); - $entity_load_reset_node = new Node\Expr\MethodCall($entity_load_reset_node, $load_method_name, [$entity_id]); + $entity_load_reset_node = new Node\Expr\MethodCall($entity_load_reset_node, $load_method_name, [$entity_ids]); // Replace the new_node with a ternary. $new_node = new Node\Expr\Ternary($reset_flag->value, $entity_load_reset_node, $new_node); diff --git a/src/Rector/Deprecation/NodeLoadMultipleRector.php b/src/Rector/Deprecation/NodeLoadMultipleRector.php new file mode 100644 index 00000000..15b8b99a --- /dev/null +++ b/src/Rector/Deprecation/NodeLoadMultipleRector.php @@ -0,0 +1,42 @@ +getStorage('node')->loadMultiple([123, 456]); +CODE_AFTER + ) + ]); + } + +} From 15cf72ffd57cb2fd05bc0f9a3181dc2a14af4143 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Herre=C3=B1o?= Date: Fri, 21 May 2021 13:32:11 -0500 Subject: [PATCH 2/2] Add rector_examples_updated/node_load_multiple.php --- rector_examples/node_load_multiple.php | 3 +- .../node_load_multiple.php | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 rector_examples_updated/node_load_multiple.php diff --git a/rector_examples/node_load_multiple.php b/rector_examples/node_load_multiple.php index 22ff439d..8ff7ee06 100644 --- a/rector_examples/node_load_multiple.php +++ b/rector_examples/node_load_multiple.php @@ -15,8 +15,7 @@ function simple_example() { * An example using all of the arguments. */ function all_arguments() { - /* @var \Drupal\node\Entity\Node $node */ - $node = node_load_multiple([123, 456], TRUE); + $nodes = node_load_multiple([123, 456], TRUE); } /** diff --git a/rector_examples_updated/node_load_multiple.php b/rector_examples_updated/node_load_multiple.php new file mode 100644 index 00000000..35a5836e --- /dev/null +++ b/rector_examples_updated/node_load_multiple.php @@ -0,0 +1,32 @@ +getStorage('node')->loadMultiple([123, 456]); +} + +/** + * An example using all of the arguments. + */ +function all_arguments() { + // TODO: Drupal Rector Notice: Please delete the following comment after you've made any necessary changes. + // A ternary operator is used here to keep the conditional contained within this part of the expression. Consider wrapping this statement in an `if / else` statement. + $nodes = TRUE ? \Drupal::service('entity_type.manager')->getStorage('node')->resetCache([123, 456])->loadMultiple([123, 456]) : \Drupal::service('entity_type.manager')->getStorage('node')->loadMultiple([123, 456]); +} + +/** + * An example using a variable for the argument. + */ +function all_arguments_as_variables() { + $node_ids = [123, 456]; + $reset = TRUE; + // TODO: Drupal Rector Notice: Please delete the following comment after you've made any necessary changes. + // A ternary operator is used here to keep the conditional contained within this part of the expression. Consider wrapping this statement in an `if / else` statement. + $nodes = $reset ? \Drupal::service('entity_type.manager')->getStorage('node')->resetCache($node_ids)->loadMultiple($node_ids) : \Drupal::service('entity_type.manager')->getStorage('node')->loadMultiple($node_ids); +}