Skip to content

Commit

Permalink
[TypeDeclaration] Add AddParamTypeSplFixedArrayRector (#3105)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Nov 27, 2022
1 parent 48febdb commit 10c76b3
Show file tree
Hide file tree
Showing 9 changed files with 242 additions and 97 deletions.
27 changes: 25 additions & 2 deletions build/target-repository/docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 403 Rules Overview
# 404 Rules Overview

<br>

Expand Down Expand Up @@ -64,7 +64,7 @@

- [Transform](#transform) (34)

- [TypeDeclaration](#typedeclaration) (31)
- [TypeDeclaration](#typedeclaration) (32)

- [Visibility](#visibility) (3)

Expand Down Expand Up @@ -8869,6 +8869,29 @@ return static function (RectorConfig $rectorConfig): void {

<br>

### AddParamTypeSplFixedArrayRector

Add exact fixed array type in known cases

- class: [`Rector\TypeDeclaration\Rector\FunctionLike\AddParamTypeSplFixedArrayRector`](../rules/TypeDeclaration/Rector/FunctionLike/AddParamTypeSplFixedArrayRector.php)

```diff
+use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;

class SomeClass
{
+ /**
+ * @param Tokens<Token>
+ */
public function run(Tokens $tokens)
{
}
}
```

<br>

### AddPropertyTypeDeclarationRector

Add type to property by added rules, mostly public/property by parent type
Expand Down
5 changes: 5 additions & 0 deletions config/set/type-declaration.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Rector\TypeDeclaration\Rector\ClassMethod\AddArrayParamDocTypeRector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddArrayReturnDocTypeRector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeBasedOnPHPUnitDataProviderRector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationBasedOnParentClassMethodRector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ArrayShapeFromConstantArrayReturnRector;
Expand All @@ -21,6 +22,7 @@
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedPropertyRector;
use Rector\TypeDeclaration\Rector\Closure\AddClosureReturnTypeRector;
use Rector\TypeDeclaration\Rector\FunctionLike\AddParamTypeSplFixedArrayRector;
use Rector\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector;
use Rector\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector;
use Rector\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector;
Expand Down Expand Up @@ -60,5 +62,8 @@
ReturnTypeFromStrictScalarReturnExprRector::class,
TypedPropertyFromStrictSetUpRector::class,
ParamTypeByParentCallTypeRector::class,
// @todo enable in next PR
// AddParamTypeBasedOnPHPUnitDataProviderRector::class,
// AddParamTypeSplFixedArrayRector::class,
]);
};

This file was deleted.

This file was deleted.

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';
}
}
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)
{
}
}

?>
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);
};
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;
}
}

This file was deleted.

0 comments on commit 10c76b3

Please sign in to comment.