You are here

function coder_upgrade_upgrade_hook_load_alter in Coder 7

Same name and namespace in other branches
  1. 7.2 coder_upgrade/conversions/function.inc \coder_upgrade_upgrade_hook_load_alter()

Implements hook_upgrade_hook_load_alter().

File

coder_upgrade/conversions/function.inc, line 400
Provides conversion routines applied to functions (or hooks).

Code

function coder_upgrade_upgrade_hook_load_alter(&$node, &$reader) {

  // Changes: hook_load_signature
  $item =& $node->data;
  $body =& $item->body;

  // Create helper objects.
  $editor = PGPEditor::getInstance();

  // Save the name of first parameter.
  if ($p0 = $item
    ->getParameterVariable()) {

    // The parameter includes a variable; save the variable.
    $p0 = $p0
      ->toString();
    $p0_new = $p0 . 's';
  }
  else {

    // The parameter has no variable.
    $p0 = '$xx_missing_xx';
    $p0_new = '$nodes';
    $body
      ->insertFirst($editor
      ->commentToStatement('// TODO: Name the variable ' . $p0 . ' which was missing in the old function signature.'));
  }

  // Add 's' to name of first parameter.
  $editor
    ->setParameter($item, 0, $p0_new);

  // Search for a return statement.
  if (!($return =& $body
    ->find(T_RETURN, 'reverse'))) {
    $msg = 'ERROR: return statement not found in hook_load';
    clp($msg);
    $body
      ->insertFirst($editor
      ->commentToStatement($msg), 'comment');
    return;
  }
  $value =& $return
    ->getParameter();
  $return_parameter = $value
    ->toString();
  if (!$value
    ->isType(T_VARIABLE)) {

    // Insert an assignment statement with the value being the return operand.
    $statement = $editor
      ->textToStatements('$node_additions = ' . $value
      ->toString())
      ->getElement();
    $return
      ->insertStatementBefore($statement);

    // Replace the operand to the return statement with a variable.
    $return = $editor
      ->textToStatements('return $node_additions')
      ->getElement();
    $return_parameter = '$node_additions';
  }

  // Replace the return statement with a foreach loop on each node object.
  $strings[] = 'foreach (' . $return_parameter . ' as $property => &$value) {';
  $strings[] = '  ' . $p0 . '->$property = $value;';
  $strings[] = '}';
  $return = $editor
    ->textToStatements(implode("\n", $strings))
    ->getElement();

  // Wrap the body in a foreach loop on $nodes.
  unset($strings);
  $strings[] = 'foreach (' . $p0_new . ' as $nid => &' . $p0 . ') {';
  $strings[] = '}';
  $loop = $editor
    ->textToStatements(implode("\n", $strings))
    ->getElement();
  $loop->body = $item->body;
  $body = new PGPBody();
  $body
    ->insertLast($loop);
  $item->body = $body;
}