You are here

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

Tries to rewrite the original function call.

Parameters

\Pharborist\Functions\FunctionCallNode $call: The original function call.

\Drupal\drupalmoduleupgrader\TargetInterface $target: The target module.

Return value

\Pharborist\Node|null If the original function call is returned (determined by object identity), the function call is not replaced. If a different node is returned, it will replace the original call. And if nothing is returned, the original call is commented out with a FIXME.

Overrides FunctionCallModifier::rewrite

File

src/Plugin/DMU/Converter/Functions/VariableGet.php, line 38

Class

VariableGet
Plugin annotation @Converter( id = "variable_get", description = @Translation("Replaces variable_get() calls with Configuration API.") )

Namespace

Drupal\drupalmoduleupgrader\Plugin\DMU\Converter\Functions

Code

public function rewrite(FunctionCallNode $call, TargetInterface $target) {
  if ($this
    ->tryRewrite($call, $target)) {
    $arguments = $call
      ->getArguments();
    $key = $arguments[0]
      ->toValue();
    if ($arguments[1] instanceof ScalarNode) {

      // @TODO Couldn't convert() derive the schema from $this->defaults?
      // That'd be preferable to having yet another state property ($schema)
      // on this class.
      $this->defaults[$key] = $arguments[1]
        ->toValue();
      $this->schema[$key]['type'] = gettype($this->defaults[$key]);
    }
    else {
      $comment = <<<END
Could not extract the default value because it is either indeterminate, or
not scalar. You'll need to provide a default value in
config/install/@module.settings.yml and config/schema/@module.schema.yml.
END;
      $variables = [
        '@module' => $target
          ->id(),
      ];
      $this
        ->buildFixMe($comment, $variables)
        ->prependTo($call
        ->getStatement());
    }
    return ClassMethodCallNode::create('\\Drupal', 'config')
      ->appendArgument($target
      ->id() . '.settings')
      ->appendMethodCall('get')
      ->appendArgument(clone $arguments[0]);
  }
}