-
-
Notifications
You must be signed in to change notification settings - Fork 440
Expand file tree
/
Copy pathInflectorSingularResolver.php
More file actions
104 lines (82 loc) · 2.97 KB
/
InflectorSingularResolver.php
File metadata and controls
104 lines (82 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
declare(strict_types=1);
namespace Rector\Naming\ExpectedNameResolver;
use Doctrine\Inflector\Inflector;
use Nette\Utils\Strings;
use Rector\Util\StringUtils;
/**
* @see \Rector\Tests\Naming\ExpectedNameResolver\InflectorSingularResolverTest
*/
final readonly class InflectorSingularResolver
{
/**
* @var array<string, string>
*/
private const array SINGULARIZE_MAP = [
'news' => 'new',
];
/**
* @see https://regex101.com/r/lbQaGC/3
*/
private const string CAMELCASE_REGEX = '#(?<camelcase>([a-z\d]+|[A-Z\d]{1,}[a-z\d]+|_))#';
/**
* @see https://regex101.com/r/2aGdkZ/2
*/
private const string BY_MIDDLE_REGEX = '#(?<by>By[A-Z][a-zA-Z]+)#';
private const string CAMELCASE = 'camelcase';
public function __construct(
private Inflector $inflector
) {
}
public function resolve(string $currentName): string
{
$matchBy = Strings::match($currentName, self::BY_MIDDLE_REGEX);
if ($matchBy !== null) {
return Strings::substring($currentName, 0, -strlen((string) $matchBy['by']));
}
$resolvedValue = $this->resolveSingularizeMap($currentName);
if ($resolvedValue !== null) {
return $resolvedValue;
}
$singularValueVarName = $this->singularizeCamelParts($currentName);
if (in_array($singularValueVarName, ['', '_'], true)) {
return $currentName;
}
$length = strlen($singularValueVarName);
if ($length < 40) {
return $singularValueVarName;
}
return $currentName;
}
private function resolveSingularizeMap(string $currentName): string|null
{
foreach (self::SINGULARIZE_MAP as $plural => $singular) {
if ($currentName === $plural) {
return $singular;
}
if (StringUtils::isMatch($currentName, '#' . ucfirst($plural) . '#')) {
$resolvedValue = Strings::replace($currentName, '#' . ucfirst($plural) . '#', ucfirst($singular));
return $this->singularizeCamelParts($resolvedValue);
}
if (StringUtils::isMatch($currentName, '#' . $plural . '#')) {
$resolvedValue = Strings::replace($currentName, '#' . $plural . '#', $singular);
return $this->singularizeCamelParts($resolvedValue);
}
}
return null;
}
private function singularizeCamelParts(string $currentName): string
{
$camelCases = Strings::matchAll($currentName, self::CAMELCASE_REGEX);
$resolvedName = '';
foreach ($camelCases as $camelCase) {
if (in_array($camelCase[self::CAMELCASE], ['is', 'has', 'cms', 'this'], true)) {
$value = $camelCase[self::CAMELCASE];
} else {
$value = $this->inflector->singularize((string) $camelCase[self::CAMELCASE]);
}
$resolvedName .= $value;
}
return $resolvedName;
}
}