-
-
Notifications
You must be signed in to change notification settings - Fork 441
[code-quality] Add FixClassCaseSensitivityVarDocblockRector #8046
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
...FixClassCaseSensitivityVarDocblockRector/FixClassCaseSensitivityVarDocblockRectorTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Tests\CodeQuality\Rector\Property\FixClassCaseSensitivityVarDocblockRector; | ||
|
|
||
| use Iterator; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
|
||
| final class FixClassCaseSensitivityVarDocblockRectorTest extends AbstractRectorTestCase | ||
| { | ||
| #[DataProvider('provideData')] | ||
| public function test(string $filePath): void | ||
| { | ||
| $this->doTestFile($filePath); | ||
| } | ||
|
|
||
| public static function provideData(): Iterator | ||
| { | ||
| return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
| } | ||
|
|
||
| public function provideConfigFilePath(): string | ||
| { | ||
| return __DIR__ . '/config/configured_rule.php'; | ||
| } | ||
| } |
37 changes: 37 additions & 0 deletions
37
...ality/Rector/Property/FixClassCaseSensitivityVarDocblockRector/Fixture/inline_var.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\CodeQuality\Rector\Property\FixClassCaseSensitivityVarDocblockRector\Fixture; | ||
|
|
||
| use Rector\Tests\CodeQuality\Rector\Property\FixClassCaseSensitivityVarDocblockRector\Source\AutoMailingService; | ||
|
|
||
| final class InlineVar | ||
| { | ||
| public function run() | ||
| { | ||
| /** @var AutomailingService $service */ | ||
| $service = $this->getService(); | ||
|
|
||
| return $service; | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\CodeQuality\Rector\Property\FixClassCaseSensitivityVarDocblockRector\Fixture; | ||
|
|
||
| use Rector\Tests\CodeQuality\Rector\Property\FixClassCaseSensitivityVarDocblockRector\Source\AutoMailingService; | ||
|
|
||
| final class InlineVar | ||
| { | ||
| public function run() | ||
| { | ||
| /** @var AutoMailingService $service */ | ||
| $service = $this->getService(); | ||
|
|
||
| return $service; | ||
| } | ||
| } | ||
|
|
||
| ?> |
31 changes: 31 additions & 0 deletions
31
...ity/Rector/Property/FixClassCaseSensitivityVarDocblockRector/Fixture/property_var.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\CodeQuality\Rector\Property\FixClassCaseSensitivityVarDocblockRector\Fixture; | ||
|
|
||
| use Rector\Tests\CodeQuality\Rector\Property\FixClassCaseSensitivityVarDocblockRector\Source\AutoMailingService; | ||
|
|
||
| final class PropertyVar | ||
| { | ||
| /** | ||
| * @var AUTOMAILINGSERVICE | ||
| */ | ||
| private $service; | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\CodeQuality\Rector\Property\FixClassCaseSensitivityVarDocblockRector\Fixture; | ||
|
|
||
| use Rector\Tests\CodeQuality\Rector\Property\FixClassCaseSensitivityVarDocblockRector\Source\AutoMailingService; | ||
|
|
||
| final class PropertyVar | ||
| { | ||
| /** | ||
| * @var AutoMailingService | ||
| */ | ||
| private $service; | ||
| } | ||
|
|
||
| ?> |
26 changes: 26 additions & 0 deletions
26
...Property/FixClassCaseSensitivityVarDocblockRector/Fixture/skip_correct_and_scalar.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\CodeQuality\Rector\Property\FixClassCaseSensitivityVarDocblockRector\Fixture; | ||
|
|
||
| use Rector\Tests\CodeQuality\Rector\Property\FixClassCaseSensitivityVarDocblockRector\Source\AutoMailingService; | ||
|
|
||
| final class SkipCorrectAndScalar | ||
| { | ||
| /** | ||
| * @var AutoMailingService | ||
| */ | ||
| private $service; | ||
|
|
||
| /** | ||
| * @var string | ||
| */ | ||
| private $name; | ||
|
|
||
| public function run() | ||
| { | ||
| /** @var self $self */ | ||
| $self = $this; | ||
|
|
||
| return $self; | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
...ctor/Property/FixClassCaseSensitivityVarDocblockRector/Fixture/skip_unknown_class.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\CodeQuality\Rector\Property\FixClassCaseSensitivityVarDocblockRector\Fixture; | ||
|
|
||
| final class SkipUnknownClass | ||
| { | ||
| /** | ||
| * @var SomeNonExistingService | ||
| */ | ||
| private $service; | ||
| } |
9 changes: 9 additions & 0 deletions
9
...ty/Rector/Property/FixClassCaseSensitivityVarDocblockRector/Source/AutoMailingService.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Tests\CodeQuality\Rector\Property\FixClassCaseSensitivityVarDocblockRector\Source; | ||
|
|
||
| final class AutoMailingService | ||
| { | ||
| } |
9 changes: 9 additions & 0 deletions
9
...ality/Rector/Property/FixClassCaseSensitivityVarDocblockRector/config/configured_rule.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Rector\CodeQuality\Rector\Property\FixClassCaseSensitivityVarDocblockRector; | ||
| use Rector\Config\RectorConfig; | ||
|
|
||
| return RectorConfig::configure() | ||
| ->withRules([FixClassCaseSensitivityVarDocblockRector::class]); |
180 changes: 180 additions & 0 deletions
180
rules/CodeQuality/Rector/Property/FixClassCaseSensitivityVarDocblockRector.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\CodeQuality\Rector\Property; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Identifier; | ||
| use PhpParser\Node\Stmt\Expression; | ||
| use PhpParser\Node\Stmt\Property; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; | ||
| use PHPStan\PhpDocParser\Ast\Type\TypeNode; | ||
| use PHPStan\Reflection\ReflectionProvider; | ||
| use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; | ||
| use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; | ||
| use Rector\Comments\NodeDocBlock\DocBlockUpdater; | ||
| use Rector\Naming\Naming\UseImportsResolver; | ||
| use Rector\NodeTypeResolver\Node\AttributeKey; | ||
| use Rector\Rector\AbstractRector; | ||
| use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
| use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
|
||
| /** | ||
| * @see \Rector\Tests\CodeQuality\Rector\Property\FixClassCaseSensitivityVarDocblockRector\FixClassCaseSensitivityVarDocblockRectorTest | ||
| */ | ||
| final class FixClassCaseSensitivityVarDocblockRector extends AbstractRector | ||
| { | ||
| public function __construct( | ||
| private readonly PhpDocInfoFactory $phpDocInfoFactory, | ||
| private readonly DocBlockUpdater $docBlockUpdater, | ||
| private readonly ReflectionProvider $reflectionProvider, | ||
| private readonly UseImportsResolver $useImportsResolver, | ||
| ) { | ||
| } | ||
|
|
||
| public function getRuleDefinition(): RuleDefinition | ||
| { | ||
| return new RuleDefinition( | ||
| 'Fix a misspelled class name casing in a @var docblock to match the real class name', | ||
| [ | ||
| new CodeSample( | ||
| <<<'CODE_SAMPLE' | ||
| /** @var AutomailingService */ | ||
| $service = $this->getService(); | ||
| CODE_SAMPLE | ||
| , | ||
| <<<'CODE_SAMPLE' | ||
| /** @var AutoMailingService */ | ||
| $service = $this->getService(); | ||
| CODE_SAMPLE | ||
| ), | ||
| ] | ||
| ); | ||
| } | ||
|
|
||
| public function getNodeTypes(): array | ||
| { | ||
| return [Property::class, Expression::class]; | ||
| } | ||
|
|
||
| /** | ||
| * @param Property|Expression $node | ||
| */ | ||
| public function refactor(Node $node): ?Node | ||
| { | ||
| $phpDocInfo = $this->phpDocInfoFactory->createFromNode($node); | ||
| if (! $phpDocInfo instanceof PhpDocInfo) { | ||
| return null; | ||
| } | ||
|
|
||
| $hasChanged = false; | ||
| foreach ($phpDocInfo->getPhpDocNode()->getVarTagValues() as $varTagValueNode) { | ||
| $correctedTypeNode = $this->correctClassNameCasing($varTagValueNode->type, $node); | ||
| if ($correctedTypeNode instanceof IdentifierTypeNode) { | ||
| $varTagValueNode->type = $correctedTypeNode; | ||
| $hasChanged = true; | ||
| } | ||
| } | ||
|
|
||
| if (! $hasChanged) { | ||
| return null; | ||
| } | ||
|
|
||
| $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node); | ||
|
|
||
| return $node; | ||
| } | ||
|
|
||
| private function correctClassNameCasing(TypeNode $typeNode, Node $node): ?IdentifierTypeNode | ||
| { | ||
| if (! $typeNode instanceof IdentifierTypeNode) { | ||
| return null; | ||
| } | ||
|
|
||
| $writtenName = $typeNode->name; | ||
| $existingClassName = $this->resolveExistingClassName($writtenName, $node); | ||
| if ($existingClassName === null) { | ||
| return null; | ||
| } | ||
|
|
||
| $realClassName = $this->reflectionProvider->getClass($existingClassName) | ||
| ->getName(); | ||
|
|
||
| $hasLeadingSlash = str_starts_with($writtenName, '\\'); | ||
|
|
||
| $writtenParts = explode('\\', ltrim($writtenName, '\\')); | ||
| $realParts = array_slice(explode('\\', $realClassName), -count($writtenParts)); | ||
|
|
||
| // not a pure casing difference, e.g. an alias or partial name | ||
| if (strtolower(implode('\\', $writtenParts)) !== strtolower(implode('\\', $realParts))) { | ||
| return null; | ||
| } | ||
|
|
||
| $correctedName = ($hasLeadingSlash ? '\\' : '') . implode('\\', $realParts); | ||
| if ($correctedName === $writtenName) { | ||
| return null; | ||
| } | ||
|
|
||
| return new IdentifierTypeNode($correctedName); | ||
| } | ||
|
|
||
| /** | ||
| * Resolve the existing class name for a written, possibly miss-cased, identifier. | ||
| * Lookups in PHPStan reflection are case-insensitive, so the namespace must match, | ||
| * while the class name casing may differ. | ||
| */ | ||
| private function resolveExistingClassName(string $writtenName, Node $node): ?string | ||
| { | ||
| $bareName = ltrim($writtenName, '\\'); | ||
|
|
||
| // already fully qualified | ||
| if (str_starts_with($writtenName, '\\')) { | ||
| return $this->reflectionProvider->hasClass($bareName) ? $bareName : null; | ||
| } | ||
|
|
||
| $parts = explode('\\', $bareName); | ||
| $firstPart = $parts[0]; | ||
|
|
||
| // resolve via use imports, matching the imported short name case-insensitively | ||
| foreach ($this->useImportsResolver->resolve() as $use) { | ||
| $prefix = $this->useImportsResolver->resolvePrefix($use); | ||
|
|
||
| foreach ($use->uses as $useItem) { | ||
| if ($useItem->alias instanceof Identifier) { | ||
| continue; | ||
| } | ||
|
|
||
| if (strtolower($useItem->name->getLast()) !== strtolower($firstPart)) { | ||
| continue; | ||
| } | ||
|
|
||
| $candidate = $prefix . $useItem->name->toString(); | ||
| if (count($parts) > 1) { | ||
| $candidate .= '\\' . implode('\\', array_slice($parts, 1)); | ||
| } | ||
|
|
||
| if ($this->reflectionProvider->hasClass($candidate)) { | ||
| return $candidate; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // same namespace | ||
| $scope = $node->getAttribute(AttributeKey::SCOPE); | ||
| if ($scope instanceof Scope) { | ||
| $namespace = $scope->getNamespace(); | ||
| if ($namespace !== null && $this->reflectionProvider->hasClass($namespace . '\\' . $bareName)) { | ||
| return $namespace . '\\' . $bareName; | ||
| } | ||
| } | ||
|
|
||
| // global namespace | ||
| if ($this->reflectionProvider->hasClass($bareName)) { | ||
| return $bareName; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.