Skip to content

Commit a40b100

Browse files
committed
Updated Rector to commit f2be385b38e514ea3d702aa4cd0f08cf98305703
rectorphp/rector-src@f2be385 [Php81] Deprecate NewInInitializerRector as depends on context (#7913)
1 parent 0079a9c commit a40b100

8 files changed

Lines changed: 11 additions & 291 deletions

File tree

config/set/php81.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Rector\Php81\Rector\Array_\ArrayToFirstClassCallableRector;
1212
use Rector\Php81\Rector\Class_\MyCLabsClassToEnumRector;
1313
use Rector\Php81\Rector\Class_\SpatieEnumClassToEnumRector;
14-
use Rector\Php81\Rector\ClassMethod\NewInInitializerRector;
1514
use Rector\Php81\Rector\FuncCall\NullToStrictIntPregSlitFuncCallLimitArgRector;
1615
use Rector\Php81\Rector\FuncCall\NullToStrictStringFuncCallArgRector;
1716
use Rector\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector;
@@ -38,6 +37,5 @@
3837
ClosureFromCallableToFirstClassCallableRector::class,
3938
FunctionFirstClassCallableRector::class,
4039
RemoveReflectionSetAccessibleCallsRector::class,
41-
NewInInitializerRector::class,
4240
]);
4341
};

rules/Php81/NodeAnalyzer/CoalescePropertyAssignMatcher.php

Lines changed: 0 additions & 68 deletions
This file was deleted.

rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php

Lines changed: 0 additions & 73 deletions
This file was deleted.

rules/Php81/Rector/ClassMethod/NewInInitializerRector.php

Lines changed: 4 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,16 @@
44
namespace Rector\Php81\Rector\ClassMethod;
55

66
use PhpParser\Node;
7-
use PhpParser\Node\Expr;
8-
use PhpParser\Node\Expr\BinaryOp\Coalesce;
9-
use PhpParser\Node\NullableType;
10-
use PhpParser\Node\Param;
117
use PhpParser\Node\Stmt\Class_;
12-
use PhpParser\Node\Stmt\ClassMethod;
13-
use PhpParser\Node\Stmt\Property;
14-
use PHPStan\Reflection\ClassReflection;
15-
use Rector\FamilyTree\NodeAnalyzer\ClassChildAnalyzer;
16-
use Rector\NodeManipulator\StmtsManipulator;
17-
use Rector\Php81\NodeAnalyzer\CoalescePropertyAssignMatcher;
8+
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
9+
use Rector\Exception\ShouldNotHappenException;
1810
use Rector\Rector\AbstractRector;
19-
use Rector\Reflection\ReflectionResolver;
20-
use Rector\ValueObject\MethodName;
2111
use Rector\ValueObject\PhpVersionFeature;
2212
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
2313
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2414
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
25-
/**
26-
* @see \Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\NewInInitializerRectorTest
27-
*/
28-
final class NewInInitializerRector extends AbstractRector implements MinPhpVersionInterface
15+
final class NewInInitializerRector extends AbstractRector implements MinPhpVersionInterface, DeprecatedInterface
2916
{
30-
/**
31-
* @readonly
32-
*/
33-
private ReflectionResolver $reflectionResolver;
34-
/**
35-
* @readonly
36-
*/
37-
private ClassChildAnalyzer $classChildAnalyzer;
38-
/**
39-
* @readonly
40-
*/
41-
private CoalescePropertyAssignMatcher $coalescePropertyAssignMatcher;
42-
/**
43-
* @readonly
44-
*/
45-
private StmtsManipulator $stmtsManipulator;
46-
public function __construct(ReflectionResolver $reflectionResolver, ClassChildAnalyzer $classChildAnalyzer, CoalescePropertyAssignMatcher $coalescePropertyAssignMatcher, StmtsManipulator $stmtsManipulator)
47-
{
48-
$this->reflectionResolver = $reflectionResolver;
49-
$this->classChildAnalyzer = $classChildAnalyzer;
50-
$this->coalescePropertyAssignMatcher = $coalescePropertyAssignMatcher;
51-
$this->stmtsManipulator = $stmtsManipulator;
52-
}
5317
public function getRuleDefinition(): RuleDefinition
5418
{
5519
return new RuleDefinition('Replace property declaration of new state with direct new', [new CodeSample(<<<'CODE_SAMPLE'
@@ -87,112 +51,10 @@ public function getNodeTypes(): array
8751
*/
8852
public function refactor(Node $node): ?Node
8953
{
90-
if ($this->shouldSkipClass($node)) {
91-
return null;
92-
}
93-
$constructClassMethod = $node->getMethod(MethodName::CONSTRUCT);
94-
if (!$constructClassMethod instanceof ClassMethod) {
95-
return null;
96-
}
97-
$params = $this->resolveParams($constructClassMethod);
98-
if ($params === []) {
99-
return null;
100-
}
101-
$hasChanged = \false;
102-
foreach ((array) $constructClassMethod->stmts as $key => $stmt) {
103-
foreach ($params as $param) {
104-
$paramName = $this->getName($param);
105-
if ($param->type instanceof NullableType && $param->default === null) {
106-
continue;
107-
}
108-
$coalesce = $this->coalescePropertyAssignMatcher->matchCoalesceAssignsToLocalPropertyNamed($stmt, $paramName);
109-
if (!$coalesce instanceof Coalesce) {
110-
continue;
111-
}
112-
if ($this->stmtsManipulator->isVariableUsedInNextStmt($constructClassMethod, $key + 1, $paramName)) {
113-
continue;
114-
}
115-
$param->default = $coalesce->right;
116-
unset($constructClassMethod->stmts[$key]);
117-
$this->processPropertyPromotion($node, $param, $paramName);
118-
$hasChanged = \true;
119-
}
120-
}
121-
if ($hasChanged) {
122-
return $node;
123-
}
124-
return null;
54+
throw new ShouldNotHappenException(sprintf('"%s" is deprecated as depends on context. Cannot be automated. Use manually where needed instead', self::class));
12555
}
12656
public function provideMinPhpVersion(): int
12757
{
12858
return PhpVersionFeature::NEW_INITIALIZERS;
12959
}
130-
/**
131-
* @return Param[]
132-
*/
133-
private function resolveParams(ClassMethod $classMethod): array
134-
{
135-
$params = $this->matchConstructorParams($classMethod);
136-
if ($params === []) {
137-
return [];
138-
}
139-
if ($this->isOverrideAbstractMethod($classMethod)) {
140-
return [];
141-
}
142-
return $params;
143-
}
144-
private function isOverrideAbstractMethod(ClassMethod $classMethod): bool
145-
{
146-
$classReflection = $this->reflectionResolver->resolveClassReflection($classMethod);
147-
$methodName = $this->getName($classMethod);
148-
return $classReflection instanceof ClassReflection && $this->classChildAnalyzer->hasAbstractParentClassMethod($classReflection, $methodName);
149-
}
150-
private function processPropertyPromotion(Class_ $class, Param $param, string $paramName): void
151-
{
152-
foreach ($class->stmts as $key => $stmt) {
153-
if (!$stmt instanceof Property) {
154-
continue;
155-
}
156-
$property = $stmt;
157-
if (!$this->isName($stmt, $paramName)) {
158-
continue;
159-
}
160-
$param->flags = $property->flags;
161-
$param->attrGroups = array_merge($property->attrGroups, $param->attrGroups);
162-
unset($class->stmts[$key]);
163-
}
164-
}
165-
/**
166-
* @return Param[]
167-
*/
168-
private function matchConstructorParams(ClassMethod $classMethod): array
169-
{
170-
// skip empty constructor assigns, as we need those here
171-
if ($classMethod->stmts === null || $classMethod->stmts === []) {
172-
return [];
173-
}
174-
$params = array_filter($classMethod->params, static fn(Param $param): bool => $param->type instanceof NullableType);
175-
if ($params === []) {
176-
return $params;
177-
}
178-
$totalParams = count($classMethod->params);
179-
foreach (array_keys($params) as $key) {
180-
for ($iteration = $key + 1; $iteration < $totalParams; ++$iteration) {
181-
if (isset($classMethod->params[$iteration]) && !$classMethod->params[$iteration]->default instanceof Expr) {
182-
return [];
183-
}
184-
}
185-
}
186-
return $params;
187-
}
188-
private function shouldSkipClass(Class_ $class): bool
189-
{
190-
if ($class->stmts === []) {
191-
return \true;
192-
}
193-
if ($class->isAbstract()) {
194-
return \true;
195-
}
196-
return $class->isAnonymous();
197-
}
19860
}

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 = '3e751eca2d58330e8528e12056172508fc314a4b';
22+
public const PACKAGE_VERSION = 'f2be385b38e514ea3d702aa4cd0f08cf98305703';
2323
/**
2424
* @api
2525
* @var string
2626
*/
27-
public const RELEASE_DATE = '2026-04-06 19:56:56';
27+
public const RELEASE_DATE = '2026-04-07 18:30:39';
2828
/**
2929
* @var int
3030
*/

src/FamilyTree/NodeAnalyzer/ClassChildAnalyzer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@
88
use PHPStan\Reflection\Php\PhpMethodReflection;
99
use PHPStan\Type\MixedType;
1010
use PHPStan\Type\Type;
11+
/**
12+
* @api
13+
*/
1114
final class ClassChildAnalyzer
1215
{
1316
/**
1417
* Look both parent class and interface, yes, all PHP interface methods are abstract
18+
*
19+
* @api rector-symfony
1520
*/
1621
public function hasAbstractParentClassMethod(ClassReflection $classReflection, string $methodName): bool
1722
{

vendor/composer/autoload_classmap.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2242,8 +2242,6 @@
22422242
'Rector\\Php80\\ValueObject\\StrStartsWith' => $baseDir . '/rules/Php80/ValueObject/StrStartsWith.php',
22432243
'Rector\\Php81\\Enum\\AttributeName' => $baseDir . '/rules/Php81/Enum/AttributeName.php',
22442244
'Rector\\Php81\\Enum\\NameNullToStrictNullFunctionMap' => $baseDir . '/rules/Php81/Enum/NameNullToStrictNullFunctionMap.php',
2245-
'Rector\\Php81\\NodeAnalyzer\\CoalescePropertyAssignMatcher' => $baseDir . '/rules/Php81/NodeAnalyzer/CoalescePropertyAssignMatcher.php',
2246-
'Rector\\Php81\\NodeAnalyzer\\ComplexNewAnalyzer' => $baseDir . '/rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php',
22472245
'Rector\\Php81\\NodeFactory\\EnumFactory' => $baseDir . '/rules/Php81/NodeFactory/EnumFactory.php',
22482246
'Rector\\Php81\\NodeManipulator\\NullToStrictStringIntConverter' => $baseDir . '/rules/Php81/NodeManipulator/NullToStrictStringIntConverter.php',
22492247
'Rector\\Php81\\Rector\\Array_\\ArrayToFirstClassCallableRector' => $baseDir . '/rules/Php81/Rector/Array_/ArrayToFirstClassCallableRector.php',

vendor/composer/autoload_static.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2502,8 +2502,6 @@ class ComposerStaticInitc9819cb6f85619d8e1cbb4426044dd33
25022502
'Rector\\Php80\\ValueObject\\StrStartsWith' => __DIR__ . '/../..' . '/rules/Php80/ValueObject/StrStartsWith.php',
25032503
'Rector\\Php81\\Enum\\AttributeName' => __DIR__ . '/../..' . '/rules/Php81/Enum/AttributeName.php',
25042504
'Rector\\Php81\\Enum\\NameNullToStrictNullFunctionMap' => __DIR__ . '/../..' . '/rules/Php81/Enum/NameNullToStrictNullFunctionMap.php',
2505-
'Rector\\Php81\\NodeAnalyzer\\CoalescePropertyAssignMatcher' => __DIR__ . '/../..' . '/rules/Php81/NodeAnalyzer/CoalescePropertyAssignMatcher.php',
2506-
'Rector\\Php81\\NodeAnalyzer\\ComplexNewAnalyzer' => __DIR__ . '/../..' . '/rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php',
25072505
'Rector\\Php81\\NodeFactory\\EnumFactory' => __DIR__ . '/../..' . '/rules/Php81/NodeFactory/EnumFactory.php',
25082506
'Rector\\Php81\\NodeManipulator\\NullToStrictStringIntConverter' => __DIR__ . '/../..' . '/rules/Php81/NodeManipulator/NullToStrictStringIntConverter.php',
25092507
'Rector\\Php81\\Rector\\Array_\\ArrayToFirstClassCallableRector' => __DIR__ . '/../..' . '/rules/Php81/Rector/Array_/ArrayToFirstClassCallableRector.php',

0 commit comments

Comments
 (0)