Skip to content

Commit

Permalink
Improve InflectorSingularResolver (#1108)
Browse files Browse the repository at this point in the history
* misc

* [ci-review] Rector Rectify

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
TomasVotruba and actions-user committed Oct 29, 2021
1 parent c6d2339 commit e5186a7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
24 changes: 17 additions & 7 deletions rules/Naming/ExpectedNameResolver/InflectorSingularResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,7 @@ public function resolve(string $currentName): string
return $currentName;
}

$camelCases = Strings::matchAll($currentName, self::CAMELCASE_REGEX);
$singularValueVarName = '';
foreach ($camelCases as $camelCase) {
$singularValueVarName .= $this->inflector->singularize($camelCase['camelcase']);
}
$singularValueVarName = $this->singularizeCamelParts($currentName);

if (in_array($singularValueVarName, ['', '_'], true)) {
return $currentName;
Expand Down Expand Up @@ -90,14 +86,28 @@ private function resolveSingularizeMap(string $currentName): string|null
}

if (Strings::match($currentName, '#' . ucfirst($plural) . '#')) {
return Strings::replace($currentName, '#' . ucfirst($plural) . '#', ucfirst($singular));
$resolvedValue = Strings::replace($currentName, '#' . ucfirst($plural) . '#', ucfirst($singular));
return $this->singularizeCamelParts($resolvedValue);
}

if (Strings::match($currentName, '#' . $plural . '#')) {
return Strings::replace($currentName, '#' . $plural . '#', $singular);
$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) {
$resolvedName .= $this->inflector->singularize($camelCase['camelcase']);
}

return $resolvedName;
}
}
14 changes: 7 additions & 7 deletions rules/Transform/Rector/New_/NewToMethodCallRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
foreach ($this->newsToMethodCalls as $newToMethodCalls) {
if (! $this->isObjectType($node, $newToMethodCalls->getNewObjectType())) {
foreach ($this->newsToMethodCalls as $newToMethodCall) {
if (! $this->isObjectType($node, $newToMethodCall->getNewObjectType())) {
continue;
}

$serviceObjectType = $newToMethodCalls->getServiceObjectType();
$serviceObjectType = $newToMethodCall->getServiceObjectType();
$className = $node->getAttribute(AttributeKey::CLASS_NAME);
if ($className === $serviceObjectType->getClassName()) {
continue;
Expand All @@ -106,25 +106,25 @@ public function refactor(Node $node): ?Node

$propertyName = $this->getExistingFactoryPropertyName(
$class,
$newToMethodCalls->getServiceObjectType()
$newToMethodCall->getServiceObjectType()
);

if ($propertyName === null) {
$serviceObjectType = $newToMethodCalls->getServiceObjectType();
$serviceObjectType = $newToMethodCall->getServiceObjectType();
$propertyName = $this->classNaming->getShortName($serviceObjectType->getClassName());
$propertyName = lcfirst($propertyName);

$propertyMetadata = new PropertyMetadata(
$propertyName,
$newToMethodCalls->getServiceObjectType(),
$newToMethodCall->getServiceObjectType(),
Class_::MODIFIER_PRIVATE
);
$this->propertyToAddCollector->addPropertyToClass($class, $propertyMetadata);
}

$propertyFetch = new PropertyFetch(new Variable('this'), $propertyName);

return new MethodCall($propertyFetch, $newToMethodCalls->getServiceMethod(), $node->args);
return new MethodCall($propertyFetch, $newToMethodCall->getServiceMethod(), $node->args);
}

return $node;
Expand Down
7 changes: 4 additions & 3 deletions rules/Transform/Rector/StaticCall/StaticCallToNewRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
foreach ($this->staticCallsToNews as $staticCallsToNew) {
if (! $this->isName($node->class, $staticCallsToNew->getClass())) {
foreach ($this->staticCallsToNews as $staticCallToNew) {
if (! $this->isName($node->class, $staticCallToNew->getClass())) {
continue;
}

if (! $this->isName($node->name, $staticCallsToNew->getMethod())) {
if (! $this->isName($node->name, $staticCallToNew->getMethod())) {
continue;
}

Expand All @@ -103,6 +103,7 @@ public function configure(array $configuration): void
{
$staticCallsToNews = $configuration[self::STATIC_CALLS_TO_NEWS] ?? [];
Assert::allIsAOf($staticCallsToNews, StaticCallToNew::class);

$this->staticCallsToNews = $staticCallsToNews;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,8 @@ public function provideData(): Iterator
yield ['nestedNews', 'nestedNew'];
yield ['news', 'new'];
yield ['argsOrOptions', 'argOrOption'];
// news and plural
yield ['staticCallsToNews', 'staticCallToNew'];
yield ['newsToMethodCalls', 'newToMethodCall'];
}
}

0 comments on commit e5186a7

Please sign in to comment.