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 f974a82 commit 9ec7c18
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 58 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 @@ -201,27 +201,27 @@ private function isInLoopWithoutContinueOrBreak(If_ $if): bool
private function processReplaceIfs(
If_ $if,
array $conditions,
Return_ $cloneIffNextReturn,
Return_ $ifNextReturn,
array $afters,
?Stmt $nextStmt
): array {
$ifs = $this->invertedIfFactory->createFromConditions($if, $conditions, $cloneIffNextReturn, $nextStmt);
$ifs = $this->invertedIfFactory->createFromConditions($if, $conditions, $ifNextReturn, $nextStmt);
$this->mirrorComments($ifs[0], $if);

$result = array_merge($ifs, $afters);
if ($if->stmts[0] instanceof Return_) {
return $result;
}

if (! $cloneIffNextReturn->expr instanceof Expr) {
if (! $ifNextReturn->expr instanceof Expr) {
return $result;
}

if ($this->contextAnalyzer->isInLoop($if)) {
return $result;
}

return array_merge($result, [$cloneIffNextReturn]);
return array_merge($result, [$ifNextReturn]);
}

private function shouldSkip(If_ $if, ?Stmt $nexStmt): bool
Expand Down
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
23 changes: 12 additions & 11 deletions rules/Renaming/Rector/MethodCall/RenameMethodRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Interface_;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
Expand Down Expand Up @@ -120,7 +121,7 @@ private function shouldSkipClassMethod(
}

private function hasClassNewClassMethod(
Class_|Interface_ $classOrInterface,
Class_|Interface_ $classOrInterface,
MethodCallRenameInterface $methodCallRename
): bool {
return (bool) $classOrInterface->getMethod($methodCallRename->getNewMethod());
Expand Down Expand Up @@ -163,7 +164,13 @@ private function refactorClass(Class_|Interface_ $classOrInterface, Scope $scope
}

foreach ($this->methodCallRenames as $methodCallRename) {
if ($this->shouldSkipRename($methodName, $classMethod, $methodCallRename, $classReflection, $classOrInterface)) {
if ($this->shouldSkipRename(
$methodName,
$classMethod,
$methodCallRename,
$classReflection,
$classOrInterface
)) {
continue;
}

Expand All @@ -181,12 +188,11 @@ private function refactorClass(Class_|Interface_ $classOrInterface, Scope $scope

private function shouldSkipRename(
string $methodName,
Node\Stmt\ClassMethod $classMethod,
ClassMethod $classMethod,
MethodCallRenameInterface $methodCallRename,
ClassReflection $classReflection,
Class_|Interface_ $classOrInterface
): bool
{
): bool {
if (! $this->nodeNameResolver->isStringName($methodName, $methodCallRename->getOldMethod())) {
return true;
}
Expand All @@ -201,12 +207,7 @@ private function shouldSkipRename(
if ($this->shouldKeepForParentInterface($methodCallRename, $classReflection)) {
return true;
}

if ($this->hasClassNewClassMethod($classOrInterface, $methodCallRename)) {
return true;
}

return false;
return $this->hasClassNewClassMethod($classOrInterface, $methodCallRename);
}

private function refactorMethodCallAndStaticCall(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function refactor(Node $node): ?Node

$methodName = $this->getName($classMethod);
foreach ($this->addParamTypeDeclarations as $addParamTypeDeclaration) {
if (!$this->nodeNameResolver->isStringName($methodName, $addParamTypeDeclaration->getMethodName())) {
if (! $this->nodeNameResolver->isStringName($methodName, $addParamTypeDeclaration->getMethodName())) {
continue;
}

Expand Down

0 comments on commit 9ec7c18

Please sign in to comment.