You are here

function coder_upgrade_convert_return_OLD in Coder 7.2

Same name and namespace in other branches
  1. 7 coder_upgrade/conversions/tool.inc \coder_upgrade_convert_return_OLD()

Initiates the transformation of array assignments in a hook.

Applies to: hook_action_info(), hook_hook_info(), hook_node_info(), hook_theme().

NOTE In general, there are 3 typical cases (or code styles):

  • return array('key1' => array('key2' => ...);
  • $var = array('key1' => array('key2' => ...); return $var;
  • $var = array(); $var['key1'] = array('key2' => ...); return $var;

The inner array to modify is 3 levels deep in the first 2 cases, but only 2 levels deep in the third. In the first 2 cases, we can loop on the key1 arrays. In the third, the loop is on assignment statements.

File

coder_upgrade/conversions/tool.inc, line 485
Provides tools to assist with conversion routines.

Code

function coder_upgrade_convert_return_OLD(&$node, $hook, $callback = '') {

  // DONE
  cdp("inside " . __FUNCTION__);
  $editor = PGPEditor::getInstance();
  $msg = '// TODO The array elements in this hook function need to be changed.';
  $item =& $node->data;

  // Get the function body.
  $body =& $item->body;
  if (!($return = $body
    ->find(T_RETURN, 'reverse'))) {
    clp("ERROR: return statement not found in hook_{$hook}");
    $body
      ->insertFirst($editor
      ->commentToStatement($msg), 'comment');
    return;
  }
  $value =& $return
    ->getParameter();

  // @todo Strip comments.
  // Examine the type of the operand in the return statement.
  $operand = $value
    ->getElement();
  if (get_class($operand) == 'PGPArray') {

    // Use case 1 - returns array directly.
    // The keys of this array are the node type items.
    $array1 = $value
      ->getElement();
    coder_upgrade_callback_return_case1($array1, $hook, $callback);
  }
  elseif (get_class($operand) == 'PGPOperand') {

    /*
     * Loop on body statements until we find an assignment to the return variable.
     * The assignment could be to an array element like $info['node_type_name'] = array(...).
     * Or directly to the variable like $info = array('node_type_name' => array(...)).
     */
    $return_variable = $operand
      ->toString();
    $count = 0;
    coder_upgrade_convert_return_loop_OLD($body
      ->first(), $count, $return_variable, $hook, $callback);
    if (!$count) {
      clp("ERROR: assignment statement to return variable not found in hook_{$hook}");
      $body
        ->insertFirst($editor
        ->commentToStatement($msg), 'comment');
      return;
    }
  }
  else {
    clp("ERROR: operand of return statement is not an array or variable in hook_{$hook}");
    $body
      ->insertFirst($editor
      ->commentToStatement($msg), 'comment');
  }
}