-
-
Notifications
You must be signed in to change notification settings - Fork 699
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CodeQuality] Add NarrowUnionTypeDocRector (#6262)
Co-authored-by: kaizen-ci <[email protected]>
- Loading branch information
1 parent
d43e40a
commit e615756
Showing
8 changed files
with
315 additions
and
33 deletions.
There are no files selected for viewing
This file contains 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
29 changes: 29 additions & 0 deletions
29
rules-tests/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/Fixture/fixture.php.inc
This file contains 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,29 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\NarrowUnionTypeDocRector\Fixture; | ||
|
||
class Fixture { | ||
/** | ||
* @param object|DateTime $message | ||
*/ | ||
public function getMessage(object $message) | ||
{ | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\NarrowUnionTypeDocRector\Fixture; | ||
|
||
class Fixture { | ||
/** | ||
* @param DateTime $message | ||
*/ | ||
public function getMessage(object $message) | ||
{ | ||
} | ||
} | ||
|
||
?> |
29 changes: 29 additions & 0 deletions
29
...ests/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/Fixture/param_scalar.php.inc
This file contains 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,29 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\NarrowUnionTypeDocRector\Fixture; | ||
|
||
class ParamScalar { | ||
/** | ||
* @param int|string|bool|float $value | ||
*/ | ||
public function getMessage($value) | ||
{ | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\NarrowUnionTypeDocRector\Fixture; | ||
|
||
class ParamScalar { | ||
/** | ||
* @param scalar $value | ||
*/ | ||
public function getMessage($value) | ||
{ | ||
} | ||
} | ||
|
||
?> |
15 changes: 15 additions & 0 deletions
15
...ts/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/Fixture/skip_non_union.php.inc
This file contains 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,15 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\NarrowUnionTypeDocRector\Fixture; | ||
|
||
class SkipNonUnion { | ||
/** | ||
* @param DateTime $message | ||
*/ | ||
public function getMessage(object $message) | ||
{ | ||
} | ||
} | ||
|
||
?> | ||
|
33 changes: 33 additions & 0 deletions
33
.../CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/NarrowUnionTypeDocRectorTest.php
This file contains 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,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\NarrowUnionTypeDocRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class NarrowUnionTypeDocRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(SmartFileInfo $fileInfo): void | ||
{ | ||
$this->doTestFileInfo($fileInfo); | ||
} | ||
|
||
/** | ||
* @return Iterator<SmartFileInfo> | ||
*/ | ||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...-tests/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector/config/configured_rule.php
This file contains 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 | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\CodeQuality\Rector\ClassMethod\NarrowUnionTypeDocRector; | ||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
|
||
return static function (ContainerConfigurator $containerConfigurator): void { | ||
$services = $containerConfigurator->services(); | ||
$services->set(NarrowUnionTypeDocRector::class); | ||
}; |
131 changes: 131 additions & 0 deletions
131
rules/CodeQuality/Rector/ClassMethod/NarrowUnionTypeDocRector.php
This file contains 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,131 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\CodeQuality\Rector\ClassMethod; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; | ||
use PHPStan\Type\UnionType; | ||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\PHPStanStaticTypeMapper\TypeAnalyzer\UnionTypeAnalyzer; | ||
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\CodeQuality\Rector\ClassMethod\NarrowUnionTypeDocRector\NarrowUnionTypeDocRectorTest | ||
*/ | ||
final class NarrowUnionTypeDocRector extends AbstractRector | ||
{ | ||
/** | ||
* @var UnionTypeAnalyzer | ||
*/ | ||
private $unionTypeAnalyzer; | ||
|
||
public function __construct(UnionTypeAnalyzer $unionTypeAnalyzer) | ||
{ | ||
$this->unionTypeAnalyzer = $unionTypeAnalyzer; | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Changes docblock by narrowing type', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
class SomeClass { | ||
/** | ||
* @param object|DateTime $message | ||
*/ | ||
public function getMessage(object $message) | ||
{ | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
class SomeClass { | ||
/** | ||
* @param DateTime $message | ||
*/ | ||
public function getMessage(object $message) | ||
{ | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [ClassMethod::class]; | ||
} | ||
|
||
/** | ||
* @param ClassMethod $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); | ||
$params = $node->getParams(); | ||
|
||
foreach ($params as $key => $param) { | ||
/** @var string $paramName */ | ||
$paramName = $this->getName($param->var); | ||
$paramType = $phpDocInfo->getParamType($paramName); | ||
|
||
if (! $paramType instanceof UnionType) { | ||
continue; | ||
} | ||
|
||
if ($this->unionTypeAnalyzer->isScalar($paramType)) { | ||
$this->changeDocObjectScalar($key, $phpDocInfo); | ||
continue; | ||
} | ||
|
||
if ($this->unionTypeAnalyzer->hasObjectWithoutClassType($paramType)) { | ||
$this->changeDocObjectWithoutClassType($paramType, $key, $phpDocInfo); | ||
continue; | ||
} | ||
} | ||
|
||
if ($phpDocInfo->hasChanged()) { | ||
return $node; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private function changeDocObjectWithoutClassType( | ||
UnionType $unionType, | ||
int $key, | ||
PhpDocInfo $phpDocInfo | ||
): void { | ||
if (! $this->unionTypeAnalyzer->hasObjectWithoutClassTypeWithOnlyFullyQualifiedObjectType($unionType)) { | ||
return; | ||
} | ||
|
||
$types = $unionType->getTypes(); | ||
$resultType = ''; | ||
foreach ($types as $type) { | ||
if ($type instanceof FullyQualifiedObjectType) { | ||
$resultType .= $type->getClassName() . '|'; | ||
} | ||
} | ||
|
||
$resultType = rtrim($resultType, '|'); | ||
$paramTagValueNodes = $phpDocInfo->getParamTagValueNodes(); | ||
$paramTagValueNodes[$key]->type = new IdentifierTypeNode($resultType); | ||
} | ||
|
||
private function changeDocObjectScalar(int $key, PhpDocInfo $phpDocInfo): void { | ||
$paramTagValueNodes = $phpDocInfo->getParamTagValueNodes(); | ||
$paramTagValueNodes[$key]->type = new IdentifierTypeNode('scalar'); | ||
} | ||
} |
Oops, something went wrong.