You are here

function coder_upgrade_upgrade_call_drupal_goto_alter in Coder 7

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

Implements hook_upgrade_call_drupal_goto_alter().

File

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

Code

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

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

  // Get the function call object.
  $item =& $node->data;
  if ($item
    ->parameterCount() < 2) {

    // Nothing to do.
    return;
  }

  // Convert query and fragment parameters into an array.
  // Save the http_response_code parameter.
  $http_code = $item
    ->getParameter(3);
  $item
    ->deleteParameter(3);
  if ($item
    ->parameterCount() > 1) {

    // Convert query string into an array
    $query = $item
      ->getParameter(1);
    $operand = $query
      ->getElement();
    $value = $query
      ->toString();
    if ($query
      ->isType(T_CONSTANT_ENCAPSED_STRING)) {

      // Parameter is a string.
      $query = coder_upgrade_query_to_array($item
        ->printParameter(1));
      $item
        ->setParameter(1, $query);
    }
    elseif ($query
      ->isType(T_VARIABLE)) {

      // Parameter is a variable.
      $variable = $operand
        ->findNode('value');

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

      // $parent = &$item->parent;
      // Get the statement list the parent is part of.
      $statement = $parent->container
        ->searchBackward('PGPAssignment', 'values', 0, $variable, $parent);
      if ($statement) {
        $operand2 =& $statement->values
          ->getElement()
          ->findNode('operand', 'backward');

        // TODO A pattern here - this is the same code as above but executed on a different object.
        if (is_array($operand2) && $operand2['type'] == T_CONSTANT_ENCAPSED_STRING) {
          $operand2 = coder_upgrade_query_to_array($operand2['value']);
        }
        else {
          clp("ERROR: Could not find a string to change in " . __FUNCTION__);
          $item
            ->insertStatementBefore($editor
            ->commentToStatement('// TODO ' . $item
            ->printParameter(1) . ' needs to be an array of keys and values instead of a string.'));
        }
      }
      else {
        clp("ERROR: Could not find a string to change in " . __FUNCTION__);
        $item
          ->insertStatementBefore($editor
          ->commentToStatement('// TODO ' . $item
          ->printParameter(1) . ' needs to be an array of keys and values instead of a string.'));
      }
    }
    elseif ($query
      ->isType(T_STRING)) {

      // Parameter is a constant.
      if ($value != 'NULL') {
        $item
          ->insertStatementBefore($editor
          ->commentToStatement('// TODO ' . $item
          ->printParameter(1) . ' needs to be an array of keys and values instead of a string.'));
      }

      // TODO Handle constants - add todo comment - try to find the define?
    }
    else {

      //      clp("ERROR: Could not find a string to change in " . __FUNCTION__);
      $item
        ->insertStatementBefore($editor
        ->commentToStatement('// TODO ' . $item
        ->printParameter(1) . ' needs to be an array of keys and values instead of a string.'));
    }
  }

  // Arrayitize the parameters.
  $keys = array(
    'query',
    'fragment',
  );
  $defaults = array(
    'NULL',
    'NULL',
  );
  $string = $editor
    ->arrayitize($item, 1, $keys, $defaults);
  if (is_object($http_code) || $string != 'array()') {
    $temp = $editor
      ->expressionToStatement($string);
    $temp
      ->getElement(0)->multiline = 0;
    $item
      ->setParameter(1, $temp);
  }
  if (is_object($http_code)) {
    $item
      ->setParameter(2, $http_code);
  }
}