3 namespace Drupal\drupalmoduleupgrader\Plugin\DMU\Converter\Functions;
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;
14 * description = @Translation("Converts calls to watchdog() to \Drupal::logger().")
17 class Watchdog extends FunctionCallModifier {
19 protected static $severityConstants = [
33 public function rewrite(FunctionCallNode $call, TargetInterface $target) {
34 $arguments = $call->getArguments();
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.
42 // @TODO Leave a FIXME for an invalid severity, since changing it to a
43 // notice alters the intent of the original code.
45 if (sizeof($arguments) > 3 && $arguments[3] instanceof ConstantNode && in_array($arguments[3]->getConstantName()->getText(), static::$severityConstants)) {
46 $method = strtolower(subStr($arguments[3], 9));
52 // If there is a third argument, and it's an array, a context array
54 $context = (sizeof($arguments) > 2 && $arguments[2] instanceof ArrayNode) ? clone $arguments[2] : ArrayNode::create([]);
56 return ClassMethodCallNode::create('\Drupal', 'logger')
57 ->appendArgument(clone $arguments[0])
58 ->appendMethodCall($method)
59 ->appendArgument(clone $arguments[1])
60 ->appendArgument($context);