You are here

function coder_upgrade_upgrade_call_user_load_alter in Coder 7

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

Implements hook_upgrade_call_user_load_alter().

File

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

Code

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

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

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

    // No parameters is an acceptable default in D7, but should cause an
    // uncaught error in D6. In D7, this will cause all users to be loaded.
    $item->name['value'] = 'user_load_multiple';
    return;
  }

  /*
   * Steps
   * - parameter may be a value, array, or variable (with a preceding assignment)
   * - value:
   *   - leave as is
   * - array
   *   - extract 'uid' element
   *   - leave remaining items as $conditions in new function
   *   - change function to user_load_multiple
   *   - wrap in array_shift to return first item
   * - variable:
   *   - if variable is an array item, eg. $var['uid']
   *     - then handle like value above (NOT DONE -- could also be an array)
   *   - if variable has an assignment statement in scope
   *     - then handle like array above
   *       (attempt to extract uid from other conditions)
   */
  $p0 = FALSE;
  if ($item
    ->getParameter()
    ->isType(T_VARIABLE)) {

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

    // TODO This single search won't find multiple assignments to array variable.
    $statement = $parent->container
      ->searchBackward('PGPAssignment', 'values', 0, $variable, $parent);
    if ($statement) {
      $operand =& $statement->values
        ->getElement()
        ->findNode('operand', 'backward');
      $p0 = coder_upgrade_convert_user_load($item, $operand, FALSE);
      if (is_object($operand) && $operand
        ->isType(T_ARRAY) && !$operand->count) {

        // Assignment variable has no more value in its array.
        // So delete this parameter from the function call.
        $item
          ->deleteParameter();
      }
    }
    else {

      // Assume the variable is the uid?
      $text = $item
        ->getParameter()
        ->stripComments()
        ->toString();
      $item
        ->insertStatementBefore($editor
        ->commentToStatement('// TODO Convert "user_load" to "user_load_multiple" if "' . $text . '" is other than a uid.'));
      $item
        ->insertStatementBefore($editor
        ->commentToStatement('// To return a single user object, wrap "user_load_multiple" with "array_shift" or equivalent.'));
      $item
        ->insertStatementBefore($editor
        ->commentToStatement('// Example: array_shift(user_load_multiple(array(), ' . $text . '))'));
    }
  }
  else {
    $operand =& $item
      ->getParameter()
      ->findNode('operand');
    $p0 = coder_upgrade_convert_user_load($item, $operand);
    if ($item
      ->getParameter()
      ->isType(T_ARRAY)) {

      // Parameter is an array.
      $array = $item
        ->getParameter()
        ->getElement();
      if (!$array->count) {
        $item
          ->deleteParameter();
      }
    }
  }
  if ($p0) {
    $item
      ->insertParameter(0, $p0);
  }
  if ($item->name['value'] == 'user_load_multiple') {

    // Wrap current call with array_shift to return first object from array.
    $call = $item
      ->toString();
    $item = $editor
      ->expressionToStatement("array_shift({$call})");
  }
}