You are here

function coder_upgrade_convert_op in Coder 7.2

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

Initiates the transformation of a hook($op) 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/tool.inc, line 24
Provides tools to assist with conversion routines.

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 variable from the function parameter at index $op_index.
  // This function removes any default value assignment (e.g. $op = 'list') or
  // inline comments included in the parameter expression.
  if (!($variable = $item
    ->getParameterVariable($op_index))) {
    clp("ERROR: Variable not found in hook(\$op) parameter {$op_index}");
    return;
  }
  $op = $variable
    ->toString();

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

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

  // Loop on the body statements looking for the $op variable in an IF or
  // SWITCH condition.
  $current = $body
    ->first();
  while ($current->next != NULL) {
    $found = FALSE;
    $statement =& $current->data;
    if ($statement instanceof 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()
          ->findNode('operand')
          ->stripComments();
        $operand = $condition
          ->toString();

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

          // 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);
        }
      }
    }
    elseif (is_array($statement) && $statement['type'] == T_WHITESPACE) {

      // Remove whitespace.
      $found = TRUE;
    }

    // Move to next node.
    $current =& $current->next;
    if ($found) {

      // Get the statement list the switch statement (or if block) node is part of.
      $container =& $current->container;
      $container
        ->delete($current->previous);
    }
  }
  if ($body
    ->count()) {
    $editor = PGPEditor::getInstance();

    // TODO Insert comment indicating the block was not changed.
    $body
      ->insertFirst($editor
      ->commentToStatement('// TODO Remaining code in this function needs to be moved to the appropriate new hook function.'));
  }
}