You are here

function coder_upgrade_reconstruct_array in Coder 7

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

Returns array with another level whose key is from assignment variable.

Parameters

PGPExpression $variable: The assignment variable (left of assignment operator).

PGPExpression $value: The assignment value (right of assignment operator).

Return value

PGPExpression The new array expression.

1 call to coder_upgrade_reconstruct_array()
coder_upgrade_convert_return in coder_upgrade/conversions/tool.inc
Initiates the transformation of array assignments in a hook.

File

coder_upgrade/conversions/tool.inc, line 431
Provides tools to assist with conversion routines.

Code

function coder_upgrade_reconstruct_array($variable, $value) {
  cdp("inside " . __FUNCTION__);
  if ($variable
    ->countType('index') < 2) {
    return new PGPExpression();
  }
  $editor = PGPEditor::getInstance();

  // This routine assumes the second index is the one to be reconstructed.
  $key = trim($variable
    ->getType('index', 2)
    ->toString(), "'\"");
  $array = $editor
    ->expressionToStatement("array('{$key}' => {$value->toString()})");

  //->getElement();
  cdp($array
    ->toString(), '$array');
  return $array;

  /*
    // The above works well for existing use cases, but fails for hook_perm possibly
    // because the array traverse2() routine will skip the key when $depth != $start_depth.
    $count = $variable->countType('index');
    if (!$count) {
      return new PGPExpression();
    }

    $editor = PGPEditor::getInstance();

    // Allow for a key being a string or a variable expression.
    $key = $variable->getType('index', $count)->toString(); // $key = trim($variable->getType('index', $count)->toString(), "'\"");
    if ($key) {
      $array = $editor->expressionToStatement("array($key => {$value->toString()})"); //->getElement();
    }
    else {
      $array = $editor->expressionToStatement("array({$value->toString()})"); //->getElement();
    }
    cdp($array->toString(), '$array');
    return $array;
  */
}