You are here

function coder_upgrade_convert_op in Coder 7

Same name in this branch
  1. 7 coder_upgrade/conversions/other.inc \coder_upgrade_convert_op()
  2. 7 coder_upgrade/conversions/tool.inc \coder_upgrade_convert_op()
Same name and namespace in other branches
  1. 7.2 coder_upgrade/conversions/other.inc \coder_upgrade_convert_op()
  2. 7.2 coder_upgrade/conversions/tool.inc \coder_upgrade_convert_op()

Initiates the transformation of $op hook to a new hook_$op style function.

Parameters

PGPNode $node: A node object containing a PGPClass (or function) item.

string $callback: A string of the callback function for the hook.

integer $op_index: An integer of the operation parameter in the function parameter list.

6 calls to coder_upgrade_convert_op()
coder_upgrade_callback_functions in coder_upgrade/conversions/other.inc
Callback routine for function changes using grammar parser.
coder_upgrade_upgrade_hook_block_alter in coder_upgrade/conversions/function.inc
Implements hook_upgrade_hook_block_alter().
coder_upgrade_upgrade_hook_comment_alter in coder_upgrade/conversions/function.inc
Implements hook_upgrade_hook_comment_alter().
coder_upgrade_upgrade_hook_nodeapi_alter in coder_upgrade/conversions/function.inc
Implements hook_upgrade_hook_nodeapi_alter().
coder_upgrade_upgrade_hook_node_type_alter in coder_upgrade/conversions/function.inc
Implements hook_upgrade_hook_node_type_alter().

... See full list

File

coder_upgrade/conversions/other.inc, line 1361
Other conversion routine file for the coder_upgrade module.

Code

function coder_upgrade_convert_op(&$node, $callback, $op_index) {
  cdp("inside " . __FUNCTION__);
  cdp("{$callback}");

  /*
   * DBTNG changes can be done in another routine
   */

  // Get the function object.
  $item =& $node->data;

  // Rename the function in case any code is left over.
  $item->name .= '_OLD';

  // Get the operation function parameter, usually called $op.
  $count = $item->parameters
    ->count();

  // TODO This gets the entire parameter including any default value. Hook_block has $op = 'list'.
  $op = $item
    ->printParameter($op_index);

  // Get the function body statements.
  $body =& $item->body;

  /*
   * Two likely cases: switch statement or series of if blocks.
   * Do the if blocks later.
   * Compare the second parameter to the function with the switch operand.
   */

  // Loop on the body statements looking for the $op parameter in an IF or
  // SWITCH condition.
  $current = $body
    ->first();
  while ($current->next != NULL) {
    $statement =& $current->data;
    if (is_object($statement)) {
      cdp($statement
        ->print_r());
    }
    if (is_a($statement, 'PGPConditional')) {

      //      cdp("inside PGPConditional check");
      //      cdp("statement->type = " . $statement->type);
      if ($statement->type == T_SWITCH) {

        //        cdp("inside T_SWITCH check");
        // Get the list of conditions.
        $conditions = $statement->conditions;

        // Get the first condition. (With a switch there should only be one condition.)
        $condition = $conditions
          ->getElement();
        $operand = $condition
          ->toString();

        // If the condition variable matches the $op variable, then go to work.
        if ($op == $operand) {
          $cases = $statement->body;
          $node
            ->traverse($cases, $callback);
        }
      }
      elseif (in_array($statement->type, array(
        T_IF,
        T_ELSEIF,
      ))) {

        //        cdp("inside T_IF check");

        /*
         * Extract the conditions referencing the $op variable and loop on them.
         * These are conditions of the form $op == 'operation'.
         * Replace them with condition of TRUE to not disrupt the logic.
         * Retain any other conditions as part of the body in the new hook
         * function.
         */
        $operations = coder_upgrade_extract_operations($statement->conditions, $op);

        // Loop on the extracted operations.
        foreach ($operations as $operation) {

          // Change a T_ELSEIF to a T_IF in the new hook function.
          $statement->type = T_IF;

          // If it isn't already.
          $block = new stdClass();
          $block->body = new PGPBody();
          $block->body
            ->insertLast($statement);
          $case_node = new PGPNode($block, $current->container);

          // TODO What is the correct container???
          $callback($node, $case_node, $operation);
        }
      }
    }

    // Move to next node.
    $current =& $current->next;

    // Get the statement list the switch statement (or if block) node is part of.
    $container =& $current->container;
    $container
      ->delete($current->previous);
  }
}