Updated Rector to commit c6d2339cb7

c6d2339cb7 Add InflectorSingularResolverTest (#1107)
This commit is contained in:
Tomas Votruba 2021-10-29 23:49:05 +00:00
parent 1eaf7106a4
commit 76ed56b260
12 changed files with 88 additions and 57 deletions

View File

@ -10,6 +10,7 @@ use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\MethodReflection;
use Rector\CodingStyle\NodeAnalyzer\SpreadVariablesCollector;
use Rector\CodingStyle\Reflection\VendorLocationDetector;
@ -90,12 +91,8 @@ CODE_SAMPLE
}
private function refactorClassMethod(\PhpParser\Node\Stmt\ClassMethod $classMethod) : ?\PhpParser\Node\Stmt\ClassMethod
{
$scope = $classMethod->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::SCOPE);
if ($scope instanceof \PHPStan\Analyser\Scope && $classMethod->isPublic()) {
$classReflection = $scope->getClassReflection();
if ($classReflection->isSubclassOf('PHPUnit\\Framework\\TestCase')) {
return null;
}
if ($this->isInPHPUnitTestCase($classMethod)) {
return null;
}
$spreadParams = $this->spreadVariablesCollector->resolveFromClassMethod($classMethod);
if ($spreadParams === []) {
@ -193,4 +190,19 @@ CODE_SAMPLE
}
return \false;
}
private function isInPHPUnitTestCase(\PhpParser\Node\Stmt\ClassMethod $classMethod) : bool
{
$scope = $classMethod->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::SCOPE);
if (!$scope instanceof \PHPStan\Analyser\Scope) {
return \false;
}
if (!$classMethod->isPublic()) {
return \false;
}
$classReflection = $scope->getClassReflection();
if (!$classReflection instanceof \PHPStan\Reflection\ClassReflection) {
return \false;
}
return $classReflection->isSubclassOf('PHPUnit\\Framework\\TestCase');
}
}

View File

@ -5,17 +5,20 @@ namespace Rector\Naming\ExpectedNameResolver;
use RectorPrefix20211029\Doctrine\Inflector\Inflector;
use RectorPrefix20211029\Nette\Utils\Strings;
/**
* @see \Rector\Core\Tests\Naming\ExpectedNameResolver\InflectorSingularResolverTest
*/
final class InflectorSingularResolver
{
/**
* @var array<string, string>
*/
private const SINGULAR_VERB = ['news' => 'new'];
private const SINGULARIZE_MAP = ['news' => 'new'];
/**
* @var string
* @see https://regex101.com/r/lbQaGC/3
*/
private const CAMELCASE_REGEX = '#(?<camelcase>([a-z]+|[A-Z]{1,}[a-z]+|_))#';
private const CAMELCASE_REGEX = '#(?<camelcase>([a-z\\d]+|[A-Z\\d]{1,}[a-z\\d]+|_))#';
/**
* @var string
* @see https://regex101.com/r/2aGdkZ/2
@ -39,8 +42,9 @@ final class InflectorSingularResolver
if ($matchBy) {
return \RectorPrefix20211029\Nette\Utils\Strings::substring($currentName, 0, -\strlen($matchBy['by']));
}
if (\array_key_exists($currentName, self::SINGULAR_VERB)) {
return self::SINGULAR_VERB[$currentName];
$resolvedValue = $this->resolveSingularizeMap($currentName);
if ($resolvedValue !== null) {
return $resolvedValue;
}
if (\strncmp($currentName, self::SINGLE, \strlen(self::SINGLE)) === 0) {
return $currentName;
@ -50,10 +54,7 @@ final class InflectorSingularResolver
foreach ($camelCases as $camelCase) {
$singularValueVarName .= $this->inflector->singularize($camelCase['camelcase']);
}
if ($singularValueVarName === '') {
return $currentName;
}
if ($singularValueVarName === '_') {
if (\in_array($singularValueVarName, ['', '_'], \true)) {
return $currentName;
}
$singularValueVarName = $singularValueVarName === $currentName ? self::SINGLE . \ucfirst($singularValueVarName) : $singularValueVarName;
@ -66,4 +67,22 @@ final class InflectorSingularResolver
}
return $currentName;
}
/**
* @return string|null
*/
private function resolveSingularizeMap(string $currentName)
{
foreach (self::SINGULARIZE_MAP as $plural => $singular) {
if ($currentName === $plural) {
return $singular;
}
if (\RectorPrefix20211029\Nette\Utils\Strings::match($currentName, '#' . \ucfirst($plural) . '#')) {
return \RectorPrefix20211029\Nette\Utils\Strings::replace($currentName, '#' . \ucfirst($plural) . '#', \ucfirst($singular));
}
if (\RectorPrefix20211029\Nette\Utils\Strings::match($currentName, '#' . $plural . '#')) {
return \RectorPrefix20211029\Nette\Utils\Strings::replace($currentName, '#' . $plural . '#', $singular);
}
}
return null;
}
}

View File

@ -83,27 +83,27 @@ CODE_SAMPLE
*/
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
{
foreach ($this->newsToMethodCalls as $newsToMethodCall) {
if (!$this->isObjectType($node, $newsToMethodCall->getNewObjectType())) {
foreach ($this->newsToMethodCalls as $newToMethodCalls) {
if (!$this->isObjectType($node, $newToMethodCalls->getNewObjectType())) {
continue;
}
$serviceObjectType = $newsToMethodCall->getServiceObjectType();
$serviceObjectType = $newToMethodCalls->getServiceObjectType();
$className = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::CLASS_NAME);
if ($className === $serviceObjectType->getClassName()) {
continue;
}
/** @var Class_ $class */
$class = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::CLASS_NODE);
$propertyName = $this->getExistingFactoryPropertyName($class, $newsToMethodCall->getServiceObjectType());
$propertyName = $this->getExistingFactoryPropertyName($class, $newToMethodCalls->getServiceObjectType());
if ($propertyName === null) {
$serviceObjectType = $newsToMethodCall->getServiceObjectType();
$serviceObjectType = $newToMethodCalls->getServiceObjectType();
$propertyName = $this->classNaming->getShortName($serviceObjectType->getClassName());
$propertyName = \lcfirst($propertyName);
$propertyMetadata = new \Rector\PostRector\ValueObject\PropertyMetadata($propertyName, $newsToMethodCall->getServiceObjectType(), \PhpParser\Node\Stmt\Class_::MODIFIER_PRIVATE);
$propertyMetadata = new \Rector\PostRector\ValueObject\PropertyMetadata($propertyName, $newToMethodCalls->getServiceObjectType(), \PhpParser\Node\Stmt\Class_::MODIFIER_PRIVATE);
$this->propertyToAddCollector->addPropertyToClass($class, $propertyMetadata);
}
$propertyFetch = new \PhpParser\Node\Expr\PropertyFetch(new \PhpParser\Node\Expr\Variable('this'), $propertyName);
return new \PhpParser\Node\Expr\MethodCall($propertyFetch, $newsToMethodCall->getServiceMethod(), $node->args);
return new \PhpParser\Node\Expr\MethodCall($propertyFetch, $newToMethodCalls->getServiceMethod(), $node->args);
}
return $node;
}

View File

@ -62,11 +62,11 @@ CODE_SAMPLE
*/
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
{
foreach ($this->staticCallsToNews as $staticCallToNews) {
if (!$this->isName($node->class, $staticCallToNews->getClass())) {
foreach ($this->staticCallsToNews as $staticCallsToNew) {
if (!$this->isName($node->class, $staticCallsToNew->getClass())) {
continue;
}
if (!$this->isName($node->name, $staticCallToNews->getMethod())) {
if (!$this->isName($node->name, $staticCallsToNew->getMethod())) {
continue;
}
$class = $this->getName($node->class);

View File

@ -16,11 +16,11 @@ final class VersionResolver
/**
* @var string
*/
public const PACKAGE_VERSION = '3bec30af83142f34a31d00017222cda91781e5f3';
public const PACKAGE_VERSION = 'c6d2339cb72f51d572918054389e04582619f16b';
/**
* @var string
*/
public const RELEASE_DATE = '2021-10-29 23:06:12';
public const RELEASE_DATE = '2021-10-29 23:31:02';
public static function resolvePackageVersion() : string
{
$process = new \RectorPrefix20211029\Symfony\Component\Process\Process(['git', 'log', '--pretty="%H"', '-n1', 'HEAD'], __DIR__);

2
vendor/autoload.php vendored
View File

@ -4,4 +4,4 @@
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit03be2723cc25fa922622dd95798a0d2c::getLoader();
return ComposerAutoloaderInit8f334491453d0d9203188f6d0db7ebbf::getLoader();

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit03be2723cc25fa922622dd95798a0d2c
class ComposerAutoloaderInit8f334491453d0d9203188f6d0db7ebbf
{
private static $loader;
@ -22,15 +22,15 @@ class ComposerAutoloaderInit03be2723cc25fa922622dd95798a0d2c
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit03be2723cc25fa922622dd95798a0d2c', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit8f334491453d0d9203188f6d0db7ebbf', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(\dirname(__FILE__)));
spl_autoload_unregister(array('ComposerAutoloaderInit03be2723cc25fa922622dd95798a0d2c', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit8f334491453d0d9203188f6d0db7ebbf', 'loadClassLoader'));
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
if ($useStaticLoader) {
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit03be2723cc25fa922622dd95798a0d2c::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit8f334491453d0d9203188f6d0db7ebbf::getInitializer($loader));
} else {
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
@ -42,19 +42,19 @@ class ComposerAutoloaderInit03be2723cc25fa922622dd95798a0d2c
$loader->register(true);
if ($useStaticLoader) {
$includeFiles = Composer\Autoload\ComposerStaticInit03be2723cc25fa922622dd95798a0d2c::$files;
$includeFiles = Composer\Autoload\ComposerStaticInit8f334491453d0d9203188f6d0db7ebbf::$files;
} else {
$includeFiles = require __DIR__ . '/autoload_files.php';
}
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire03be2723cc25fa922622dd95798a0d2c($fileIdentifier, $file);
composerRequire8f334491453d0d9203188f6d0db7ebbf($fileIdentifier, $file);
}
return $loader;
}
}
function composerRequire03be2723cc25fa922622dd95798a0d2c($fileIdentifier, $file)
function composerRequire8f334491453d0d9203188f6d0db7ebbf($fileIdentifier, $file)
{
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
require $file;

View File

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

View File

@ -833,12 +833,12 @@
"source": {
"type": "git",
"url": "https:\/\/github.com\/phpstan\/phpstan.git",
"reference": "00dd9b25d901c63753fffd178e1303db59f0b1e0"
"reference": "9529499b60b1846099c588a8a46abfca6fc8e610"
},
"dist": {
"type": "zip",
"url": "https:\/\/api.github.com\/repos\/phpstan\/phpstan\/zipball\/00dd9b25d901c63753fffd178e1303db59f0b1e0",
"reference": "00dd9b25d901c63753fffd178e1303db59f0b1e0",
"url": "https:\/\/api.github.com\/repos\/phpstan\/phpstan\/zipball\/9529499b60b1846099c588a8a46abfca6fc8e610",
"reference": "9529499b60b1846099c588a8a46abfca6fc8e610",
"shasum": ""
},
"require": {
@ -847,7 +847,7 @@
"conflict": {
"phpstan\/phpstan-shim": "*"
},
"time": "2021-10-29T14:46:33+00:00",
"time": "2021-10-29T22:23:14+00:00",
"default-branch": true,
"bin": [
"phpstan",
@ -3817,12 +3817,12 @@
"source": {
"type": "git",
"url": "https:\/\/github.com\/symplify\/astral.git",
"reference": "ad8a2dafbf7fa6960fa384066fad9cf2ba45b0fe"
"reference": "b22ad66ec871f7d70859120890b9220f4898b771"
},
"dist": {
"type": "zip",
"url": "https:\/\/api.github.com\/repos\/symplify\/astral\/zipball\/ad8a2dafbf7fa6960fa384066fad9cf2ba45b0fe",
"reference": "ad8a2dafbf7fa6960fa384066fad9cf2ba45b0fe",
"url": "https:\/\/api.github.com\/repos\/symplify\/astral\/zipball\/b22ad66ec871f7d70859120890b9220f4898b771",
"reference": "b22ad66ec871f7d70859120890b9220f4898b771",
"shasum": ""
},
"require": {
@ -3868,7 +3868,7 @@
"phpunit\/phpunit": "^9.5",
"symplify\/easy-testing": "^9.5"
},
"time": "2021-10-29T18:17:30+00:00",
"time": "2021-10-29T22:33:26+00:00",
"default-branch": true,
"type": "phpstan-extension",
"extra": {
@ -3914,12 +3914,12 @@
"source": {
"type": "git",
"url": "https:\/\/github.com\/symplify\/autowire-array-parameter.git",
"reference": "4b02e8d4f9d231534260aaaf1309c4488b24d84b"
"reference": "652360ef343afcf3c354a0ff1eb59a9fed2427e0"
},
"dist": {
"type": "zip",
"url": "https:\/\/api.github.com\/repos\/symplify\/autowire-array-parameter\/zipball\/4b02e8d4f9d231534260aaaf1309c4488b24d84b",
"reference": "4b02e8d4f9d231534260aaaf1309c4488b24d84b",
"url": "https:\/\/api.github.com\/repos\/symplify\/autowire-array-parameter\/zipball\/652360ef343afcf3c354a0ff1eb59a9fed2427e0",
"reference": "652360ef343afcf3c354a0ff1eb59a9fed2427e0",
"shasum": ""
},
"require": {
@ -3961,7 +3961,7 @@
"require-dev": {
"phpunit\/phpunit": "^9.5"
},
"time": "2021-10-29T18:17:35+00:00",
"time": "2021-10-29T22:33:28+00:00",
"default-branch": true,
"type": "library",
"extra": {

File diff suppressed because one or more lines are too long

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('RectorPrefix20211029\AutoloadIncluder');
}
if (!class_exists('ComposerAutoloaderInit03be2723cc25fa922622dd95798a0d2c', false) && !interface_exists('ComposerAutoloaderInit03be2723cc25fa922622dd95798a0d2c', false) && !trait_exists('ComposerAutoloaderInit03be2723cc25fa922622dd95798a0d2c', false)) {
spl_autoload_call('RectorPrefix20211029\ComposerAutoloaderInit03be2723cc25fa922622dd95798a0d2c');
if (!class_exists('ComposerAutoloaderInit8f334491453d0d9203188f6d0db7ebbf', false) && !interface_exists('ComposerAutoloaderInit8f334491453d0d9203188f6d0db7ebbf', false) && !trait_exists('ComposerAutoloaderInit8f334491453d0d9203188f6d0db7ebbf', false)) {
spl_autoload_call('RectorPrefix20211029\ComposerAutoloaderInit8f334491453d0d9203188f6d0db7ebbf');
}
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('RectorPrefix20211029\Helmich\TypoScriptParser\Parser\AST\Statement');
@ -3306,9 +3306,9 @@ if (!function_exists('print_node')) {
return \RectorPrefix20211029\print_node(...func_get_args());
}
}
if (!function_exists('composerRequire03be2723cc25fa922622dd95798a0d2c')) {
function composerRequire03be2723cc25fa922622dd95798a0d2c() {
return \RectorPrefix20211029\composerRequire03be2723cc25fa922622dd95798a0d2c(...func_get_args());
if (!function_exists('composerRequire8f334491453d0d9203188f6d0db7ebbf')) {
function composerRequire8f334491453d0d9203188f6d0db7ebbf() {
return \RectorPrefix20211029\composerRequire8f334491453d0d9203188f6d0db7ebbf(...func_get_args());
}
}
if (!function_exists('parseArgs')) {

View File

@ -44,7 +44,7 @@ final class FuncCallValueResolver implements \RectorPrefix20211029\Symplify\Astr
if ($this->simpleNameResolver->isName($expr, 'getcwd')) {
return \dirname($currentFilePath);
}
$args = $expr->args;
$args = $expr->getArgs();
$arguments = [];
foreach ($args as $arg) {
$arguments[] = $this->constExprEvaluator->evaluateDirectly($arg->value);