Further modules included.
[yaffs-website] / web / modules / contrib / drupalmoduleupgrader / src / Plugin / DMU / Converter / Functions / FunctionCallModifier.php
1 <?php
2
3 namespace Drupal\drupalmoduleupgrader\Plugin\DMU\Converter\Functions;
4
5 use Drupal\drupalmoduleupgrader\ConverterBase;
6 use Drupal\drupalmoduleupgrader\TargetInterface;
7 use Pharborist\Functions\FunctionCallNode;
8
9 /**
10  * Base class for converters which modify individual function calls.
11  */
12 abstract class FunctionCallModifier extends ConverterBase {
13
14   /**
15    * Tries to rewrite the original function call.
16    *
17    * @param \Pharborist\Functions\FunctionCallNode $call
18    *  The original function call.
19    * @param \Drupal\drupalmoduleupgrader\TargetInterface $target
20    *  The target module.
21    *
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.
27    */
28   abstract public function rewrite(FunctionCallNode $call, TargetInterface $target);
29
30   /**
31    * {@inheritdoc}
32    */
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);
38   }
39
40   /**
41    * {@inheritdoc}
42    */
43   public function convert(TargetInterface $target) {
44     // Prevent stupid effing 'undefined index' notices.
45     $function = @($this->pluginDefinition['function'] ?: $this->getPluginId());
46
47     $function_calls = $target
48       ->getIndexer('function_call')
49       ->get($function);
50
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()) {
58         continue;
59       }
60
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);
67       }
68       elseif ($rewritten !== $function_call) {
69         $function_call->replaceWith($rewritten);
70       }
71
72       $target->save($rewritten);
73     }
74   }
75
76 }