Skip to content

Commit

Permalink
Updated Rector to commit 2c6790841ec149b1849c5b931b7b46648fe2f18e
Browse files Browse the repository at this point in the history
rectorphp/rector-src@2c67908 Fix ClassDependencyManipulator to add dependency on right position (#6413)
  • Loading branch information
TomasVotruba committed Nov 8, 2024
1 parent 13fa633 commit ee75027
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 47 deletions.
4 changes: 2 additions & 2 deletions src/Application/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '1b1807b0f8426641e13e5914389e1c118b3a3d14';
public const PACKAGE_VERSION = '2c6790841ec149b1849c5b931b7b46648fe2f18e';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2024-11-08 17:26:04';
public const RELEASE_DATE = '2024-11-08 14:55:18';
/**
* @var int
*/
Expand Down
3 changes: 3 additions & 0 deletions src/NodeManipulator/ClassDependencyManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
use Rector\TypeDeclaration\NodeAnalyzer\AutowiredClassMethodOrPropertyAnalyzer;
use Rector\ValueObject\MethodName;
use Rector\ValueObject\PhpVersionFeature;
/**
* @see \Rector\Tests\NodeManipulator\ClassDependencyManipulatorTest
*/
final class ClassDependencyManipulator
{
/**
Expand Down
79 changes: 34 additions & 45 deletions src/NodeManipulator/ClassInsertManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
declare (strict_types=1);
namespace Rector\NodeManipulator;

use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassConst;
use PhpParser\Node\Stmt\ClassMethod;
Expand All @@ -24,19 +23,47 @@ public function __construct(NodeFactory $nodeFactory)
}
/**
* @api
* @param \PhpParser\Node\Stmt\Property|\PhpParser\Node\Stmt\ClassConst|\PhpParser\Node\Stmt\ClassMethod $stmt
* @param \PhpParser\Node\Stmt\Property|\PhpParser\Node\Stmt\ClassConst|\PhpParser\Node\Stmt\ClassMethod $addedStmt
*/
public function addAsFirstMethod(Class_ $class, $stmt) : void
public function addAsFirstMethod(Class_ $class, $addedStmt) : void
{
$scope = $class->getAttribute(AttributeKey::SCOPE);
$stmt->setAttribute(AttributeKey::SCOPE, $scope);
if ($this->isSuccessToInsertBeforeFirstMethod($class, $stmt)) {
$addedStmt->setAttribute(AttributeKey::SCOPE, $scope);
// no stmts? add this one
if ($class->stmts === []) {
$class->stmts[] = $addedStmt;
return;
}
if ($this->isSuccessToInsertAfterLastProperty($class, $stmt)) {
$newClassStmts = [];
$isAdded = \false;
foreach ($class->stmts as $key => $classStmt) {
$nextStmt = $class->stmts[$key + 1] ?? null;
if ($isAdded === \false) {
// first class method
if ($classStmt instanceof ClassMethod) {
$newClassStmts[] = $addedStmt;
$newClassStmts[] = $classStmt;
$isAdded = \true;
continue;
}
// after last property
if ($classStmt instanceof Property && !$nextStmt instanceof Property) {
$newClassStmts[] = $classStmt;
$newClassStmts[] = $addedStmt;
$isAdded = \true;
continue;
}
}
$newClassStmts[] = $classStmt;
}
// still not added? try after last trait
// @todo
if ($isAdded) {
$class->stmts = $newClassStmts;
return;
}
$class->stmts[] = $stmt;
// keep added at least as first stmt
$class->stmts = \array_merge([$addedStmt], $class->stmts);
}
/**
* @internal Use PropertyAdder service instead
Expand All @@ -50,42 +77,4 @@ public function addPropertyToClass(Class_ $class, string $name, ?Type $type) : v
$property = $this->nodeFactory->createPrivatePropertyFromNameAndType($name, $type);
$this->addAsFirstMethod($class, $property);
}
/**
* @param Stmt[] $stmts
* @return Stmt[]
*/
private function insertBefore(array $stmts, Stmt $stmt, int $key) : array
{
\array_splice($stmts, $key, 0, [$stmt]);
return $stmts;
}
/**
* @param \PhpParser\Node\Stmt\ClassConst|\PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Property $stmt
*/
private function isSuccessToInsertBeforeFirstMethod(Class_ $class, $stmt) : bool
{
foreach ($class->stmts as $key => $classStmt) {
if (!$classStmt instanceof ClassMethod) {
continue;
}
$class->stmts = $this->insertBefore($class->stmts, $stmt, $key);
return \true;
}
return \false;
}
/**
* @param \PhpParser\Node\Stmt\ClassConst|\PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Property $stmt
*/
private function isSuccessToInsertAfterLastProperty(Class_ $class, $stmt) : bool
{
$previousElement = null;
foreach ($class->stmts as $key => $classStmt) {
if ($previousElement instanceof Property && !$classStmt instanceof Property) {
$class->stmts = $this->insertBefore($class->stmts, $stmt, $key);
return \true;
}
$previousElement = $classStmt;
}
return \false;
}
}

0 comments on commit ee75027

Please sign in to comment.