ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
ReplaceUtilSendMessageRector.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\CI\Rector\ilUtils;
22 
23 use PhpParser\Node;
28 
29 final class ReplaceUtilSendMessageRector extends \Rector\Core\Rector\AbstractRector
30 {
31  protected array $old_method_names = [
32  'sendInfo',
33  'sendSuccess',
34  'sendFailure',
35  'sendQuestion',
36  ];
37  protected string $new_method_name = 'setOnScreenMessage';
38 
39  public function __construct(
40  protected \ILIAS\CI\Rector\DIC\DICMemberResolver $dicMemberResolver,
41  protected \Rector\Transform\NodeTypeAnalyzer\TypeProvidingExprFromClassResolver $typeProvidingExprFromClassResolver,
42  protected \Rector\Core\NodeManipulator\ClassDependencyManipulator $classDependencyManipulator,
43  protected \Rector\PostRector\Collector\PropertyToAddCollector $propertyToAddCollector,
44  protected \Rector\Core\NodeManipulator\ClassInsertManipulator $classInsertManipulator
45  ) {
46  }
47 
51  public function getNodeTypes(): array
52  {
53  return [\PhpParser\Node\Expr\StaticCall::class];
54  }
55 
56  private function isApplicable(Node $node): bool
57  {
59  if (!$node->class instanceof \PhpParser\Node\Name) {
60  return false;
61  }
62  $staticCallClassName = $node->class->toString();
63  if ($staticCallClassName !== \ilUtil::class) {
64  // not calling ilUtil
65  return false;
66  }
67  if (!$node->name instanceof \PhpParser\Node\Identifier) {
68  // node has no name
69  return false;
70  }
71  // not interested in method since not in list
72  return in_array($node->name->name, $this->old_method_names);
73  }
74 
78  public function refactor(Node $node): ?\PhpParser\Node\Expr\MethodCall
79  {
80  if (!$this->isApplicable($node)) {
81  return null; // leave the node as it is
82  }
83  $class_where_call_happens = $this->betterNodeFinder->findParentType(
84  $node,
85  \PhpParser\Node\Stmt\Class_::class
86  );
87  if (!$class_where_call_happens instanceof \PhpParser\Node\Stmt\Class_) {
88  // not on class, abort
89  return null; // leave the node as it is
90  }
91  $method_where_call_happend = $this->betterNodeFinder->findParentType(
92  $node,
93  \PhpParser\Node\Stmt\ClassMethod::class
94  );
95  if (!$method_where_call_happend instanceof \PhpParser\Node\Stmt\ClassMethod) {
96  // not in a method, abort
97  return null; // leave the node as it is
98  }
99 
100  if ($method_where_call_happend->isStatic()) {
101 // return null;
102  }
103 
104  // prepend a new argument with the type of the message, aka sendInfo goes to setOnScreenMessage('info', ...
105  $message_type = strtolower(str_replace('send', '', $node->name->name));
106  $arg = $this->nodeFactory->createArg($message_type);
107  $arguments = $node->args;
108  array_unshift($arguments, $arg);
109 
110  // ensure a dic property for ilGlobalTemplate is in the class. or we get another Expr to fetch ilGlobalTemplate
111  try {
112  $dicPropertyFetch = $this->dicMemberResolver->ensureDICDependency(
114  $class_where_call_happens,
115  $method_where_call_happend
116  );
117  } catch (ShouldNotHappenException $e) {
118  throw new ShouldNotHappenException(
119  "Could not process " . $this->file->getFilePath() . ': ' . $e->getMessage()
120  );
121  }
122 
123  // return new method call
124  $methodCall = new \PhpParser\Node\Expr\MethodCall(
125  $dicPropertyFetch,
126  $this->new_method_name,
127  $arguments
128  );
129  return $methodCall;
130  }
131 
132  public function getRuleDefinition(): \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
133  {
134  return new RuleDefinition('lorem', [
135  new CodeSample(
136  "\ilUtil::sendQuestion('my_text', true);",
137  "\$this->main_tpl->setOnScreenMessage('question', 'my_text', true)"
138  )
139  ]);
140  }
141 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ChatMainBarProvider .
__construct(protected \ILIAS\CI\Rector\DIC\DICMemberResolver $dicMemberResolver, protected \Rector\Transform\NodeTypeAnalyzer\TypeProvidingExprFromClassResolver $typeProvidingExprFromClassResolver, protected \Rector\Core\NodeManipulator\ClassDependencyManipulator $classDependencyManipulator, protected \Rector\PostRector\Collector\PropertyToAddCollector $propertyToAddCollector, protected \Rector\Core\NodeManipulator\ClassInsertManipulator $classInsertManipulator)