19 declare(strict_types=1);
31 public const DIC =
'DIC';
36 private \Rector\Core\PhpParser\Node\NodeFactory $nodeFactory,
37 private \Rector\Core\NodeManipulator\StmtsManipulator $stmtsManipulator,
38 private \Rector\Core\PhpParser\Comparing\NodeComparator $nodeComparator,
39 private NodesToAddCollector $nodesToAddCollector,
40 private \Rector\Core\PhpParser\Node\BetterNodeFinder $betterNodeFinder,
41 private \Rector\Core\Contract\Console\OutputStyleInterface $outputStyle
47 return new Variable(self::DIC);
56 ClassMethod $classMethod,
60 $class_method_string = $class->name->name .
'::' . $classMethod->name->name;
61 $stmt_string = $this->nodeComparator->printWithoutComments($stmt);
62 if (isset($this->duplicate_checker[$class_method_string][$stmt_string])
63 && $this->duplicate_checker[$class_method_string][$stmt_string] ===
true) {
66 $stmts = $this->stmtsManipulator->filterOutExistingStmts(
75 foreach ($classMethod->getStmts() as $inner_statement) {
76 if ($inner_statement->getAttributes() === []) {
79 $first = $inner_statement;
82 if ($first !== null) {
83 $this->nodesToAddCollector->addNodeBeforeNode($stmt, $first);
85 $classMethod->stmts[] = $stmt;
87 $this->duplicate_checker[$class_method_string][$stmt_string] =
true;
91 \PhpParser\Node\Stmt\Class_ $class
93 if (isset($this->added_constructors[$class->name->name])) {
94 return $this->added_constructors[$class->name->name];
96 $classMethod = $this->nodeFactory->createPublicMethod(
97 \Rector\Core\ValueObject\MethodName::CONSTRUCT
102 \Rector\Core\ValueObject\MethodName::CONSTRUCT
105 \Rector\Core\ValueObject\MethodName::CONSTRUCT
108 $first_class_method = array_filter($class->stmts,
function (\PhpParser\Node $node):
bool {
109 return $node instanceof ClassMethod;
111 $first_class_method = array_shift($first_class_method);
112 if ($first_class_method !== null) {
113 $this->nodesToAddCollector->addNodeBeforeNode($classMethod, $first_class_method);
115 array_unshift($class->stmts, $classMethod);
117 $this->outputStyle->newline();
118 $this->outputStyle->warning(
119 'created constructor for ' . $class->name->name .
'. Please check the parent-call for missing parameters!' 121 $this->outputStyle->newline();
123 $this->added_constructors[$class->name->name] = $classMethod;
129 \PhpParser\Node\Stmt\Class_ $class,
132 $classMethod = $class->getMethod(
133 \Rector\Core\ValueObject\MethodName::CONSTRUCT
135 if (!$classMethod instanceof \PhpParser\Node\Stmt\ClassMethod) {
152 $this->duplicate_checker[$class->name->name][$this->nodeComparator->printWithoutComments($stmt)] =
true;
166 ClassMethod $classMethod,
170 $class_method_string = $class->name->name .
'::' . $classMethod->name->name;
171 $statement_string = $this->nodeComparator->printWithoutComments($stmt);
172 if (isset($this->duplicate_checker[$class_method_string][$statement_string])
173 && $this->duplicate_checker[$class_method_string][$statement_string] ===
true) {
176 $stmts = $this->stmtsManipulator->filterOutExistingStmts(
185 $node = $this->betterNodeFinder->findFirst($classMethod->stmts,
function (\PhpParser\Node $node):
bool {
186 if (!$node instanceof Stmt\Global_) {
189 foreach ($node->vars as $var) {
190 if (!(property_exists($var,
'name') && $var->name !== null)) {
193 if ($var->name !== self::DIC) {
200 $dic_statement_string = $this->nodeComparator->printWithoutComments($this->
getGlobalDIC());
201 if (!$node instanceof \PhpParser\Node
202 && !isset($this->duplicate_checker[$class_method_string][$dic_statement_string])
203 && !$this->duplicate_checker[$class_method_string][$dic_statement_string]
205 throw new ShouldNotHappenException(
206 'no dic found: ' . $class_method_string .
' (' . $statement_string .
') ' 211 $first_existing = array_filter($classMethod->stmts,
function (\PhpParser\Node $node):
bool {
212 if ($node->getAttributes() === []) {
215 return !$node instanceof Stmt\Global_;
217 $first_existing = array_shift($first_existing);
218 if ($first_existing !== null) {
219 $this->nodesToAddCollector->addNodeBeforeNode($stmt, $first_existing);
224 $classMethod->stmts[] = $stmt;
226 $this->duplicate_checker[$class_method_string][$statement_string] =
true;
230 \PhpParser\Node\Stmt\Class_ $class,
233 $classMethod = $class->getMethod(
234 \Rector\Core\ValueObject\MethodName::CONSTRUCT
236 if (!$classMethod instanceof \PhpParser\Node\Stmt\ClassMethod) {
247 \PhpParser\Node\Stmt\Class_ $class,
250 $scope = $class->getAttribute(
251 \Rector\NodeTypeResolver\Node\AttributeKey::SCOPE
253 if (!
$scope instanceof \PHPStan\Analyser\Scope) {
256 $classReflection =
$scope->getClassReflection();
257 if (!$classReflection instanceof \PHPStan\Reflection\ClassReflection) {
260 foreach ($classReflection->getParents() as $parentClassReflection) {
261 if ($parentClassReflection->hasMethod($methodName)) {
270 ): \PhpParser\Node\Stmt\Expression {
271 $staticCall = new \PhpParser\Node\Expr\StaticCall(
272 new \PhpParser\Node\Name(
273 \Rector\Core\Enum\ObjectReference::PARENT()->
getValue()
280 return new \PhpParser\Node\Stmt\Expression($staticCall);
createParentClassMethodCall(string $methodName)
hasClassParentClassMethod(\PhpParser\Node\Stmt\Class_ $class, string $methodName)
__construct(private \Rector\Core\PhpParser\Node\NodeFactory $nodeFactory, private \Rector\Core\NodeManipulator\StmtsManipulator $stmtsManipulator, private \Rector\Core\PhpParser\Comparing\NodeComparator $nodeComparator, private NodesToAddCollector $nodesToAddCollector, private \Rector\Core\PhpParser\Node\BetterNodeFinder $betterNodeFinder, private \Rector\Core\Contract\Console\OutputStyleInterface $outputStyle)
array $added_constructors
ensureGlobalDICinConstructor(Stmt\Class_ $class)
addStmtToMethodIfNotThereAfterGlobalDIC(ClassMethod $classMethod, Stmt\Class_ $class, Stmt $stmt)
if(!file_exists(getcwd() . '/ilias.ini.php'))
addStmtToConstructorIfNotThereAfterGlobalDIC(\PhpParser\Node\Stmt\Class_ $class, Stmt $stmt)
createConstructor(\PhpParser\Node\Stmt\Class_ $class)
addStmtToConstructorIfNotThereYetAtFirstPosition(\PhpParser\Node\Stmt\Class_ $class, Stmt $stmt)
ensureGlobalDICinMethod(ClassMethod $classMethod, Stmt\Class_ $class)
addStmtToMethodIfNotThereYetAtFirstPosition(ClassMethod $classMethod, Stmt\Class_ $class, Stmt $stmt)