-
-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TypeDeclaration] Add AddParamTypeSplFixedArrayRector (#3105)
- Loading branch information
1 parent
48febdb
commit 10c76b3
Showing
9 changed files
with
242 additions
and
97 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
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
32 changes: 0 additions & 32 deletions
32
...laration/Rector/ClassMethod/AddArrayParamDocTypeRector/Fixture/token_php_cs_fixer.php.inc
This file was deleted.
Oops, something went wrong.
32 changes: 0 additions & 32 deletions
32
...Rector/ClassMethod/AddArrayParamDocTypeRector/Fixture/token_php_cs_fixer_doctrine.php.inc
This file was deleted.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
...ctor/FunctionLike/AddParamTypeSplFixedArrayRector/AddParamTypeSplFixedArrayRectorTest.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,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddParamTypeSplFixedArrayRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class AddParamTypeSplFixedArrayRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
/** | ||
* @return Iterator<array<string>> | ||
*/ | ||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...laration/Rector/FunctionLike/AddParamTypeSplFixedArrayRector/Fixture/some_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,32 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddParamTypeSplFixedArrayRector\Fixture; | ||
|
||
use PhpCsFixer\Tokenizer\Tokens; | ||
|
||
final class SomeFixture | ||
{ | ||
public function someFunction(Tokens $tokens) | ||
{ | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddParamTypeSplFixedArrayRector\Fixture; | ||
|
||
use PhpCsFixer\Tokenizer\Tokens; | ||
|
||
final class SomeFixture | ||
{ | ||
/** | ||
* @param \PhpCsFixer\Tokenizer\Tokens<\PhpCsFixer\Tokenizer\Token> $tokens | ||
*/ | ||
public function someFunction(Tokens $tokens) | ||
{ | ||
} | ||
} | ||
|
||
?> |
10 changes: 10 additions & 0 deletions
10
...eclaration/Rector/FunctionLike/AddParamTypeSplFixedArrayRector/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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\TypeDeclaration\Rector\FunctionLike\AddParamTypeSplFixedArrayRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(AddParamTypeSplFixedArrayRector::class); | ||
}; |
138 changes: 138 additions & 0 deletions
138
rules/TypeDeclaration/Rector/FunctionLike/AddParamTypeSplFixedArrayRector.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,138 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\TypeDeclaration\Rector\FunctionLike; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\FunctionLike; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PhpParser\Node\Stmt\Function_; | ||
use PHPStan\Type\Generic\GenericObjectType; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeWithClassName; | ||
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddParamTypeSplFixedArrayRector\AddParamTypeSplFixedArrayRectorTest | ||
*/ | ||
final class AddParamTypeSplFixedArrayRector extends AbstractRector | ||
{ | ||
/** | ||
* @var array<string, string> | ||
*/ | ||
private const SPL_FIXED_ARRAY_TO_SINGLE = [ | ||
'PhpCsFixer\Tokenizer\Tokens' => 'PhpCsFixer\Tokenizer\Token', | ||
'PhpCsFixer\Doctrine\Annotation\Tokens' => 'PhpCsFixer\Doctrine\Annotation\Token', | ||
]; | ||
|
||
public function __construct( | ||
private readonly PhpDocTypeChanger $phpDocTypeChanger | ||
) { | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [Function_::class, ClassMethod::class]; | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Add exact fixed array type in known cases', | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
use PhpCsFixer\Tokenizer\Tokens; | ||
class SomeClass | ||
{ | ||
public function run(Tokens $tokens) | ||
{ | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
use PhpCsFixer\Tokenizer\Token; | ||
use PhpCsFixer\Tokenizer\Tokens; | ||
class SomeClass | ||
{ | ||
/** | ||
* @param Tokens<Token> | ||
*/ | ||
public function run(Tokens $tokens) | ||
{ | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* @param FunctionLike $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if ($node->getParams() === []) { | ||
return null; | ||
} | ||
|
||
$functionLikePhpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); | ||
|
||
foreach ($node->getParams() as $param) { | ||
if ($param->type === null) { | ||
continue; | ||
} | ||
|
||
$paramType = $this->nodeTypeResolver->getType($param->type); | ||
if ($paramType->isSuperTypeOf(new ObjectType('SplFixedArray'))->no()) { | ||
continue; | ||
} | ||
|
||
if (! $paramType instanceof TypeWithClassName) { | ||
continue; | ||
} | ||
|
||
if ($paramType instanceof GenericObjectType) { | ||
continue; | ||
} | ||
|
||
$genericParamType = $this->resolveGenericType($paramType); | ||
if (! $genericParamType instanceof Type) { | ||
continue; | ||
} | ||
|
||
$paramName = $this->getName($param); | ||
$this->phpDocTypeChanger->changeParamType($functionLikePhpDocInfo, $genericParamType, $param, $paramName); | ||
} | ||
|
||
if ($functionLikePhpDocInfo->hasChanged()) { | ||
return $node; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private function resolveGenericType(TypeWithClassName $typeWithClassName): GenericObjectType|null | ||
{ | ||
foreach (self::SPL_FIXED_ARRAY_TO_SINGLE as $fixedArrayClass => $singleClass) { | ||
if ($typeWithClassName->getClassName() === $fixedArrayClass) { | ||
$genericObjectType = new ObjectType($singleClass); | ||
return new GenericObjectType($typeWithClassName->getClassName(), [$genericObjectType]); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
31 changes: 0 additions & 31 deletions
31
rules/TypeDeclaration/TypeInferer/ParamTypeInferer/SplFixedArrayParamTypeInferer.php
This file was deleted.
Oops, something went wrong.