Updated Rector to commit 0c0e5eeb4fd337d98f27224d0b46bfd310405dfd

0c0e5eeb4f AddParamTypeBasedOnPHPUnitDataProviderRector: Enhance existing rule to handle PHPUnit 10+ DataProvider Attribute (#4925)
This commit is contained in:
Tomas Votruba 2023-09-30 09:01:28 +00:00
parent a8fc4e6c01
commit d7242c6a67
8 changed files with 93 additions and 33 deletions

View File

@ -6230,7 +6230,7 @@ Adds param type declaration based on PHPUnit provider return type declaration
- class: [`Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeBasedOnPHPUnitDataProviderRector`](../rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeBasedOnPHPUnitDataProviderRector.php) - class: [`Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeBasedOnPHPUnitDataProviderRector`](../rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeBasedOnPHPUnitDataProviderRector.php)
```diff ```diff
use PHPUnit\Framework\TestCase use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase final class SomeTest extends TestCase
{ {

View File

@ -5,10 +5,13 @@ namespace Rector\TypeDeclaration\Rector\ClassMethod;
use RectorPrefix202309\Nette\Utils\Strings; use RectorPrefix202309\Nette\Utils\Strings;
use PhpParser\Node; use PhpParser\Node;
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Expr\Array_; use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem; use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\Yield_; use PhpParser\Node\Expr\Yield_;
use PhpParser\Node\Param; use PhpParser\Node\Param;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Return_; use PhpParser\Node\Stmt\Return_;
@ -28,6 +31,7 @@ use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\StaticTypeMapper\StaticTypeMapper; use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\TypeDeclaration\ValueObject\DataProviderNodes;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/** /**
@ -80,7 +84,7 @@ final class AddParamTypeBasedOnPHPUnitDataProviderRector extends AbstractRector
public function getRuleDefinition() : RuleDefinition public function getRuleDefinition() : RuleDefinition
{ {
return new RuleDefinition(self::ERROR_MESSAGE, [new CodeSample(<<<'CODE_SAMPLE' return new RuleDefinition(self::ERROR_MESSAGE, [new CodeSample(<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase final class SomeTest extends TestCase
{ {
@ -98,7 +102,7 @@ final class SomeTest extends TestCase
} }
CODE_SAMPLE CODE_SAMPLE
, <<<'CODE_SAMPLE' , <<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase final class SomeTest extends TestCase
{ {
@ -140,11 +144,11 @@ CODE_SAMPLE
if ($classMethod->getParams() === []) { if ($classMethod->getParams() === []) {
continue; continue;
} }
$dataProviderPhpDocTagNodes = $this->resolveDataProviderPhpDocTagNode($classMethod); $dataProviderNodes = $this->resolveDataProviderNodes($classMethod);
if ($dataProviderPhpDocTagNodes === []) { if ($dataProviderNodes->isEmpty()) {
return null; return null;
} }
$hasClassMethodChanged = $this->refactorClassMethod($classMethod, $node, $dataProviderPhpDocTagNodes); $hasClassMethodChanged = $this->refactorClassMethod($classMethod, $node, $dataProviderNodes->nodes);
if ($hasClassMethodChanged) { if ($hasClassMethodChanged) {
$hasChanged = \true; $hasChanged = \true;
} }
@ -154,9 +158,12 @@ CODE_SAMPLE
} }
return null; return null;
} }
private function inferParam(Class_ $class, Param $param, PhpDocTagNode $dataProviderPhpDocTagNode) : Type /**
* @param \PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode|\PhpParser\Node\Attribute $dataProviderNode
*/
private function inferParam(Class_ $class, Param $param, $dataProviderNode) : Type
{ {
$dataProviderClassMethod = $this->resolveDataProviderClassMethod($class, $dataProviderPhpDocTagNode); $dataProviderClassMethod = $this->resolveDataProviderClassMethod($class, $dataProviderNode);
if (!$dataProviderClassMethod instanceof ClassMethod) { if (!$dataProviderClassMethod instanceof ClassMethod) {
return new MixedType(); return new MixedType();
} }
@ -173,12 +180,22 @@ CODE_SAMPLE
$yields = $this->betterNodeFinder->findInstanceOf((array) $dataProviderClassMethod->stmts, Yield_::class); $yields = $this->betterNodeFinder->findInstanceOf((array) $dataProviderClassMethod->stmts, Yield_::class);
return $this->resolveYieldStaticArrayTypeByParameterPosition($yields, $parameterPosition); return $this->resolveYieldStaticArrayTypeByParameterPosition($yields, $parameterPosition);
} }
private function resolveDataProviderClassMethod(Class_ $class, PhpDocTagNode $dataProviderPhpDocTagNode) : ?ClassMethod /**
* @param \PhpParser\Node\Attribute|\PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode $dataProviderNode
*/
private function resolveDataProviderClassMethod(Class_ $class, $dataProviderNode) : ?ClassMethod
{ {
if (!$dataProviderPhpDocTagNode->value instanceof GenericTagValueNode) { if ($dataProviderNode instanceof Attribute) {
$value = $dataProviderNode->args[0]->value;
if (!$value instanceof String_) {
return null;
}
$content = $value->value;
} elseif ($dataProviderNode->value instanceof GenericTagValueNode) {
$content = $dataProviderNode->value->value;
} else {
return null; return null;
} }
$content = $dataProviderPhpDocTagNode->value->value;
$match = Strings::match($content, self::METHOD_NAME_REGEX); $match = Strings::match($content, self::METHOD_NAME_REGEX);
if ($match === null) { if ($match === null) {
return null; return null;
@ -261,21 +278,36 @@ CODE_SAMPLE
} }
return $paramOnPositionTypes; return $paramOnPositionTypes;
} }
/** private function resolveDataProviderNodes(ClassMethod $classMethod) : DataProviderNodes
* @return array<PhpDocTagNode>
*/
private function resolveDataProviderPhpDocTagNode(ClassMethod $classMethod) : array
{ {
$attributes = $this->getPhpDataProviderAttributes($classMethod);
$classMethodPhpDocInfo = $this->phpDocInfoFactory->createFromNode($classMethod); $classMethodPhpDocInfo = $this->phpDocInfoFactory->createFromNode($classMethod);
if (!$classMethodPhpDocInfo instanceof PhpDocInfo) { $phpdocNodes = $classMethodPhpDocInfo instanceof PhpDocInfo ? $classMethodPhpDocInfo->getTagsByName('@dataProvider') : [];
return []; return new DataProviderNodes(\array_merge($attributes, $phpdocNodes));
}
return $classMethodPhpDocInfo->getTagsByName('@dataProvider');
} }
/** /**
* @param array<PhpDocTagNode> $dataProviderPhpDocTagNodes * @return array<array-key, Attribute>
*/ */
private function refactorClassMethod(ClassMethod $classMethod, Class_ $class, array $dataProviderPhpDocTagNodes) : bool private function getPhpDataProviderAttributes(ClassMethod $node) : array
{
$attributeName = 'PHPUnit\\Framework\\Attributes\\DataProvider';
/** @var AttributeGroup[] $attrGroups */
$attrGroups = $node->attrGroups;
$dataProviders = [];
foreach ($attrGroups as $attrGroup) {
foreach ($attrGroup->attrs as $attribute) {
if (!$this->nodeNameResolver->isName($attribute->name, $attributeName)) {
continue;
}
$dataProviders[] = $attribute;
}
}
return $dataProviders;
}
/**
* @param array<Attribute|PhpDocTagNode> $dataProviderNodes
*/
private function refactorClassMethod(ClassMethod $classMethod, Class_ $class, array $dataProviderNodes) : bool
{ {
$hasChanged = \false; $hasChanged = \false;
foreach ($classMethod->getParams() as $param) { foreach ($classMethod->getParams() as $param) {
@ -283,8 +315,8 @@ CODE_SAMPLE
continue; continue;
} }
$paramTypes = []; $paramTypes = [];
foreach ($dataProviderPhpDocTagNodes as $dataProviderPhpDocTagNode) { foreach ($dataProviderNodes as $dataProviderNode) {
$paramTypes[] = $this->inferParam($class, $param, $dataProviderPhpDocTagNode); $paramTypes[] = $this->inferParam($class, $param, $dataProviderNode);
} }
$paramTypeDeclaration = TypeCombinator::union(...$paramTypes); $paramTypeDeclaration = TypeCombinator::union(...$paramTypes);
if ($paramTypeDeclaration instanceof MixedType) { if ($paramTypeDeclaration instanceof MixedType) {

View File

@ -0,0 +1,26 @@
<?php
declare (strict_types=1);
namespace Rector\TypeDeclaration\ValueObject;
use PhpParser\Node\Attribute;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
final class DataProviderNodes
{
/**
* @var array<array-key, (Attribute | PhpDocTagNode)>
* @readonly
*/
public $nodes;
/**
* @param array<array-key, Attribute|PhpDocTagNode> $nodes
*/
public function __construct(array $nodes)
{
$this->nodes = $nodes;
}
public function isEmpty() : bool
{
return $this->nodes === [];
}
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api * @api
* @var string * @var string
*/ */
public const PACKAGE_VERSION = '41235b8c7c52b0ba77872c2f04921685569c3b6e'; public const PACKAGE_VERSION = '0c0e5eeb4fd337d98f27224d0b46bfd310405dfd';
/** /**
* @api * @api
* @var string * @var string
*/ */
public const RELEASE_DATE = '2023-09-29 14:46:03'; public const RELEASE_DATE = '2023-09-30 10:58:42';
/** /**
* @var int * @var int
*/ */

View File

@ -2348,6 +2348,7 @@ return array(
'Rector\\TypeDeclaration\\ValueObject\\AddPropertyTypeDeclaration' => $baseDir . '/rules/TypeDeclaration/ValueObject/AddPropertyTypeDeclaration.php', 'Rector\\TypeDeclaration\\ValueObject\\AddPropertyTypeDeclaration' => $baseDir . '/rules/TypeDeclaration/ValueObject/AddPropertyTypeDeclaration.php',
'Rector\\TypeDeclaration\\ValueObject\\AddReturnTypeDeclaration' => $baseDir . '/rules/TypeDeclaration/ValueObject/AddReturnTypeDeclaration.php', 'Rector\\TypeDeclaration\\ValueObject\\AddReturnTypeDeclaration' => $baseDir . '/rules/TypeDeclaration/ValueObject/AddReturnTypeDeclaration.php',
'Rector\\TypeDeclaration\\ValueObject\\AssignToVariable' => $baseDir . '/rules/TypeDeclaration/ValueObject/AssignToVariable.php', 'Rector\\TypeDeclaration\\ValueObject\\AssignToVariable' => $baseDir . '/rules/TypeDeclaration/ValueObject/AssignToVariable.php',
'Rector\\TypeDeclaration\\ValueObject\\DataProviderNodes' => $baseDir . '/rules/TypeDeclaration/ValueObject/DataProviderNodes.php',
'Rector\\TypeDeclaration\\ValueObject\\NestedArrayType' => $baseDir . '/rules/TypeDeclaration/ValueObject/NestedArrayType.php', 'Rector\\TypeDeclaration\\ValueObject\\NestedArrayType' => $baseDir . '/rules/TypeDeclaration/ValueObject/NestedArrayType.php',
'Rector\\ValueObject\\ClassMethodWillChangeReturnType' => $vendorDir . '/rector/rector-downgrade-php/src/ValueObject/ClassMethodWillChangeReturnType.php', 'Rector\\ValueObject\\ClassMethodWillChangeReturnType' => $vendorDir . '/rector/rector-downgrade-php/src/ValueObject/ClassMethodWillChangeReturnType.php',
'Rector\\VendorLocker\\Exception\\UnresolvableClassException' => $baseDir . '/packages/VendorLocker/Exception/UnresolvableClassException.php', 'Rector\\VendorLocker\\Exception\\UnresolvableClassException' => $baseDir . '/packages/VendorLocker/Exception/UnresolvableClassException.php',

View File

@ -2567,6 +2567,7 @@ class ComposerStaticInit46a0b0ac2ea1371bb06be09a20f71c2b
'Rector\\TypeDeclaration\\ValueObject\\AddPropertyTypeDeclaration' => __DIR__ . '/../..' . '/rules/TypeDeclaration/ValueObject/AddPropertyTypeDeclaration.php', 'Rector\\TypeDeclaration\\ValueObject\\AddPropertyTypeDeclaration' => __DIR__ . '/../..' . '/rules/TypeDeclaration/ValueObject/AddPropertyTypeDeclaration.php',
'Rector\\TypeDeclaration\\ValueObject\\AddReturnTypeDeclaration' => __DIR__ . '/../..' . '/rules/TypeDeclaration/ValueObject/AddReturnTypeDeclaration.php', 'Rector\\TypeDeclaration\\ValueObject\\AddReturnTypeDeclaration' => __DIR__ . '/../..' . '/rules/TypeDeclaration/ValueObject/AddReturnTypeDeclaration.php',
'Rector\\TypeDeclaration\\ValueObject\\AssignToVariable' => __DIR__ . '/../..' . '/rules/TypeDeclaration/ValueObject/AssignToVariable.php', 'Rector\\TypeDeclaration\\ValueObject\\AssignToVariable' => __DIR__ . '/../..' . '/rules/TypeDeclaration/ValueObject/AssignToVariable.php',
'Rector\\TypeDeclaration\\ValueObject\\DataProviderNodes' => __DIR__ . '/../..' . '/rules/TypeDeclaration/ValueObject/DataProviderNodes.php',
'Rector\\TypeDeclaration\\ValueObject\\NestedArrayType' => __DIR__ . '/../..' . '/rules/TypeDeclaration/ValueObject/NestedArrayType.php', 'Rector\\TypeDeclaration\\ValueObject\\NestedArrayType' => __DIR__ . '/../..' . '/rules/TypeDeclaration/ValueObject/NestedArrayType.php',
'Rector\\ValueObject\\ClassMethodWillChangeReturnType' => __DIR__ . '/..' . '/rector/rector-downgrade-php/src/ValueObject/ClassMethodWillChangeReturnType.php', 'Rector\\ValueObject\\ClassMethodWillChangeReturnType' => __DIR__ . '/..' . '/rector/rector-downgrade-php/src/ValueObject/ClassMethodWillChangeReturnType.php',
'Rector\\VendorLocker\\Exception\\UnresolvableClassException' => __DIR__ . '/../..' . '/packages/VendorLocker/Exception/UnresolvableClassException.php', 'Rector\\VendorLocker\\Exception\\UnresolvableClassException' => __DIR__ . '/../..' . '/packages/VendorLocker/Exception/UnresolvableClassException.php',

View File

@ -2179,17 +2179,17 @@
}, },
{ {
"name": "symfony\/finder", "name": "symfony\/finder",
"version": "v6.3.3", "version": "v6.3.5",
"version_normalized": "6.3.3.0", "version_normalized": "6.3.5.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https:\/\/github.com\/symfony\/finder.git", "url": "https:\/\/github.com\/symfony\/finder.git",
"reference": "9915db259f67d21eefee768c1abcf1cc61b1fc9e" "reference": "a1b31d88c0e998168ca7792f222cbecee47428c4"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https:\/\/api.github.com\/repos\/symfony\/finder\/zipball\/9915db259f67d21eefee768c1abcf1cc61b1fc9e", "url": "https:\/\/api.github.com\/repos\/symfony\/finder\/zipball\/a1b31d88c0e998168ca7792f222cbecee47428c4",
"reference": "9915db259f67d21eefee768c1abcf1cc61b1fc9e", "reference": "a1b31d88c0e998168ca7792f222cbecee47428c4",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -2198,7 +2198,7 @@
"require-dev": { "require-dev": {
"symfony\/filesystem": "^6.0" "symfony\/filesystem": "^6.0"
}, },
"time": "2023-07-31T08:31:44+00:00", "time": "2023-09-26T12:56:25+00:00",
"type": "library", "type": "library",
"installation-source": "dist", "installation-source": "dist",
"autoload": { "autoload": {
@ -2226,7 +2226,7 @@
"description": "Finds files and directories via an intuitive fluent interface", "description": "Finds files and directories via an intuitive fluent interface",
"homepage": "https:\/\/symfony.com", "homepage": "https:\/\/symfony.com",
"support": { "support": {
"source": "https:\/\/github.com\/symfony\/finder\/tree\/v6.3.3" "source": "https:\/\/github.com\/symfony\/finder\/tree\/v6.3.5"
}, },
"funding": [ "funding": [
{ {

File diff suppressed because one or more lines are too long