Skip to content

Commit

Permalink
[Naming] Fix dotted name on generics bug in RenameVariableToMatchMeth…
Browse files Browse the repository at this point in the history
…odCallReturnTypeRector (#5250)
  • Loading branch information
TomasVotruba committed Nov 15, 2023
1 parent 7ec1be6 commit 5647e1b
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 54 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\Naming\Rector\Assign\RenameVariableToMatchMethodCallReturnTypeRector\Fixture;

use Symfony\Component\DependencyInjection\Container;

class SkipDotName
{
public function run()
{
$someType = $this->getService('some.type');
}

/**
* @template TService as object
*
* @param class-string<TService> $type
* @return TService
*/
public function getService(string $type): object
{
/** @var Container $container */
$container = self::$container;

return $container->get($type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Function_;
use PHPStan\Analyser\Scope;
use Rector\Core\NodeAnalyzer\VariableAnalyzer;
use Rector\Core\Php\ReservedKeywordAnalyzer;
Expand Down Expand Up @@ -69,13 +70,13 @@ public function run()
*/
public function getNodeTypes(): array
{
return [ClassMethod::class, Stmt\Function_::class];
return [ClassMethod::class, Function_::class];
}

/**
* @param ClassMethod|Stmt\Function_ $node
* @param ClassMethod|Function_ $node
*/
public function refactorWithScope(Node $node, Scope $scope): null|ClassMethod|Stmt\Function_
public function refactorWithScope(Node $node, Scope $scope): null|ClassMethod|Function_
{
$stmts = $node->stmts;
if ($stmts === null || $stmts === []) {
Expand Down Expand Up @@ -144,9 +145,9 @@ private function hasCallLikeInAssignExpr(Expr $expr, Scope $scope): bool
}

private function isVariableUsedInFollowingStmts(
ClassMethod|Stmt\Function_ $functionLike,
int $assignStmtPosition,
string $variableName
ClassMethod|Function_ $functionLike,
int $assignStmtPosition,
string $variableName
): bool {
if ($functionLike->stmts === null) {
return false;
Expand All @@ -172,7 +173,7 @@ private function isVariableUsedInFollowingStmts(
return false;
}

private function containsCompactFuncCall(ClassMethod|Stmt\Function_ $functionLike): bool
private function containsCompactFuncCall(ClassMethod|Function_ $functionLike): bool
{
$compactFuncCall = $this->betterNodeFinder->findFirst($functionLike, function (Node $node): bool {
if (! $node instanceof FuncCall) {
Expand All @@ -185,7 +186,7 @@ private function containsCompactFuncCall(ClassMethod|Stmt\Function_ $functionLik
return $compactFuncCall instanceof FuncCall;
}

private function containsFileIncludes(ClassMethod|Stmt\Function_ $functionLike): bool
private function containsFileIncludes(ClassMethod|Function_ $functionLike): bool
{
return (bool) $this->betterNodeFinder->findInstancesOf($functionLike, [Include_::class]);
}
Expand Down
8 changes: 1 addition & 7 deletions rules/Naming/Naming/ExpectedNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use PhpParser\Node\Param;
use PhpParser\Node\UnionType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Rector\Naming\ExpectedNameResolver\MatchParamTypeExpectedNameResolver;
Expand Down Expand Up @@ -110,12 +109,7 @@ public function resolveForCall(MethodCall | StaticCall | FuncCall $expr): ?strin
}

$returnedType = $this->nodeTypeResolver->getType($expr);

if ($returnedType instanceof ArrayType) {
return null;
}

if ($returnedType instanceof MixedType) {
if (! $returnedType->isObject()->yes()) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ final class RenameVariableToMatchMethodCallReturnTypeRector extends AbstractRect
*/
private const OR_BETWEEN_WORDS_REGEX = '#[a-z]Or[A-Z]#';

/**
* @var string
* @see https://regex101.com/r/TV8YXZ/1
*/
private const VALID_VARIABLE_NAME_REGEX = '#^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$#';

public function __construct(
private readonly BreakingVariableRenameGuard $breakingVariableRenameGuard,
private readonly ExpectedNameResolver $expectedNameResolver,
Expand Down Expand Up @@ -143,6 +149,10 @@ public function refactor(Node $node): ?Node

private function shouldSkip(VariableAndCallAssign $variableAndCallAssign, string $expectedName): bool
{
if (Strings::match($expectedName, self::VALID_VARIABLE_NAME_REGEX) === null) {
return true;
}

if ($this->namingConventionAnalyzer->isCallMatchingVariableName(
$variableAndCallAssign->getCall(),
$variableAndCallAssign->getVariableName(),
Expand Down

0 comments on commit 5647e1b

Please sign in to comment.