3 namespace Drupal\drupalmoduleupgrader\Plugin\DMU\Converter\Functions;
5 use Drupal\drupalmoduleupgrader\ConverterBase;
6 use Drupal\drupalmoduleupgrader\TargetInterface;
7 use Pharborist\Functions\FunctionCallNode;
10 * Base class for converters which modify individual function calls.
12 abstract class FunctionCallModifier extends ConverterBase {
15 * Tries to rewrite the original function call.
17 * @param \Pharborist\Functions\FunctionCallNode $call
18 * The original function call.
19 * @param \Drupal\drupalmoduleupgrader\TargetInterface $target
22 * @return \Pharborist\Node|NULL
23 * If the original function call is returned (determined by object identity),
24 * the function call is not replaced. If a different node is returned, it
25 * will replace the original call. And if nothing is returned, the original
26 * call is commented out with a FIXME.
28 abstract public function rewrite(FunctionCallNode $call, TargetInterface $target);
33 public function isExecutable(TargetInterface $target) {
34 // Silence 'undefined index' notices if the 'function' key doesn't exist in
35 // the plugin definition.
36 $function = @($this->pluginDefinition['function'] ?: $this->getPluginId());
37 return $target->getIndexer('function_call')->has($function);
43 public function convert(TargetInterface $target) {
44 // Prevent stupid effing 'undefined index' notices.
45 $function = @($this->pluginDefinition['function'] ?: $this->getPluginId());
47 $function_calls = $target
48 ->getIndexer('function_call')
51 foreach ($function_calls as $function_call) {
52 // If the function call is no longer attached to a tree, don't even
53 // try to rewrite it. This could happen when there are two calls to
54 // the same function in a single statement, and the first one has
55 // been commented out -- the second one will be attached to an orphaned
56 // sub-tree, and this will result in fatal errors.
57 if (! $function_call->hasRoot()) {
61 $rewritten = $this->rewrite($function_call, $target);
62 if (empty($rewritten)) {
63 $statement = $function_call->getStatement();
64 $rewritten = $statement->toComment();
65 $statement->replaceWith($rewritten);
66 $this->buildFixMe()->insertBefore($rewritten);
68 elseif ($rewritten !== $function_call) {
69 $function_call->replaceWith($rewritten);
72 $target->save($rewritten);