You are here

protected function ConverterBase::rewriteFunction in Drupal 7 to 8/9 Module Upgrader 8

Parametrically rewrites a function.

Parameters

\Drupal\drupalmoduleupgrader\RewriterInterface $rewriter: A fully configured parametric rewriter.

\Pharborist\Functions\ParameterNode $parameter: The parameter upon which to base the rewrite.

TargetInterface $target: The target module.

bool $recursive: If TRUE, rewriting will recurse into called functions which are passed the rewritten parameter as an argument.

4 calls to ConverterBase::rewriteFunction()
EntityHooks::convert in src/Plugin/DMU/Converter/EntityHooks.php
Performs required conversions.
HookEntityTypeView::convert in src/Plugin/DMU/Converter/HookEntityTypeView.php
Performs required conversions.
HookUserLogin::convert in src/Plugin/DMU/Converter/HookUserLogin.php
Performs required conversions.
UserHooks::convert in src/Plugin/DMU/Converter/UserHooks.php
Performs required conversions.

File

src/ConverterBase.php, line 258

Class

ConverterBase
Base class for converters.

Namespace

Drupal\drupalmoduleupgrader

Code

protected function rewriteFunction(RewriterInterface $rewriter, ParameterNode $parameter, TargetInterface $target, $recursive = TRUE) {
  $rewriter
    ->rewrite($parameter);
  $target
    ->save($parameter);

  // Find function calls within the rewritten function which are called
  // with the rewritten parameter.
  $indexer = $target
    ->getIndexer('function');
  $next = $parameter
    ->getFunction()
    ->find(new FunctionCallArgumentFilter($parameter
    ->getName()))
    ->filter(function (FunctionCallNode $call) use ($indexer) {
    return $indexer
      ->has($call
      ->getName()
      ->getText());
  });

  /** @var \Pharborist\Functions\FunctionCallNode $call */
  foreach ($next as $call) {

    /** @var \Pharborist\Functions\FunctionDeclarationNode $function */
    $function = $indexer
      ->get($call
      ->getName()
      ->getText());
    foreach ($call
      ->getArguments() as $index => $argument) {
      if ($argument instanceof VariableNode && $argument
        ->getName() == $parameter
        ->getName()) {
        $this
          ->rewriteFunction($rewriter, $function
          ->getParameterAtIndex($index), $target, $recursive);
        break;
      }
    }
  }
}