Skip to content

Commit

Permalink
[CodeQuality] Refactor FixClassCaseSensitivityNameRector (#1093)
Browse files Browse the repository at this point in the history
* [CodeQuality] Refactor FixClassCaseSensitivityNameRector

* debug

* Fixed 🎉

* skip union param type

* verify

* clean up

* tweak

* eol

* phpstan

* [ci-review] Rector Rectify

* clean up

* clean up

* [ci-review] Rector Rectify

* fixture skip correct class const fetch

* [ci-review] Rector Rectify

* rename fixture

* handle union param type

* fixture fix

* phpstan

* rectify

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Oct 29, 2021
1 parent 87f6d1b commit 8895e1b
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ final class FixClassCaseSensitivityNameRectorTest extends AbstractRectorTestCase
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->markTestSkipped('Broken in PHPStan 1.0');

$this->doTestFileInfo($fileInfo);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\Name\FixClassCaseSensitivityNameRector\Fixture;

use Rector\Tests\CodeQuality\Rector\Name\FixClassCaseSensitivityNameRector\Source\MissCaseTypedClass;

final class SkipClassConstFetchCorrect
{
public function run()
{
$fatherSon = MissCaseTypedClass::SOME_CONST;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\Name\FixClassCaseSensitivityNameRector\Fixture;

use Rector\Tests\CodeQuality\Rector\Name\FixClassCaseSensitivityNameRector\Source\MissCaseTypedClass;

final class SkipParamTypeCorrect
{
public function run(MissCaseTypedClass $misscasetypedclass)
{
return $misscasetypedclass;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\Name\FixClassCaseSensitivityNameRector\Fixture;

use Rector\Tests\CodeQuality\Rector\Name\FixClassCaseSensitivityNameRector\Source\MissCaseTypedClass;

final class SkipParamWithoutType
{
public function run($misscasetypedclass)
{
return $misscasetypedclass;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\Name\FixClassCaseSensitivityNameRector\Fixture;

use Rector\Tests\CodeQuality\Rector\Name\FixClassCaseSensitivityNameRector\Source\MissCaseTypedClass;

final class UnionParamType
{
public function run(string|misscasetypedclass $misscasetypedclass)
{
return $misscasetypedclass;
}
}

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\Name\FixClassCaseSensitivityNameRector\Fixture;

use Rector\Tests\CodeQuality\Rector\Name\FixClassCaseSensitivityNameRector\Source\MissCaseTypedClass;

final class UnionParamType
{
public function run(string|\Rector\Tests\CodeQuality\Rector\Name\FixClassCaseSensitivityNameRector\Source\MissCaseTypedClass $misscasetypedclass)
{
return $misscasetypedclass;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\UseUse;
use PhpParser\Node\UnionType;
use PHPStan\Reflection\ReflectionProvider;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand Down Expand Up @@ -121,16 +122,23 @@ public function refactor(Node $node): ?Node
private function resolveFullyQualifiedName(Name $name): string
{
$parent = $name->getAttribute(AttributeKey::PARENT_NODE);
// for some reason, Param gets already corrected name
if (! $parent instanceof Param && ! $parent instanceof ClassConstFetch) {
return $this->getName($name);
if (! $parent instanceof Node) {
return '';
}

$originalName = $name->getAttribute(AttributeKey::ORIGINAL_NAME);
if (! $originalName instanceof Name) {
return $this->getName($name);
}

if ($this->isParamTypeNameOrClassConstFetchClassName($parent)) {
return $this->processParamTypeNameOrClassConstFetchClassName($name, $originalName);
}

if ($parent instanceof UnionType) {
return $this->processUnionType($parent, $name, $originalName);
}

// replace parts from the old one
$originalReversedParts = array_reverse($originalName->parts);
$resolvedReversedParts = array_reverse($name->parts);
Expand All @@ -140,4 +148,57 @@ private function resolveFullyQualifiedName(Name $name): string

return implode('\\', $mergedParts);
}

private function isParamTypeNameOrClassConstFetchClassName(Node $node): bool
{
if (! $node instanceof Param && ! $node instanceof ClassConstFetch) {
return false;
}

if ($node instanceof Param) {
return $node->type instanceof Name;
}

return $node->class instanceof Name;
}

private function processUnionType(UnionType $unionType, Name $name, Name $originalName): string
{
foreach ($unionType->types as $type) {
if (! $type instanceof Name) {
continue;
}

if ($type !== $name) {
continue;
}

return $this->processParamTypeNameOrClassConstFetchClassName($name, $originalName);
}

return '';
}

private function processParamTypeNameOrClassConstFetchClassName(Name $name, Name $originalName): string
{
$oldTokens = $this->file->getOldTokens();
$startTokenPos = $name->getStartTokenPos();

if (! isset($oldTokens[$startTokenPos][1])) {
return '';
}

$type = $oldTokens[$startTokenPos][1];
if (str_contains($type, '\\')) {
return '';
}

$last = $originalName->getLast();
if (strtolower($last) !== strtolower($type)) {
return '';
}

$name->parts[count($name->parts) - 1] = $type;
return (string) $name;
}
}

0 comments on commit 8895e1b

Please sign in to comment.