Updated Rector to commit 4468743419

4468743419 [FEAT] refacto OrderAttributesRector (#2243)
This commit is contained in:
Tomas Votruba 2022-05-09 10:52:19 +00:00
parent fa402052ff
commit 0070cfd0cc
7 changed files with 112 additions and 37 deletions

View File

@ -2068,21 +2068,45 @@ Order attributes by desired names
- class: [`Rector\CodingStyle\Rector\ClassMethod\OrderAttributesRector`](../rules/CodingStyle/Rector/ClassMethod/OrderAttributesRector.php)
#### 1) Order by specific namespace
```php
use Rector\CodingStyle\Rector\ClassMethod\OrderAttributesRector;
use Rector\Config\RectorConfig;
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(OrderAttributesRector::class, ['First', 'Second']);
$rectorConfig->ruleWithConfiguration(OrderAttributesRector::class, [Annotation\First::class, Annotation\Second::class]);
};
```
```diff
+#[First]
#[Second]
-#[First]
+#[Annotation\First]
#[Annotation\Second]
-#[Annotation\First]
class Someclass
{
}
```
#### 2) Order alphabetically
```php
use Rector\CodingStyle\Rector\ClassMethod\OrderAttributesRector;
use Rector\Config\RectorConfig;
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(OrderAttributesRector::class, [Rector\CodingStyle\Rector\ClassMethod::ALPHABETICALLY]);
};
```
```diff
+#[Annotation\AAttribute]
#[Annotation\BAttribute]
-#[Annotation\AAttribute]
class Someclass
{
}

View File

@ -18,14 +18,15 @@ use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use RectorPrefix20220509\Webmozart\Assert\Assert;
/**
* @see \Rector\Tests\CodingStyle\Rector\ClassMethod\OrderAttributesRector\OrderAttributesRectorTest
* @see \Rector\Tests\CodingStyle\Rector\ClassMethod\OrderAttributesRector\SpecificOrder\OrderAttributesRectorTest
*/
final class OrderAttributesRector extends \Rector\Core\Rector\AbstractRector implements \Rector\Core\Contract\Rector\ConfigurableRectorInterface
{
public const ALPHABETICALLY = 'alphabetically';
/**
* @var array<string, int>
* @var array<string, int>|array<string>
*/
private $attributesOrderByName = [];
private $configuration = [];
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
{
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Order attributes by desired names', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample(<<<'CODE_SAMPLE'
@ -42,7 +43,21 @@ class Someclass
{
}
CODE_SAMPLE
, ['First', 'Second'])]);
, ['First', 'Second']), new \Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample(<<<'CODE_SAMPLE'
#[BAttribute]
#[AAttribute]
class Someclass
{
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
#[AAttribute]
#[BAttribute]
class Someclass
{
}
CODE_SAMPLE
, [self::ALPHABETICALLY])]);
}
/**
* @return array<class-string<Node>>
@ -60,12 +75,11 @@ CODE_SAMPLE
return null;
}
$originalAttrGroups = $node->attrGroups;
$currentAttrGroups = $originalAttrGroups;
\usort($currentAttrGroups, function (\PhpParser\Node\AttributeGroup $firstAttributeGroup, \PhpParser\Node\AttributeGroup $secondAttributeGroup) : int {
$firstAttributePosition = $this->resolveAttributeGroupPosition($firstAttributeGroup);
$secondAttributePosition = $this->resolveAttributeGroupPosition($secondAttributeGroup);
return $firstAttributePosition <=> $secondAttributePosition;
});
if ($this->isAlphabetically($this->configuration)) {
$currentAttrGroups = $this->sortAlphabetically($originalAttrGroups);
} else {
$currentAttrGroups = $this->sortBySpecificOrder($originalAttrGroups);
}
if ($currentAttrGroups === $originalAttrGroups) {
return null;
}
@ -75,15 +89,52 @@ CODE_SAMPLE
/**
* @param mixed[] $configuration
*/
public function configure(array $configuration) : void
public function configure(array $configuration = [self::ALPHABETICALLY]) : void
{
\RectorPrefix20220509\Webmozart\Assert\Assert::allString($configuration);
$this->attributesOrderByName = \array_flip($configuration);
\RectorPrefix20220509\Webmozart\Assert\Assert::minCount($configuration, 1);
if ($this->isAlphabetically($configuration)) {
$this->configuration = $configuration;
} else {
$this->configuration = \array_flip($configuration);
}
}
/**
* @param array<AttributeGroup> $originalAttrGroups
* @return array<AttributeGroup>
*/
private function sortAlphabetically(array $originalAttrGroups) : array
{
\usort($originalAttrGroups, function (\PhpParser\Node\AttributeGroup $firstAttributeGroup, \PhpParser\Node\AttributeGroup $secondAttributeGroup) : int {
$currentNamespace = $this->getName($firstAttributeGroup->attrs[0]->name);
$nextNamespace = $this->getName($secondAttributeGroup->attrs[0]->name);
return \strcmp($currentNamespace, $nextNamespace);
});
return $originalAttrGroups;
}
/**
* @param array<AttributeGroup> $originalAttrGroups
* @return array<AttributeGroup>
*/
private function sortBySpecificOrder(array $originalAttrGroups) : array
{
\usort($originalAttrGroups, function (\PhpParser\Node\AttributeGroup $firstAttributeGroup, \PhpParser\Node\AttributeGroup $secondAttributeGroup) : int {
$firstAttributePosition = $this->resolveAttributeGroupPosition($firstAttributeGroup);
$secondAttributePosition = $this->resolveAttributeGroupPosition($secondAttributeGroup);
return $firstAttributePosition <=> $secondAttributePosition;
});
return $originalAttrGroups;
}
private function resolveAttributeGroupPosition(\PhpParser\Node\AttributeGroup $attributeGroup) : int
{
$attrName = $this->getName($attributeGroup->attrs[0]->name);
// 1000 makes the attribute last, as positioned attributes have a higher priority
return $this->attributesOrderByName[$attrName] ?? 1000;
return (int) ($this->configuration[$attrName] ?? \count($this->configuration));
}
/**
* @param array<string, int>|array<string> $configuration
*/
private function isAlphabetically(array $configuration) : bool
{
return \count($configuration) === 1 && $configuration[0] === self::ALPHABETICALLY;
}
}

View File

@ -16,11 +16,11 @@ final class VersionResolver
/**
* @var string
*/
public const PACKAGE_VERSION = '97a33aa69494836b44ce68e0803e2568233f3e06';
public const PACKAGE_VERSION = '4468743419d944b511ec2bcb5697c9a8a636b725';
/**
* @var string
*/
public const RELEASE_DATE = '2022-05-09 06:18:42';
public const RELEASE_DATE = '2022-05-09 12:44:14';
/**
* @var string
*/

2
vendor/autoload.php vendored
View File

@ -9,4 +9,4 @@ if (PHP_VERSION_ID < 50600) {
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit07099ef05db3f8f711c50d39ba7abb0c::getLoader();
return ComposerAutoloaderInita04b588e187dbd6fc9f19f805dce0d89::getLoader();

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit07099ef05db3f8f711c50d39ba7abb0c
class ComposerAutoloaderInita04b588e187dbd6fc9f19f805dce0d89
{
private static $loader;
@ -22,19 +22,19 @@ class ComposerAutoloaderInit07099ef05db3f8f711c50d39ba7abb0c
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit07099ef05db3f8f711c50d39ba7abb0c', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInita04b588e187dbd6fc9f19f805dce0d89', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit07099ef05db3f8f711c50d39ba7abb0c', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInita04b588e187dbd6fc9f19f805dce0d89', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit07099ef05db3f8f711c50d39ba7abb0c::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInita04b588e187dbd6fc9f19f805dce0d89::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$includeFiles = \Composer\Autoload\ComposerStaticInit07099ef05db3f8f711c50d39ba7abb0c::$files;
$includeFiles = \Composer\Autoload\ComposerStaticInita04b588e187dbd6fc9f19f805dce0d89::$files;
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire07099ef05db3f8f711c50d39ba7abb0c($fileIdentifier, $file);
composerRequirea04b588e187dbd6fc9f19f805dce0d89($fileIdentifier, $file);
}
return $loader;
@ -46,7 +46,7 @@ class ComposerAutoloaderInit07099ef05db3f8f711c50d39ba7abb0c
* @param string $file
* @return void
*/
function composerRequire07099ef05db3f8f711c50d39ba7abb0c($fileIdentifier, $file)
function composerRequirea04b588e187dbd6fc9f19f805dce0d89($fileIdentifier, $file)
{
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInit07099ef05db3f8f711c50d39ba7abb0c
class ComposerStaticInita04b588e187dbd6fc9f19f805dce0d89
{
public static $files = array (
'320cde22f66dd4f5d3fd621d3e88b98f' => __DIR__ . '/..' . '/symfony/polyfill-ctype/bootstrap.php',
@ -3885,9 +3885,9 @@ class ComposerStaticInit07099ef05db3f8f711c50d39ba7abb0c
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit07099ef05db3f8f711c50d39ba7abb0c::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit07099ef05db3f8f711c50d39ba7abb0c::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit07099ef05db3f8f711c50d39ba7abb0c::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInita04b588e187dbd6fc9f19f805dce0d89::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInita04b588e187dbd6fc9f19f805dce0d89::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInita04b588e187dbd6fc9f19f805dce0d89::$classMap;
}, null, ClassLoader::class);
}

View File

@ -9,8 +9,8 @@ $loader = require_once __DIR__.'/autoload.php';
if (!class_exists('AutoloadIncluder', false) && !interface_exists('AutoloadIncluder', false) && !trait_exists('AutoloadIncluder', false)) {
spl_autoload_call('RectorPrefix20220509\AutoloadIncluder');
}
if (!class_exists('ComposerAutoloaderInit07099ef05db3f8f711c50d39ba7abb0c', false) && !interface_exists('ComposerAutoloaderInit07099ef05db3f8f711c50d39ba7abb0c', false) && !trait_exists('ComposerAutoloaderInit07099ef05db3f8f711c50d39ba7abb0c', false)) {
spl_autoload_call('RectorPrefix20220509\ComposerAutoloaderInit07099ef05db3f8f711c50d39ba7abb0c');
if (!class_exists('ComposerAutoloaderInita04b588e187dbd6fc9f19f805dce0d89', false) && !interface_exists('ComposerAutoloaderInita04b588e187dbd6fc9f19f805dce0d89', false) && !trait_exists('ComposerAutoloaderInita04b588e187dbd6fc9f19f805dce0d89', false)) {
spl_autoload_call('RectorPrefix20220509\ComposerAutoloaderInita04b588e187dbd6fc9f19f805dce0d89');
}
if (!class_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false) && !interface_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false) && !trait_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false)) {
spl_autoload_call('RectorPrefix20220509\Helmich\TypoScriptParser\Parser\AST\Statement');
@ -59,9 +59,9 @@ if (!function_exists('print_node')) {
return \RectorPrefix20220509\print_node(...func_get_args());
}
}
if (!function_exists('composerRequire07099ef05db3f8f711c50d39ba7abb0c')) {
function composerRequire07099ef05db3f8f711c50d39ba7abb0c() {
return \RectorPrefix20220509\composerRequire07099ef05db3f8f711c50d39ba7abb0c(...func_get_args());
if (!function_exists('composerRequirea04b588e187dbd6fc9f19f805dce0d89')) {
function composerRequirea04b588e187dbd6fc9f19f805dce0d89() {
return \RectorPrefix20220509\composerRequirea04b588e187dbd6fc9f19f805dce0d89(...func_get_args());
}
}
if (!function_exists('scanPath')) {