diff --git a/rector_examples/node_load_multiple.php b/rector_examples/node_load_multiple.php new file mode 100644 index 00000000..8ff7ee06 --- /dev/null +++ b/rector_examples/node_load_multiple.php @@ -0,0 +1,28 @@ +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); +} diff --git a/src/Rector/Deprecation/Base/EntityLoadBase.php b/src/Rector/Deprecation/Base/EntityLoadBase.php index 9d09ef59..f1fbaf73 100644 --- a/src/Rector/Deprecation/Base/EntityLoadBase.php +++ b/src/Rector/Deprecation/Base/EntityLoadBase.php @@ -57,9 +57,12 @@ public function refactor(Node $node): ?Node // This will work for node_load, etc. $method_name = $this->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 + ) + ]); + } + +}