Skip to content

Commit

Permalink
Rector 0.18.6
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Oct 24, 2023
1 parent 52923dc commit 02041b2
Show file tree
Hide file tree
Showing 57 changed files with 2,181 additions and 47 deletions.
38 changes: 11 additions & 27 deletions rules/Naming/ExpectedNameResolver/InflectorSingularResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
declare (strict_types=1);
namespace Rector\Naming\ExpectedNameResolver;

use RectorPrefix202310\Doctrine\Inflector\Inflector;
use RectorPrefix202310\Nette\Utils\Strings;
use Rector\Core\Util\StringUtils;
/**
* @see \Rector\Core\Tests\Naming\ExpectedNameResolver\InflectorSingularResolverTest
*/
final class InflectorSingularResolver
{
/**
* @readonly
* @var \Doctrine\Inflector\Inflector
*/
private $inflector;
/**
* @var array<string, string>
*/
Expand All @@ -28,6 +34,10 @@ final class InflectorSingularResolver
* @var string
*/
private const CAMELCASE = 'camelcase';
public function __construct(Inflector $inflector)
{
$this->inflector = $inflector;
}
public function resolve(string $currentName) : string
{
$matchBy = Strings::match($currentName, self::BY_MIDDLE_REGEX);
Expand Down Expand Up @@ -70,38 +80,12 @@ private function singularizeCamelParts(string $currentName) : string
$camelCases = Strings::matchAll($currentName, self::CAMELCASE_REGEX);
$resolvedName = '';
foreach ($camelCases as $camelCase) {
$value = $this->singularize($camelCase[self::CAMELCASE]);
$value = $this->inflector->singularize($camelCase[self::CAMELCASE]);
if (\in_array($camelCase[self::CAMELCASE], ['is', 'has'], \true)) {
$value = $camelCase[self::CAMELCASE];
}
$resolvedName .= $value;
}
return $resolvedName;
}
// see https://gist.github.com/peter-mcconnell/9757549
private function singularize(string $word) : string
{
$singular = ['/(quiz)zes$/i' => '\\1', '/(matr)ices$/i' => '\\1ix', '/(vert|ind)ices$/i' => '\\1ex', '/^(ox)en/i' => '\\1', '/^(axe)s$/i' => '\\1', '/(alias|status|iris|hoax|hero|gas|fax|circus|canvas|atlas)es$/i' => '\\1', '/([octop|vir])i$/i' => '\\1us', '/(cris|ax|test)es$/i' => '\\1is', '/(shoe|grave|glove|foe|dive|database|curve|cookie|cave|cache|avalanche|abuse)s$/i' => '\\1', '/(o)es$/i' => '\\1', '/(bus|lens)es$/i' => '\\1', '/([m|l])ice$/i' => '\\1ouse', '/(x|ch|ss|sh)es$/i' => '\\1', '/(m)ovies$/i' => '\\1ovie', '/(s)eries$/i' => '\\1eries', '/([^aeiouy]|qu)ies$/i' => '\\1y', '/([lr])ves$/i' => '\\1f', '/(tive)s$/i' => '\\1', '/(hive)s$/i' => '\\1', '/([^f])ves$/i' => '\\1fe', '/(^analy)ses$/i' => '\\1sis', '/((a)naly|(b)a|(d)iagno|empha|(p)arenthe|(p)rogno|(s)ynop|(t)he|(oa)|neuro)ses$/i' => '1\\2sis', '/([ti]|memorand|curricul)a$/i' => '\\1um', '/(n)ews$/i' => '\\1ews', '/s$/i' => ''];
$irregular = ['alumnus' => 'alumni', 'person' => 'people', 'man' => 'men', 'bacillus' => 'bacilli', 'criterion' => 'criteria', 'fungus' => 'fungi', 'foot' => 'feet', 'goose' => 'geese', 'genus' => 'genera', 'hippopotamus' => 'hippopotami', 'child' => 'children', 'code' => 'codes', 'octopus' => 'octopuses', 'olive' => 'olives', 'chateau' => 'chateaux', 'plateau' => 'plateaux', 'niveau' => 'niveaux', 'passerby' => 'passersby', 'save' => 'saves', 'sex' => 'sexes', 'syllabus' => 'syllabi', 'stimulus' => 'stimuli', 'sku' => 'skus', 'sieve' => 'sieves', 'taxon' => 'taxa', 'taxi' => 'taxis', 'tax' => 'taxes', 'tooth' => 'teeth', 'tights' => 'tights', 'Thief' => 'Thieves', 'terminus' => 'termini', 'larva' => 'larvae', 'leaf' => 'leaves', 'loaf' => 'loaves', 'move' => 'moves', 'nucleus' => 'nuclei', 'valve' => 'valves', 'wave' => 'waves', 'zombie' => 'zombies'];
// keep words ending in $ignore
$ignore = ['breeches', 'britches', 'cantus', 'chassis', 'corps', 'coreopsis', 'contretemps', 'coitus', 'clothes', 'clippers', 'data', 'diabetes', 'debris', 'equipment', 'gallows', 'hijinks', 'herpes', 'headquarters', 'information', 'rice', 'socialmedia', 'jeans', 'jackanapes', 'nodemedia', 'money', 'mumps', 'mews', 'innings', 'nexus', 'rhinoceros', 'rabies', 'pants', 'police', 'pliers', 'progress', 'proceedings', 'pincers', 'scissors', 'species', 'series', 'status', 'shorts', 'shears', 'fish', 'sheep', 'press', 'sms', 'trousers', 'trivia', 'yengeese'];
$lower_word = \strtolower($word);
foreach ($ignore as $ignore_word) {
if (\substr($lower_word, -1 * \strlen($ignore_word)) === $ignore_word) {
return $word;
}
}
foreach ($irregular as $singular_word => $plural_word) {
$arr = Strings::match($word, '/(' . $plural_word . ')$/i');
if ($arr !== null) {
return Strings::replace($word, '/(' . $plural_word . ')$/i', \substr($arr[0], 0, 1) . \substr($singular_word, 1));
}
}
foreach ($singular as $rule => $replacement) {
if (Strings::match($word, $rule) !== null) {
return Strings::replace($word, $rule, $replacement);
}
}
return $word;
}
}
14 changes: 7 additions & 7 deletions rules/Naming/Naming/PropertyNaming.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use PHPStan\Type\TypeWithClassName;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Util\StringUtils;
use Rector\Naming\ExpectedNameResolver\InflectorSingularResolver;
use Rector\Naming\RectorNamingInflector;
use Rector\Naming\ValueObject\ExpectedName;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType;
Expand All @@ -25,9 +25,9 @@ final class PropertyNaming
{
/**
* @readonly
* @var \Rector\Naming\ExpectedNameResolver\InflectorSingularResolver
* @var \Rector\Naming\RectorNamingInflector
*/
private $inflectorSingularResolver;
private $rectorNamingInflector;
/**
* @readonly
* @var \Rector\NodeTypeResolver\NodeTypeResolver
Expand Down Expand Up @@ -55,9 +55,9 @@ final class PropertyNaming
* @var string
*/
private const GET_PREFIX_REGEX = '#^get(?<root_name>[A-Z].+)#';
public function __construct(InflectorSingularResolver $inflectorSingularResolver, NodeTypeResolver $nodeTypeResolver)
public function __construct(RectorNamingInflector $rectorNamingInflector, NodeTypeResolver $nodeTypeResolver)
{
$this->inflectorSingularResolver = $inflectorSingularResolver;
$this->rectorNamingInflector = $rectorNamingInflector;
$this->nodeTypeResolver = $nodeTypeResolver;
}
public function getExpectedNameFromMethodName(string $methodName) : ?ExpectedName
Expand All @@ -67,7 +67,7 @@ public function getExpectedNameFromMethodName(string $methodName) : ?ExpectedNam
return null;
}
$originalName = \lcfirst((string) $matches['root_name']);
return new ExpectedName($originalName, $this->inflectorSingularResolver->resolve($originalName));
return new ExpectedName($originalName, $this->rectorNamingInflector->singularize($originalName));
}
public function getExpectedNameFromType(Type $type) : ?ExpectedName
{
Expand Down Expand Up @@ -99,7 +99,7 @@ public function getExpectedNameFromType(Type $type) : ?ExpectedName
$shortClassName = $this->normalizeShortClassName($shortClassName);
// prolong too short generic names with one namespace up
$originalName = $this->prolongIfTooShort($shortClassName, $className);
return new ExpectedName($originalName, $this->inflectorSingularResolver->resolve($originalName));
return new ExpectedName($originalName, $this->rectorNamingInflector->singularize($originalName));
}
/**
* @param \PHPStan\Type\ThisType|\PHPStan\Type\ObjectType|string $objectType
Expand Down
34 changes: 34 additions & 0 deletions rules/Naming/RectorNamingInflector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare (strict_types=1);
namespace Rector\Naming;

use RectorPrefix202310\Doctrine\Inflector\Inflector;
use RectorPrefix202310\Nette\Utils\Strings;
final class RectorNamingInflector
{
/**
* @readonly
* @var \Doctrine\Inflector\Inflector
*/
private $inflector;
/**
* @var string
* @see https://regex101.com/r/VqVvke/3
*/
private const DATA_INFO_SUFFIX_REGEX = '#^(?<prefix>.+)(?<suffix>Data|Info)$#';
public function __construct(Inflector $inflector)
{
$this->inflector = $inflector;
}
public function singularize(string $name) : string
{
$matches = Strings::match($name, self::DATA_INFO_SUFFIX_REGEX);
if ($matches === null) {
return $this->inflector->singularize($name);
}
$singularized = $this->inflector->singularize($matches['prefix']);
$uninflectable = $matches['suffix'];
return $singularized . $uninflectable;
}
}
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 = '1c54292ed68e46f4ddfa2adb21f28130f308b99c';
public const PACKAGE_VERSION = '0.18.6';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-10-24 16:57:53';
public const RELEASE_DATE = '2023-10-24 16:57:04';
/**
* @var int
*/
Expand Down
6 changes: 6 additions & 0 deletions src/DependencyInjection/LazyContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
declare (strict_types=1);
namespace Rector\Core\DependencyInjection;

use RectorPrefix202310\Doctrine\Inflector\Inflector;
use RectorPrefix202310\Doctrine\Inflector\Rules\English\InflectorFactory;
use RectorPrefix202310\Illuminate\Container\Container;
use PhpParser\Lexer;
use PHPStan\Analyser\NodeScopeResolver;
Expand Down Expand Up @@ -258,6 +260,10 @@ public function create() : RectorConfig
return $application;
});
$rectorConfig->when(ConsoleApplication::class)->needs('$commands')->giveTagged(Command::class);
$rectorConfig->singleton(Inflector::class, static function () : Inflector {
$inflectorFactory = new InflectorFactory();
return $inflectorFactory->build();
});
$rectorConfig->tag(ProcessCommand::class, Command::class);
$rectorConfig->tag(WorkerCommand::class, Command::class);
$rectorConfig->tag(SetupCICommand::class, Command::class);
Expand Down
2 changes: 1 addition & 1 deletion vendor/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInitb5644708b7ed04b3c875c18370cf2e53::getLoader();
return ComposerAutoloaderInit18ad0e678efbbb500e116f7c54cccdd4::getLoader();
42 changes: 42 additions & 0 deletions vendor/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,47 @@
'RectorPrefix202310\\Composer\\XdebugHandler\\Process' => $vendorDir . '/composer/xdebug-handler/src/Process.php',
'RectorPrefix202310\\Composer\\XdebugHandler\\Status' => $vendorDir . '/composer/xdebug-handler/src/Status.php',
'RectorPrefix202310\\Composer\\XdebugHandler\\XdebugHandler' => $vendorDir . '/composer/xdebug-handler/src/XdebugHandler.php',
'RectorPrefix202310\\Doctrine\\Inflector\\CachedWordInflector' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/CachedWordInflector.php',
'RectorPrefix202310\\Doctrine\\Inflector\\GenericLanguageInflectorFactory' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/GenericLanguageInflectorFactory.php',
'RectorPrefix202310\\Doctrine\\Inflector\\Inflector' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/Inflector.php',
'RectorPrefix202310\\Doctrine\\Inflector\\InflectorFactory' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/InflectorFactory.php',
'RectorPrefix202310\\Doctrine\\Inflector\\Language' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/Language.php',
'RectorPrefix202310\\Doctrine\\Inflector\\LanguageInflectorFactory' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/LanguageInflectorFactory.php',
'RectorPrefix202310\\Doctrine\\Inflector\\NoopWordInflector' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/NoopWordInflector.php',
'RectorPrefix202310\\Doctrine\\Inflector\\Rules\\English\\Inflectible' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/Rules/English/Inflectible.php',
'RectorPrefix202310\\Doctrine\\Inflector\\Rules\\English\\InflectorFactory' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/Rules/English/InflectorFactory.php',
'RectorPrefix202310\\Doctrine\\Inflector\\Rules\\English\\Rules' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/Rules/English/Rules.php',
'RectorPrefix202310\\Doctrine\\Inflector\\Rules\\English\\Uninflected' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/Rules/English/Uninflected.php',
'RectorPrefix202310\\Doctrine\\Inflector\\Rules\\French\\Inflectible' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/Rules/French/Inflectible.php',
'RectorPrefix202310\\Doctrine\\Inflector\\Rules\\French\\InflectorFactory' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/Rules/French/InflectorFactory.php',
'RectorPrefix202310\\Doctrine\\Inflector\\Rules\\French\\Rules' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/Rules/French/Rules.php',
'RectorPrefix202310\\Doctrine\\Inflector\\Rules\\French\\Uninflected' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/Rules/French/Uninflected.php',
'RectorPrefix202310\\Doctrine\\Inflector\\Rules\\NorwegianBokmal\\Inflectible' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/Rules/NorwegianBokmal/Inflectible.php',
'RectorPrefix202310\\Doctrine\\Inflector\\Rules\\NorwegianBokmal\\InflectorFactory' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/Rules/NorwegianBokmal/InflectorFactory.php',
'RectorPrefix202310\\Doctrine\\Inflector\\Rules\\NorwegianBokmal\\Rules' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/Rules/NorwegianBokmal/Rules.php',
'RectorPrefix202310\\Doctrine\\Inflector\\Rules\\NorwegianBokmal\\Uninflected' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/Rules/NorwegianBokmal/Uninflected.php',
'RectorPrefix202310\\Doctrine\\Inflector\\Rules\\Pattern' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/Rules/Pattern.php',
'RectorPrefix202310\\Doctrine\\Inflector\\Rules\\Patterns' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/Rules/Patterns.php',
'RectorPrefix202310\\Doctrine\\Inflector\\Rules\\Portuguese\\Inflectible' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/Rules/Portuguese/Inflectible.php',
'RectorPrefix202310\\Doctrine\\Inflector\\Rules\\Portuguese\\InflectorFactory' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/Rules/Portuguese/InflectorFactory.php',
'RectorPrefix202310\\Doctrine\\Inflector\\Rules\\Portuguese\\Rules' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/Rules/Portuguese/Rules.php',
'RectorPrefix202310\\Doctrine\\Inflector\\Rules\\Portuguese\\Uninflected' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/Rules/Portuguese/Uninflected.php',
'RectorPrefix202310\\Doctrine\\Inflector\\Rules\\Ruleset' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/Rules/Ruleset.php',
'RectorPrefix202310\\Doctrine\\Inflector\\Rules\\Spanish\\Inflectible' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/Rules/Spanish/Inflectible.php',
'RectorPrefix202310\\Doctrine\\Inflector\\Rules\\Spanish\\InflectorFactory' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/Rules/Spanish/InflectorFactory.php',
'RectorPrefix202310\\Doctrine\\Inflector\\Rules\\Spanish\\Rules' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/Rules/Spanish/Rules.php',
'RectorPrefix202310\\Doctrine\\Inflector\\Rules\\Spanish\\Uninflected' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/Rules/Spanish/Uninflected.php',
'RectorPrefix202310\\Doctrine\\Inflector\\Rules\\Substitution' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/Rules/Substitution.php',
'RectorPrefix202310\\Doctrine\\Inflector\\Rules\\Substitutions' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/Rules/Substitutions.php',
'RectorPrefix202310\\Doctrine\\Inflector\\Rules\\Transformation' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/Rules/Transformation.php',
'RectorPrefix202310\\Doctrine\\Inflector\\Rules\\Transformations' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/Rules/Transformations.php',
'RectorPrefix202310\\Doctrine\\Inflector\\Rules\\Turkish\\Inflectible' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/Rules/Turkish/Inflectible.php',
'RectorPrefix202310\\Doctrine\\Inflector\\Rules\\Turkish\\InflectorFactory' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/Rules/Turkish/InflectorFactory.php',
'RectorPrefix202310\\Doctrine\\Inflector\\Rules\\Turkish\\Rules' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/Rules/Turkish/Rules.php',
'RectorPrefix202310\\Doctrine\\Inflector\\Rules\\Turkish\\Uninflected' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/Rules/Turkish/Uninflected.php',
'RectorPrefix202310\\Doctrine\\Inflector\\Rules\\Word' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/Rules/Word.php',
'RectorPrefix202310\\Doctrine\\Inflector\\RulesetInflector' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/RulesetInflector.php',
'RectorPrefix202310\\Doctrine\\Inflector\\WordInflector' => $vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector/WordInflector.php',
'RectorPrefix202310\\Evenement\\EventEmitter' => $vendorDir . '/evenement/evenement/src/EventEmitter.php',
'RectorPrefix202310\\Evenement\\EventEmitterInterface' => $vendorDir . '/evenement/evenement/src/EventEmitterInterface.php',
'RectorPrefix202310\\Evenement\\EventEmitterTrait' => $vendorDir . '/evenement/evenement/src/EventEmitterTrait.php',
Expand Down Expand Up @@ -1492,6 +1533,7 @@
'Rector\\Naming\\PropertyRenamer\\MatchTypePropertyRenamer' => $baseDir . '/rules/Naming/PropertyRenamer/MatchTypePropertyRenamer.php',
'Rector\\Naming\\PropertyRenamer\\PropertyFetchRenamer' => $baseDir . '/rules/Naming/PropertyRenamer/PropertyFetchRenamer.php',
'Rector\\Naming\\PropertyRenamer\\PropertyPromotionRenamer' => $baseDir . '/rules/Naming/PropertyRenamer/PropertyPromotionRenamer.php',
'Rector\\Naming\\RectorNamingInflector' => $baseDir . '/rules/Naming/RectorNamingInflector.php',
'Rector\\Naming\\Rector\\Assign\\RenameVariableToMatchMethodCallReturnTypeRector' => $baseDir . '/rules/Naming/Rector/Assign/RenameVariableToMatchMethodCallReturnTypeRector.php',
'Rector\\Naming\\Rector\\ClassMethod\\RenameParamToMatchTypeRector' => $baseDir . '/rules/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector.php',
'Rector\\Naming\\Rector\\ClassMethod\\RenameVariableToMatchNewTypeRector' => $baseDir . '/rules/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector.php',
Expand Down
1 change: 1 addition & 0 deletions vendor/composer/autoload_psr4.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
'RectorPrefix202310\\Illuminate\\Container\\' => array($vendorDir . '/illuminate/container'),
'RectorPrefix202310\\Fidry\\CpuCoreCounter\\' => array($vendorDir . '/fidry/cpu-core-counter/src'),
'RectorPrefix202310\\Evenement\\' => array($vendorDir . '/evenement/evenement/src'),
'RectorPrefix202310\\Doctrine\\Inflector\\' => array($vendorDir . '/doctrine/inflector/lib/Doctrine/Inflector'),
'RectorPrefix202310\\Composer\\XdebugHandler\\' => array($vendorDir . '/composer/xdebug-handler/src'),
'RectorPrefix202310\\Composer\\Semver\\' => array($vendorDir . '/composer/semver/src'),
'RectorPrefix202310\\Composer\\Pcre\\' => array($vendorDir . '/composer/pcre/src'),
Expand Down
Loading

0 comments on commit 02041b2

Please sign in to comment.