You are here

public function LoadMultiple::rewrite in Drupal 7 to 8/9 Module Upgrader 8

Tries to rewrite the original function call.

Parameters

\Pharborist\Functions\FunctionCallNode $call: The original function call.

\Drupal\drupalmoduleupgrader\TargetInterface $target: The target module.

Return value

\Pharborist\Node|null If the original function call is returned (determined by object identity), the function call is not replaced. If a different node is returned, it will replace the original call. And if nothing is returned, the original call is commented out with a FIXME.

Overrides FunctionCallModifier::rewrite

File

src/Plugin/DMU/Converter/Functions/LoadMultiple.php, line 20

Class

LoadMultiple
Plugin annotation @Converter( id = "_load_multiple", deriver = "\Drupal\drupalmoduleupgrader\Plugin\DMU\Converter\Functions\LoadMultipleDeriver" )

Namespace

Drupal\drupalmoduleupgrader\Plugin\DMU\Converter\Functions

Code

public function rewrite(FunctionCallNode $call, TargetInterface $target) {
  $arguments = $call
    ->getArguments();

  // If there were three arguments, the call is affecting the internal
  // entity cache. Unfortunately, it's pretty much impossible to reliably
  // determine whether or not they wanted to reset the cache, so let's just
  // leave a FIXME.
  if (sizeof($arguments) == 3) {
    $variables = [
      '@entity_type' => $this->pluginDefinition['entity_type'],
    ];
    $this
      ->buildFixMe('To reset the @entity_type cache, use EntityStorageInterface::resetCache().', $variables)
      ->insertBefore($call);
  }
  $rewritten = ClassMethodCallNode::create('\\Drupal', 'entityTypeManager')
    ->appendMethodCall('getStorage')
    ->appendArgument($this->pluginDefinition['entity_type']);

  // If there's more than one argument, conditions were passed (not a
  // recommended practice, but modules might have done it anyway), in which
  // case we need to use loadByProperties(). Otherwise, loadMultiple().
  if (sizeof($arguments) > 1) {
    return $rewritten
      ->appendMethodCall('loadByProperties')
      ->appendArgument(clone $arguments[1]);
  }
  else {
    return $rewritten
      ->appendMethodCall('loadMultiple')
      ->appendArgument(clone $arguments[0]);
  }
}