Skip to content

Commit

Permalink
Remove startsWith() and endsWith(), use getName() directly and compar…
Browse files Browse the repository at this point in the history
…e as needed
  • Loading branch information
TomasVotruba committed Sep 9, 2023
1 parent 6a88d44 commit 3cecb0e
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 42 deletions.
35 changes: 0 additions & 35 deletions packages/NodeNameResolver/NodeNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,41 +165,6 @@ public function getNames(array $nodes): array
return $names;
}

/**
* @param string|string[] $suffix
*/
public function endsWith(Node $node, string|array $suffix): bool
{
$name = $this->getName($node);
if (! is_string($name)) {
return false;
}

if (! is_array($suffix)) {
$suffixes = [$suffix];
} else {
$suffixes = $suffix;
}

foreach ($suffixes as $suffix) {
if (str_ends_with($name, $suffix) || str_ends_with($name, ucfirst($suffix))) {
return true;
}
}

return false;
}

public function startsWith(Node $node, string $prefix): bool
{
$name = $this->getName($node);
if (! is_string($name)) {
return false;
}

return str_starts_with($name, $prefix);
}

public function getShortName(string | Name | Identifier | ClassLike $name): string
{
return $this->classNaming->getShortName($name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ private function hasParentClassController(Class_ $class): bool
return false;
}

return $this->nodeNameResolver->endsWith($class->extends, ['Controller', 'Presenter']);
$parentClassName = $this->nodeNameResolver->getName($class->extends);
if (str_ends_with($parentClassName, 'Controller')) {
return true;
}

return str_ends_with($parentClassName, 'Presenter');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,13 @@ public function resolve(Property $property, ClassLike $classLike): ?string
return null;
}

$propertyName = $this->nodeNameResolver->getName($property);

// skip if already has suffix
if ($this->nodeNameResolver->endsWith($property, $expectedName->getName())) {
if (str_ends_with($propertyName, $expectedName->getName()) || str_ends_with(
$propertyName,
ucfirst($expectedName->getName())
)) {
return null;
}

Expand Down
5 changes: 1 addition & 4 deletions rules/Naming/Naming/ExpectedNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,8 @@ public function resolveForParamIfNotYet(Param $param): ?string

/** @var string $currentName */
$currentName = $this->nodeNameResolver->getName($param->var);
if ($currentName === $expectedName) {
return null;
}

if ($this->nodeNameResolver->endsWith($param->var, $expectedName)) {
if ($currentName === $expectedName || str_ends_with($currentName, $expectedName)) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
private function shouldSkipClassMethod(ClassMethod $classMethod): bool
{
// edge case in nette framework
if ($this->nodeNameResolver->startsWith($classMethod->name, 'createComponent')) {
/** @var string $methodName */
$methodName = $this->getName($classMethod->name);
if (str_starts_with($methodName, 'createComponent')) {
return true;
}

Expand Down

0 comments on commit 3cecb0e

Please sign in to comment.