mirror of
https://github.com/rectorphp/rector.git
synced 2025-04-13 03:52:15 +02:00
Updated Rector to commit 6aca457745976acad6cec231ffc400f0d399a386
6aca457745
[Php81][php82] Add AttributeGroupNewLiner to make new line based on token on ReadOnlyPropertyRector and ReadOnlyClassRector (#6618)
This commit is contained in:
parent
fa66798989
commit
ca7f3cd180
49
rules/Php81/NodeManipulator/AttributeGroupNewLiner.php
Normal file
49
rules/Php81/NodeManipulator/AttributeGroupNewLiner.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Php81\NodeManipulator;
|
||||
|
||||
use PhpParser\Node\Param;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
use Rector\ValueObject\Application\File;
|
||||
final class AttributeGroupNewLiner
|
||||
{
|
||||
/**
|
||||
* @param \PhpParser\Node\Stmt\Property|\PhpParser\Node\Param|\PhpParser\Node\Stmt\Class_ $node
|
||||
*/
|
||||
public function newLine(File $file, $node) : void
|
||||
{
|
||||
$oldTokens = $file->getOldTokens();
|
||||
$startTokenPos = $node->getStartTokenPos();
|
||||
if (!isset($oldTokens[$startTokenPos])) {
|
||||
return;
|
||||
}
|
||||
if ($oldTokens[$startTokenPos]->text !== '#[') {
|
||||
return;
|
||||
}
|
||||
$iteration = 1;
|
||||
$lastKey = \array_key_last($node->attrGroups);
|
||||
if ($lastKey === null) {
|
||||
return;
|
||||
}
|
||||
$lastAttributeTokenPos = $node->attrGroups[$lastKey]->getEndTokenPos();
|
||||
while (isset($oldTokens[$startTokenPos + $iteration])) {
|
||||
if ($startTokenPos + $iteration === $lastAttributeTokenPos) {
|
||||
if ($oldTokens[$startTokenPos + $iteration]->text !== ']') {
|
||||
break;
|
||||
}
|
||||
if (\trim($oldTokens[$startTokenPos + $iteration + 1]->text ?? '') === '') {
|
||||
$space = \ltrim($oldTokens[$startTokenPos + $iteration + 1]->text ?? '', "\r\n");
|
||||
} elseif (\trim($oldTokens[$startTokenPos - 1]->text ?? '') === '') {
|
||||
$space = \ltrim($oldTokens[$startTokenPos - 1]->text ?? '', "\r\n");
|
||||
} else {
|
||||
$space = '';
|
||||
}
|
||||
$oldTokens[$startTokenPos + $iteration]->text = "]\n" . $space;
|
||||
break;
|
||||
}
|
||||
++$iteration;
|
||||
}
|
||||
}
|
||||
}
|
@ -22,7 +22,7 @@ use Rector\Comments\NodeDocBlock\DocBlockUpdater;
|
||||
use Rector\NodeAnalyzer\ParamAnalyzer;
|
||||
use Rector\NodeManipulator\PropertyFetchAssignManipulator;
|
||||
use Rector\NodeManipulator\PropertyManipulator;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\Php81\NodeManipulator\AttributeGroupNewLiner;
|
||||
use Rector\PhpParser\Node\BetterNodeFinder;
|
||||
use Rector\PHPStan\ScopeFetcher;
|
||||
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
|
||||
@ -66,7 +66,11 @@ final class ReadOnlyPropertyRector extends AbstractRector implements MinPhpVersi
|
||||
* @readonly
|
||||
*/
|
||||
private DocBlockUpdater $docBlockUpdater;
|
||||
public function __construct(PropertyManipulator $propertyManipulator, PropertyFetchAssignManipulator $propertyFetchAssignManipulator, ParamAnalyzer $paramAnalyzer, VisibilityManipulator $visibilityManipulator, BetterNodeFinder $betterNodeFinder, PhpDocInfoFactory $phpDocInfoFactory, DocBlockUpdater $docBlockUpdater)
|
||||
/**
|
||||
* @readonly
|
||||
*/
|
||||
private AttributeGroupNewLiner $attributeGroupNewLiner;
|
||||
public function __construct(PropertyManipulator $propertyManipulator, PropertyFetchAssignManipulator $propertyFetchAssignManipulator, ParamAnalyzer $paramAnalyzer, VisibilityManipulator $visibilityManipulator, BetterNodeFinder $betterNodeFinder, PhpDocInfoFactory $phpDocInfoFactory, DocBlockUpdater $docBlockUpdater, AttributeGroupNewLiner $attributeGroupNewLiner)
|
||||
{
|
||||
$this->propertyManipulator = $propertyManipulator;
|
||||
$this->propertyFetchAssignManipulator = $propertyFetchAssignManipulator;
|
||||
@ -75,6 +79,7 @@ final class ReadOnlyPropertyRector extends AbstractRector implements MinPhpVersi
|
||||
$this->betterNodeFinder = $betterNodeFinder;
|
||||
$this->phpDocInfoFactory = $phpDocInfoFactory;
|
||||
$this->docBlockUpdater = $docBlockUpdater;
|
||||
$this->attributeGroupNewLiner = $attributeGroupNewLiner;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
@ -177,7 +182,7 @@ CODE_SAMPLE
|
||||
$this->visibilityManipulator->makeReadonly($property);
|
||||
$attributeGroups = $property->attrGroups;
|
||||
if ($attributeGroups !== []) {
|
||||
$property->setAttribute(AttributeKey::ORIGINAL_NODE, null);
|
||||
$this->attributeGroupNewLiner->newLine($this->file, $property);
|
||||
}
|
||||
$this->removeReadOnlyDoc($property);
|
||||
return $property;
|
||||
@ -226,7 +231,7 @@ CODE_SAMPLE
|
||||
return null;
|
||||
}
|
||||
if ($param->attrGroups !== []) {
|
||||
$param->setAttribute(AttributeKey::ORIGINAL_NODE, null);
|
||||
$this->attributeGroupNewLiner->newLine($this->file, $param);
|
||||
}
|
||||
$this->visibilityManipulator->makeReadonly($param);
|
||||
$this->removeReadOnlyDoc($param);
|
||||
|
@ -14,9 +14,9 @@ use PHPStan\BetterReflection\Reflection\Adapter\ReflectionProperty;
|
||||
use PHPStan\Reflection\ClassReflection;
|
||||
use PHPStan\Reflection\ReflectionProvider;
|
||||
use Rector\NodeAnalyzer\ClassAnalyzer;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
|
||||
use Rector\Php81\Enum\AttributeName;
|
||||
use Rector\Php81\NodeManipulator\AttributeGroupNewLiner;
|
||||
use Rector\PHPStan\ScopeFetcher;
|
||||
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
|
||||
use Rector\Rector\AbstractRector;
|
||||
@ -47,12 +47,17 @@ final class ReadOnlyClassRector extends AbstractRector implements MinPhpVersionI
|
||||
* @readonly
|
||||
*/
|
||||
private ReflectionProvider $reflectionProvider;
|
||||
public function __construct(ClassAnalyzer $classAnalyzer, VisibilityManipulator $visibilityManipulator, PhpAttributeAnalyzer $phpAttributeAnalyzer, ReflectionProvider $reflectionProvider)
|
||||
/**
|
||||
* @readonly
|
||||
*/
|
||||
private AttributeGroupNewLiner $attributeGroupNewLiner;
|
||||
public function __construct(ClassAnalyzer $classAnalyzer, VisibilityManipulator $visibilityManipulator, PhpAttributeAnalyzer $phpAttributeAnalyzer, ReflectionProvider $reflectionProvider, AttributeGroupNewLiner $attributeGroupNewLiner)
|
||||
{
|
||||
$this->classAnalyzer = $classAnalyzer;
|
||||
$this->visibilityManipulator = $visibilityManipulator;
|
||||
$this->phpAttributeAnalyzer = $phpAttributeAnalyzer;
|
||||
$this->reflectionProvider = $reflectionProvider;
|
||||
$this->attributeGroupNewLiner = $attributeGroupNewLiner;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
@ -98,21 +103,18 @@ CODE_SAMPLE
|
||||
foreach ($constructClassMethod->getParams() as $param) {
|
||||
$this->visibilityManipulator->removeReadonly($param);
|
||||
if ($param->attrGroups !== []) {
|
||||
// invoke reprint with correct newline
|
||||
$param->setAttribute(AttributeKey::ORIGINAL_NODE, null);
|
||||
$this->attributeGroupNewLiner->newLine($this->file, $param);
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach ($node->getProperties() as $property) {
|
||||
$this->visibilityManipulator->removeReadonly($property);
|
||||
if ($property->attrGroups !== []) {
|
||||
// invoke reprint with correct newline
|
||||
$property->setAttribute(AttributeKey::ORIGINAL_NODE, null);
|
||||
$this->attributeGroupNewLiner->newLine($this->file, $property);
|
||||
}
|
||||
}
|
||||
if ($node->attrGroups !== []) {
|
||||
// invoke reprint with correct readonly newline
|
||||
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);
|
||||
$this->attributeGroupNewLiner->newLine($this->file, $node);
|
||||
}
|
||||
return $node;
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ namespace Rector\Privatization\Rector\Class_;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PHPStan\Reflection\ReflectionProvider;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\Php81\NodeManipulator\AttributeGroupNewLiner;
|
||||
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
@ -24,10 +24,15 @@ final class FinalizeTestCaseClassRector extends AbstractRector
|
||||
* @readonly
|
||||
*/
|
||||
private VisibilityManipulator $visibilityManipulator;
|
||||
public function __construct(ReflectionProvider $reflectionProvider, VisibilityManipulator $visibilityManipulator)
|
||||
/**
|
||||
* @readonly
|
||||
*/
|
||||
private AttributeGroupNewLiner $attributeGroupNewLiner;
|
||||
public function __construct(ReflectionProvider $reflectionProvider, VisibilityManipulator $visibilityManipulator, AttributeGroupNewLiner $attributeGroupNewLiner)
|
||||
{
|
||||
$this->reflectionProvider = $reflectionProvider;
|
||||
$this->visibilityManipulator = $visibilityManipulator;
|
||||
$this->attributeGroupNewLiner = $attributeGroupNewLiner;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
@ -78,7 +83,7 @@ CODE_SAMPLE
|
||||
return null;
|
||||
}
|
||||
if ($node->attrGroups !== []) {
|
||||
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);
|
||||
$this->attributeGroupNewLiner->newLine($this->file, $node);
|
||||
}
|
||||
$this->visibilityManipulator->makeFinal($node);
|
||||
return $node;
|
||||
|
@ -19,12 +19,12 @@ final class VersionResolver
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = '8fea0f5b27dbdb4d3e0b2d672d43c60cb0c2ea2c';
|
||||
public const PACKAGE_VERSION = '6aca457745976acad6cec231ffc400f0d399a386';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2024-12-19 08:35:14';
|
||||
public const RELEASE_DATE = '2024-12-19 15:58:19';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
1
vendor/composer/autoload_classmap.php
vendored
1
vendor/composer/autoload_classmap.php
vendored
@ -1995,6 +1995,7 @@ return array(
|
||||
'Rector\\Php81\\NodeAnalyzer\\CoalesePropertyAssignMatcher' => $baseDir . '/rules/Php81/NodeAnalyzer/CoalesePropertyAssignMatcher.php',
|
||||
'Rector\\Php81\\NodeAnalyzer\\ComplexNewAnalyzer' => $baseDir . '/rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php',
|
||||
'Rector\\Php81\\NodeFactory\\EnumFactory' => $baseDir . '/rules/Php81/NodeFactory/EnumFactory.php',
|
||||
'Rector\\Php81\\NodeManipulator\\AttributeGroupNewLiner' => $baseDir . '/rules/Php81/NodeManipulator/AttributeGroupNewLiner.php',
|
||||
'Rector\\Php81\\Rector\\Array_\\FirstClassCallableRector' => $baseDir . '/rules/Php81/Rector/Array_/FirstClassCallableRector.php',
|
||||
'Rector\\Php81\\Rector\\ClassMethod\\NewInInitializerRector' => $baseDir . '/rules/Php81/Rector/ClassMethod/NewInInitializerRector.php',
|
||||
'Rector\\Php81\\Rector\\Class_\\MyCLabsClassToEnumRector' => $baseDir . '/rules/Php81/Rector/Class_/MyCLabsClassToEnumRector.php',
|
||||
|
1
vendor/composer/autoload_static.php
vendored
1
vendor/composer/autoload_static.php
vendored
@ -2214,6 +2214,7 @@ class ComposerStaticInite1459f150f08d0eee2804dfc37f818db
|
||||
'Rector\\Php81\\NodeAnalyzer\\CoalesePropertyAssignMatcher' => __DIR__ . '/../..' . '/rules/Php81/NodeAnalyzer/CoalesePropertyAssignMatcher.php',
|
||||
'Rector\\Php81\\NodeAnalyzer\\ComplexNewAnalyzer' => __DIR__ . '/../..' . '/rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php',
|
||||
'Rector\\Php81\\NodeFactory\\EnumFactory' => __DIR__ . '/../..' . '/rules/Php81/NodeFactory/EnumFactory.php',
|
||||
'Rector\\Php81\\NodeManipulator\\AttributeGroupNewLiner' => __DIR__ . '/../..' . '/rules/Php81/NodeManipulator/AttributeGroupNewLiner.php',
|
||||
'Rector\\Php81\\Rector\\Array_\\FirstClassCallableRector' => __DIR__ . '/../..' . '/rules/Php81/Rector/Array_/FirstClassCallableRector.php',
|
||||
'Rector\\Php81\\Rector\\ClassMethod\\NewInInitializerRector' => __DIR__ . '/../..' . '/rules/Php81/Rector/ClassMethod/NewInInitializerRector.php',
|
||||
'Rector\\Php81\\Rector\\Class_\\MyCLabsClassToEnumRector' => __DIR__ . '/../..' . '/rules/Php81/Rector/Class_/MyCLabsClassToEnumRector.php',
|
||||
|
Loading…
x
Reference in New Issue
Block a user