Further modules included.
[yaffs-website] / web / modules / contrib / drupalmoduleupgrader / src / Plugin / DMU / Converter / Functions / Watchdog.php
1 <?php
2
3 namespace Drupal\drupalmoduleupgrader\Plugin\DMU\Converter\Functions;
4
5 use Drupal\drupalmoduleupgrader\TargetInterface;
6 use Pharborist\Constants\ConstantNode;
7 use Pharborist\Functions\FunctionCallNode;
8 use Pharborist\Objects\ClassMethodCallNode;
9 use Pharborist\Types\ArrayNode;
10
11 /**
12  * @Converter(
13  *  id = "watchdog",
14  *  description = @Translation("Converts calls to watchdog() to \Drupal::logger().")
15  * )
16  */
17 class Watchdog extends FunctionCallModifier {
18
19   protected static $severityConstants = [
20     'WATCHDOG_EMERGENCY',
21     'WATCHDOG_ALERT',
22     'WATCHDOG_CRITICAL',
23     'WATCHDOG_ERROR',
24     'WATCHDOG_WARNING',
25     'WATCHDOG_NOTICE',
26     'WATCHDOG_INFO',
27     'WATCHDOG_DEBUG',
28   ];
29
30   /**
31    * {@inheritdoc}
32    */
33   public function rewrite(FunctionCallNode $call, TargetInterface $target) {
34     $arguments = $call->getArguments();
35
36     // We'll call a specific method on the logger object, depending on the
37     // severity passed in the original function call (if any). If there are
38     // at least four arguments, a severity was passed. We check $arguments[3]
39     // to ensure it's a valid severity constant, and if it's not, we default
40     // to the notice() severity.
41     //
42     // @TODO Leave a FIXME for an invalid severity, since changing it to a
43     // notice alters the intent of the original code.
44     //
45     if (sizeof($arguments) > 3 && $arguments[3] instanceof ConstantNode && in_array($arguments[3]->getConstantName()->getText(), static::$severityConstants)) {
46       $method = strtolower(subStr($arguments[3], 9));
47     }
48     else {
49       $method = 'notice';
50     }
51
52     // If there is a third argument, and it's an array, a context array
53     // was passed.
54     $context = (sizeof($arguments) > 2 && $arguments[2] instanceof ArrayNode) ? clone $arguments[2] : ArrayNode::create([]);
55
56     return ClassMethodCallNode::create('\Drupal', 'logger')
57       ->appendArgument(clone $arguments[0])
58       ->appendMethodCall($method)
59       ->appendArgument(clone $arguments[1])
60       ->appendArgument($context);
61   }
62
63 }