You are here

public function FormState::rewriteAsGetter in Drupal 7 to 8/9 Module Upgrader 8

Rewrites the given expression as a property getter. Returns NULL if the expression cannot be rewritten.

Parameters

\Pharborist\ExpressionNode $expr: The expression to rewrite.

string $property: The property being used in the expression.

Return value

\Pharborist\ExpressionNode|null

Overrides Generic::rewriteAsGetter

File

src/Plugin/DMU/Rewriter/FormState.php, line 164

Class

FormState
Plugin annotation @Rewriter( id = "form_state", type_hint = "\Drupal\Core\Form\FormStateInterface", properties = { "always_process" = { "get" = "getAlwaysProcess", "set" = "setAlwaysProcess" }, "build_info" = { "get" =…

Namespace

Drupal\drupalmoduleupgrader\Plugin\DMU\Rewriter

Code

public function rewriteAsGetter(ExpressionNode $expr, $property) {

  /** @var \Pharborist\ArrayLookupNode $expr */
  $object = clone $expr
    ->getRootArray();
  $keys = $expr
    ->getKeys();

  // $foo = $form_state['values'] --> $foo = $form_state->getValues()
  // $foo = $form_state['values']['baz'] --> $form_state->getValue(['baz'])
  if ($property == 'values') {
    if (sizeof($keys) == 1) {
      return ObjectMethodCallNode::create($object, 'getValues');
    }
    else {
      array_shift($keys);
      return ObjectMethodCallNode::create($object, 'getValue')
        ->appendArgument(ArrayNode::create($keys));
    }
  }
  elseif (isset($this->pluginDefinition['properties'][$property]['get'])) {
    return parent::rewriteAsGetter($expr, $property);
  }
  else {
    return ObjectMethodCallNode::create($object, 'get')
      ->appendArgument(ArrayNode::create($keys));
  }
}