You are here

public function DrupalWriteRecord::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/DrupalWriteRecord.php, line 22

Class

DrupalWriteRecord
Plugin annotation @Converter( id = "drupal_write_record", description = @Translation("Rewrites calls to drupal_write_record().") )

Namespace

Drupal\drupalmoduleupgrader\Plugin\DMU\Converter\Functions

Code

public function rewrite(FunctionCallNode $call, TargetInterface $target) {
  $rewritten = ClassMethodCallNode::create('\\Drupal', 'database');
  $arguments = $call
    ->getArguments();
  if (sizeof($arguments) == 3) {
    $key = $arguments[2] instanceof StringNode ? ArrayNode::create([
      clone $arguments[2],
    ]) : clone $arguments[2];
    return $rewritten
      ->appendMethodCall('merge')
      ->appendArgument(clone $arguments[0])
      ->appendMethodCall('fields')
      ->appendArgument(clone $arguments[1])
      ->appendMethodCall('key')
      ->appendArgument($key)
      ->appendMethodCall('execute');
  }
  else {
    return $rewritten
      ->appendMethodCall('insert')
      ->appendArgument(clone $arguments[0])
      ->appendMethodCall('fields')
      ->appendArgument(clone $arguments[1])
      ->appendMethodCall('execute');
  }
}