Skip to content

Commit ee75027

Browse files
committed
Updated Rector to commit 2c6790841ec149b1849c5b931b7b46648fe2f18e
rectorphp/rector-src@2c67908 Fix ClassDependencyManipulator to add dependency on right position (#6413)
1 parent 13fa633 commit ee75027

File tree

3 files changed

+39
-47
lines changed

3 files changed

+39
-47
lines changed

src/Application/VersionResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ final class VersionResolver
1919
* @api
2020
* @var string
2121
*/
22-
public const PACKAGE_VERSION = '1b1807b0f8426641e13e5914389e1c118b3a3d14';
22+
public const PACKAGE_VERSION = '2c6790841ec149b1849c5b931b7b46648fe2f18e';
2323
/**
2424
* @api
2525
* @var string
2626
*/
27-
public const RELEASE_DATE = '2024-11-08 17:26:04';
27+
public const RELEASE_DATE = '2024-11-08 14:55:18';
2828
/**
2929
* @var int
3030
*/

src/NodeManipulator/ClassDependencyManipulator.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
use Rector\TypeDeclaration\NodeAnalyzer\AutowiredClassMethodOrPropertyAnalyzer;
2424
use Rector\ValueObject\MethodName;
2525
use Rector\ValueObject\PhpVersionFeature;
26+
/**
27+
* @see \Rector\Tests\NodeManipulator\ClassDependencyManipulatorTest
28+
*/
2629
final class ClassDependencyManipulator
2730
{
2831
/**

src/NodeManipulator/ClassInsertManipulator.php

Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
declare (strict_types=1);
44
namespace Rector\NodeManipulator;
55

6-
use PhpParser\Node\Stmt;
76
use PhpParser\Node\Stmt\Class_;
87
use PhpParser\Node\Stmt\ClassConst;
98
use PhpParser\Node\Stmt\ClassMethod;
@@ -24,19 +23,47 @@ public function __construct(NodeFactory $nodeFactory)
2423
}
2524
/**
2625
* @api
27-
* @param \PhpParser\Node\Stmt\Property|\PhpParser\Node\Stmt\ClassConst|\PhpParser\Node\Stmt\ClassMethod $stmt
26+
* @param \PhpParser\Node\Stmt\Property|\PhpParser\Node\Stmt\ClassConst|\PhpParser\Node\Stmt\ClassMethod $addedStmt
2827
*/
29-
public function addAsFirstMethod(Class_ $class, $stmt) : void
28+
public function addAsFirstMethod(Class_ $class, $addedStmt) : void
3029
{
3130
$scope = $class->getAttribute(AttributeKey::SCOPE);
32-
$stmt->setAttribute(AttributeKey::SCOPE, $scope);
33-
if ($this->isSuccessToInsertBeforeFirstMethod($class, $stmt)) {
31+
$addedStmt->setAttribute(AttributeKey::SCOPE, $scope);
32+
// no stmts? add this one
33+
if ($class->stmts === []) {
34+
$class->stmts[] = $addedStmt;
3435
return;
3536
}
36-
if ($this->isSuccessToInsertAfterLastProperty($class, $stmt)) {
37+
$newClassStmts = [];
38+
$isAdded = \false;
39+
foreach ($class->stmts as $key => $classStmt) {
40+
$nextStmt = $class->stmts[$key + 1] ?? null;
41+
if ($isAdded === \false) {
42+
// first class method
43+
if ($classStmt instanceof ClassMethod) {
44+
$newClassStmts[] = $addedStmt;
45+
$newClassStmts[] = $classStmt;
46+
$isAdded = \true;
47+
continue;
48+
}
49+
// after last property
50+
if ($classStmt instanceof Property && !$nextStmt instanceof Property) {
51+
$newClassStmts[] = $classStmt;
52+
$newClassStmts[] = $addedStmt;
53+
$isAdded = \true;
54+
continue;
55+
}
56+
}
57+
$newClassStmts[] = $classStmt;
58+
}
59+
// still not added? try after last trait
60+
// @todo
61+
if ($isAdded) {
62+
$class->stmts = $newClassStmts;
3763
return;
3864
}
39-
$class->stmts[] = $stmt;
65+
// keep added at least as first stmt
66+
$class->stmts = \array_merge([$addedStmt], $class->stmts);
4067
}
4168
/**
4269
* @internal Use PropertyAdder service instead
@@ -50,42 +77,4 @@ public function addPropertyToClass(Class_ $class, string $name, ?Type $type) : v
5077
$property = $this->nodeFactory->createPrivatePropertyFromNameAndType($name, $type);
5178
$this->addAsFirstMethod($class, $property);
5279
}
53-
/**
54-
* @param Stmt[] $stmts
55-
* @return Stmt[]
56-
*/
57-
private function insertBefore(array $stmts, Stmt $stmt, int $key) : array
58-
{
59-
\array_splice($stmts, $key, 0, [$stmt]);
60-
return $stmts;
61-
}
62-
/**
63-
* @param \PhpParser\Node\Stmt\ClassConst|\PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Property $stmt
64-
*/
65-
private function isSuccessToInsertBeforeFirstMethod(Class_ $class, $stmt) : bool
66-
{
67-
foreach ($class->stmts as $key => $classStmt) {
68-
if (!$classStmt instanceof ClassMethod) {
69-
continue;
70-
}
71-
$class->stmts = $this->insertBefore($class->stmts, $stmt, $key);
72-
return \true;
73-
}
74-
return \false;
75-
}
76-
/**
77-
* @param \PhpParser\Node\Stmt\ClassConst|\PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Property $stmt
78-
*/
79-
private function isSuccessToInsertAfterLastProperty(Class_ $class, $stmt) : bool
80-
{
81-
$previousElement = null;
82-
foreach ($class->stmts as $key => $classStmt) {
83-
if ($previousElement instanceof Property && !$classStmt instanceof Property) {
84-
$class->stmts = $this->insertBefore($class->stmts, $stmt, $key);
85-
return \true;
86-
}
87-
$previousElement = $classStmt;
88-
}
89-
return \false;
90-
}
9180
}

0 commit comments

Comments
 (0)