You are here

function coder_upgrade_convert_menu_valid_path in Coder 7.2

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

Updates menu_valid_path().

Copied from hook_node_info() change.

NOTE In general, there are 3 typical cases (or code styles): Case 1: passes variable directly. if (!menu_valid_path(array('link_path' => $expression))) { ... }

Case 2: makes one assignment to array variable; passes variable. $item = array('link_path' => $expression); if (!menu_valid_path($item)) { ... }

Case 3: makes multiple assignments to array variable; passes variable. $item['link_path'] = $expression1; $item['link_title'] = $expression2; if (!menu_valid_path($item)) { ... }

[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.]

1 call to coder_upgrade_convert_menu_valid_path()
coder_upgrade_upgrade_call_menu_valid_path_alter in coder_upgrade/conversions/call.inc
Implements hook_upgrade_call_menu_valid_path_alter().

File

coder_upgrade/conversions/call.inc, line 2878
Provides conversion routines applied to function calls.

Code

function coder_upgrade_convert_menu_valid_path(&$item, &$reader, &$editor) {

  // DONE
  cdp("inside " . __FUNCTION__);
  $operand = $item
    ->getParameter()
    ->getElement();
  cdp($operand
    ->print_r());

  // Examine the type of the operand in the parameter.
  if (get_class($operand) == 'PGPArray') {

    // Use case 1 - passes variable directly.
    cdp("Case 1: passes variable directly");
    coder_upgrade_callback_menu_valid_path($item, $operand);
  }
  elseif (get_class($operand) == 'PGPOperand') {
    $p0 = $operand
      ->toString();
    if ($operand
      ->count() > 2) {

      // Use case 4 - passes more complex variable directly. (NOT HANDLED)
      // The operand passed is more complex than a variable name. It might
      // be an expression like $item['xx'] or $item->xx.
      clp("ERROR: Variable expression {$p0} passed as parameter in menu_valid_path");
      $editor
        ->setParameter($item, 0, "{$p0} /* TODO Please pass a menu path directly */");
      return;
    }
    if ($operand
      ->getElement(0) != T_VARIABLE) {
      clp("ERROR: Parameter {$p0} is not a variable in menu_valid_path");
      return;
    }
    $variable = $operand
      ->getElement(1);
    cdp("variable = {$variable}");

    // Get the statement (i.e. node) this function call is part of.
    $parent =& $item->parent;

    // Get the statement list the parent is part of.
    $container =& $parent->container;

    // Search the container for assignment to the variable.

    /*
     * Loop on body statements until we find an assignment to the parameter
     * variable. Loop in reverse from the parent statement to find the last
     * setting of the variable.
     */
    $parameter_variable = $operand
      ->toString();
    $count = 0;
    $current = $parent->previous;
    while ($current->previous != NULL) {
      if (is_object($current->data) && $current->data->type == T_ASSIGNMENT) {
        $assignment = $current->data;
        $assign_variable = $assignment->values
          ->getElement()
          ->getElement()
          ->toString();
        if ($parameter_variable == $assign_variable) {

          // Use case 2: makes one assignment to array variable; pass variable.
          cdp("Case 2: Assignment variable matches parameter variable");
          $value1 = $assignment->values
            ->getElement();
          $array1 = $value1
            ->getElement($value1
            ->count() - 1);
          coder_upgrade_callback_menu_valid_path($item, $array1);

          //          $count++;
          return;
        }
        elseif (strpos($assign_variable, $parameter_variable) !== FALSE && strpos($assign_variable, 'link_path') !== FALSE) {

          // Use case 3: makes multiple assignments to array variable; pass variable.
          cdp("Case 3: Assignment variable includes parameter variable");
          $value1 = $assignment->values
            ->getElement();
          $value2 = $value1
            ->getElement($value1
            ->count() - 1);

          // Make an expression object to use as function call parameter.
          $expression = new PGPExpression();
          $expression
            ->insertLast($value2, 'operand');
          $item
            ->setParameter(0, $value2);
          cdp($item
            ->toString());

          //          $count++;
          return;
        }
      }
      $current = $current->previous;
    }
    if (!$count) {
      clp("ERROR: Assignment statement to parameter variable not found in menu_valid_path");
      return;
    }
  }
}