You are here

public function Generic::rewrite in Drupal 7 to 8/9 Module Upgrader 8

Parametrically rewrites the function containing the given parameter.

Parameters

\Pharborist\Functions\ParameterNode $parameter: The parameter upon which to base the rewrite. The parameter must be attached to a function or method declaration node, or fatal errors will likely result.

Overrides RewriterInterface::rewrite

1 call to Generic::rewrite()
FormState::rewrite in src/Plugin/DMU/Rewriter/FormState.php
Parametrically rewrites the function containing the given parameter.
1 method overrides Generic::rewrite()
FormState::rewrite in src/Plugin/DMU/Rewriter/FormState.php
Parametrically rewrites the function containing the given parameter.

File

src/Plugin/DMU/Rewriter/Generic.php, line 49

Class

Generic
Plugin annotation @Rewriter( id = "_rewriter", deriver = "\Drupal\drupalmoduleupgrader\Plugin\DMU\Rewriter\GenericDeriver" )

Namespace

Drupal\drupalmoduleupgrader\Plugin\DMU\Rewriter

Code

public function rewrite(ParameterNode $parameter) {

  // Don't even try to rewrite the function if the parameter is reassigned.
  if ($this
    ->isReassigned($parameter)) {
    $error = $this
      ->t('@function() cannot be parametrically rewritten because @parameter is reassigned.', [
      '@parameter' => $parameter
        ->getName(),
      '@function' => $parameter
        ->getFunction()
        ->getName()
        ->getText(),
    ]);
    throw new \LogicException($error);
  }
  foreach ($this
    ->getExpressions($parameter)
    ->not($this->isAssigned) as $expr) {
    $property = $this
      ->getProperty($expr);
    if (empty($property)) {
      continue;
    }
    $getter = $this
      ->rewriteAsGetter($expr, $property);
    if ($getter) {
      $empty = $expr
        ->closest(Filter::isFunctionCall('empty', 'isset'));

      // If the original expression was wrapped by a call to isset() or
      // empty(), we need to replace it entirely.
      if ($getter instanceof CallNode && $empty instanceof CallNode) {

        // If the isset() or empty() call was negated, reverse that logic.
        $parent = $empty
          ->parent();
        if ($parent instanceof BooleanNotNode) {
          $parent
            ->replaceWith($getter);
        }
        else {
          $empty
            ->replaceWith(BooleanNotNode::fromExpression($getter));
        }
      }
      else {
        $expr
          ->replaceWith($getter);
      }
    }
  }
  foreach ($this
    ->getExpressions($parameter)
    ->filter($this->isAssigned) as $expr) {

    // If the property cannot be determined, don't even try to rewrite the
    // expression.
    $property = $this
      ->getProperty($expr);
    if (empty($property)) {
      continue;
    }
    $assignment = $expr
      ->closest(Filter::isInstanceOf('\\Pharborist\\Operators\\AssignNode'));
    $setter = $this
      ->rewriteAsSetter($expr, $property, $assignment);
    if ($setter) {
      $assignment
        ->replaceWith($setter);
    }
  }

  // Set the type hint, if one is defined.
  if (isset($this->pluginDefinition['type_hint'])) {
    $parameter
      ->setTypeHint($this->pluginDefinition['type_hint']);

    // If the type hint extends FieldableEntityInterface, rewrite any field
    // lookups (e.g. $node->body[LANGUAGE_NONE][0]['value']).
    if (in_array('Drupal\\Core\\Entity\\FieldableEntityInterface', class_implements($this->pluginDefinition['type_hint']))) {
      $filter = new FieldValueFilter($parameter
        ->getName());
      foreach ($parameter
        ->getFunction()
        ->find($filter) as $lookup) {
        $lookup
          ->replaceWith(self::rewriteFieldLookup($lookup));
      }
    }
  }
}