Skip to content

Commit

Permalink
Updated Rector to commit 0c0e5eeb4fd337d98f27224d0b46bfd310405dfd
Browse files Browse the repository at this point in the history
rectorphp/rector-src@0c0e5ee AddParamTypeBasedOnPHPUnitDataProviderRector: Enhance existing rule to handle PHPUnit 10+ DataProvider Attribute (#4925)
  • Loading branch information
TomasVotruba committed Sep 30, 2023
1 parent a8fc4e6 commit d7242c6
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 31 deletions.
2 changes: 1 addition & 1 deletion docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

```diff
use PHPUnit\Framework\TestCase
use PHPUnit\Framework\TestCase;

final class SomeTest extends TestCase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@

use RectorPrefix202309\Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\Yield_;
use PhpParser\Node\Param;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Return_;
Expand All @@ -28,6 +31,7 @@
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\TypeDeclaration\ValueObject\DataProviderNodes;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
Expand Down Expand Up @@ -80,7 +84,7 @@ public function __construct(TypeFactory $typeFactory, TestsNodeAnalyzer $testsNo
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition(self::ERROR_MESSAGE, [new CodeSample(<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
Expand All @@ -98,7 +102,7 @@ public static function provideData()
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
Expand Down Expand Up @@ -140,11 +144,11 @@ public function refactor(Node $node) : ?Node
if ($classMethod->getParams() === []) {
continue;
}
$dataProviderPhpDocTagNodes = $this->resolveDataProviderPhpDocTagNode($classMethod);
if ($dataProviderPhpDocTagNodes === []) {
$dataProviderNodes = $this->resolveDataProviderNodes($classMethod);
if ($dataProviderNodes->isEmpty()) {
return null;
}
$hasClassMethodChanged = $this->refactorClassMethod($classMethod, $node, $dataProviderPhpDocTagNodes);
$hasClassMethodChanged = $this->refactorClassMethod($classMethod, $node, $dataProviderNodes->nodes);
if ($hasClassMethodChanged) {
$hasChanged = \true;
}
Expand All @@ -154,9 +158,12 @@ public function refactor(Node $node) : ?Node
}
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) {
return new MixedType();
}
Expand All @@ -173,12 +180,22 @@ private function inferParam(Class_ $class, Param $param, PhpDocTagNode $dataProv
$yields = $this->betterNodeFinder->findInstanceOf((array) $dataProviderClassMethod->stmts, Yield_::class);
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;
}
$content = $dataProviderPhpDocTagNode->value->value;
$match = Strings::match($content, self::METHOD_NAME_REGEX);
if ($match === null) {
return null;
Expand Down Expand Up @@ -261,30 +278,45 @@ private function resolveParamOnPositionTypes(Array_ $array, int $parameterPositi
}
return $paramOnPositionTypes;
}
private function resolveDataProviderNodes(ClassMethod $classMethod) : DataProviderNodes
{
$attributes = $this->getPhpDataProviderAttributes($classMethod);
$classMethodPhpDocInfo = $this->phpDocInfoFactory->createFromNode($classMethod);
$phpdocNodes = $classMethodPhpDocInfo instanceof PhpDocInfo ? $classMethodPhpDocInfo->getTagsByName('@dataProvider') : [];
return new DataProviderNodes(\array_merge($attributes, $phpdocNodes));
}
/**
* @return array<PhpDocTagNode>
* @return array<array-key, Attribute>
*/
private function resolveDataProviderPhpDocTagNode(ClassMethod $classMethod) : array
private function getPhpDataProviderAttributes(ClassMethod $node) : array
{
$classMethodPhpDocInfo = $this->phpDocInfoFactory->createFromNode($classMethod);
if (!$classMethodPhpDocInfo instanceof PhpDocInfo) {
return [];
$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 $classMethodPhpDocInfo->getTagsByName('@dataProvider');
return $dataProviders;
}
/**
* @param array<PhpDocTagNode> $dataProviderPhpDocTagNodes
* @param array<Attribute|PhpDocTagNode> $dataProviderNodes
*/
private function refactorClassMethod(ClassMethod $classMethod, Class_ $class, array $dataProviderPhpDocTagNodes) : bool
private function refactorClassMethod(ClassMethod $classMethod, Class_ $class, array $dataProviderNodes) : bool
{
$hasChanged = \false;
foreach ($classMethod->getParams() as $param) {
if ($param->type instanceof Node) {
continue;
}
$paramTypes = [];
foreach ($dataProviderPhpDocTagNodes as $dataProviderPhpDocTagNode) {
$paramTypes[] = $this->inferParam($class, $param, $dataProviderPhpDocTagNode);
foreach ($dataProviderNodes as $dataProviderNode) {
$paramTypes[] = $this->inferParam($class, $param, $dataProviderNode);
}
$paramTypeDeclaration = TypeCombinator::union(...$paramTypes);
if ($paramTypeDeclaration instanceof MixedType) {
Expand Down
26 changes: 26 additions & 0 deletions rules/TypeDeclaration/ValueObject/DataProviderNodes.php
Original file line number Diff line number Diff line change
@@ -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 === [];
}
}
4 changes: 2 additions & 2 deletions src/Application/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '41235b8c7c52b0ba77872c2f04921685569c3b6e';
public const PACKAGE_VERSION = '0c0e5eeb4fd337d98f27224d0b46bfd310405dfd';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-09-29 14:46:03';
public const RELEASE_DATE = '2023-09-30 10:58:42';
/**
* @var int
*/
Expand Down
1 change: 1 addition & 0 deletions vendor/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2348,6 +2348,7 @@
'Rector\\TypeDeclaration\\ValueObject\\AddPropertyTypeDeclaration' => $baseDir . '/rules/TypeDeclaration/ValueObject/AddPropertyTypeDeclaration.php',
'Rector\\TypeDeclaration\\ValueObject\\AddReturnTypeDeclaration' => $baseDir . '/rules/TypeDeclaration/ValueObject/AddReturnTypeDeclaration.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\\ValueObject\\ClassMethodWillChangeReturnType' => $vendorDir . '/rector/rector-downgrade-php/src/ValueObject/ClassMethodWillChangeReturnType.php',
'Rector\\VendorLocker\\Exception\\UnresolvableClassException' => $baseDir . '/packages/VendorLocker/Exception/UnresolvableClassException.php',
Expand Down
1 change: 1 addition & 0 deletions vendor/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -2567,6 +2567,7 @@ class ComposerStaticInit46a0b0ac2ea1371bb06be09a20f71c2b
'Rector\\TypeDeclaration\\ValueObject\\AddPropertyTypeDeclaration' => __DIR__ . '/../..' . '/rules/TypeDeclaration/ValueObject/AddPropertyTypeDeclaration.php',
'Rector\\TypeDeclaration\\ValueObject\\AddReturnTypeDeclaration' => __DIR__ . '/../..' . '/rules/TypeDeclaration/ValueObject/AddReturnTypeDeclaration.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\\ValueObject\\ClassMethodWillChangeReturnType' => __DIR__ . '/..' . '/rector/rector-downgrade-php/src/ValueObject/ClassMethodWillChangeReturnType.php',
'Rector\\VendorLocker\\Exception\\UnresolvableClassException' => __DIR__ . '/../..' . '/packages/VendorLocker/Exception/UnresolvableClassException.php',
Expand Down
14 changes: 7 additions & 7 deletions vendor/composer/installed.json
Original file line number Diff line number Diff line change
Expand Up @@ -2179,17 +2179,17 @@
},
{
"name": "symfony\/finder",
"version": "v6.3.3",
"version_normalized": "6.3.3.0",
"version": "v6.3.5",
"version_normalized": "6.3.5.0",
"source": {
"type": "git",
"url": "https:\/\/github.com\/symfony\/finder.git",
"reference": "9915db259f67d21eefee768c1abcf1cc61b1fc9e"
"reference": "a1b31d88c0e998168ca7792f222cbecee47428c4"
},
"dist": {
"type": "zip",
"url": "https:\/\/api.github.com\/repos\/symfony\/finder\/zipball\/9915db259f67d21eefee768c1abcf1cc61b1fc9e",
"reference": "9915db259f67d21eefee768c1abcf1cc61b1fc9e",
"url": "https:\/\/api.github.com\/repos\/symfony\/finder\/zipball\/a1b31d88c0e998168ca7792f222cbecee47428c4",
"reference": "a1b31d88c0e998168ca7792f222cbecee47428c4",
"shasum": ""
},
"require": {
Expand All @@ -2198,7 +2198,7 @@
"require-dev": {
"symfony\/filesystem": "^6.0"
},
"time": "2023-07-31T08:31:44+00:00",
"time": "2023-09-26T12:56:25+00:00",
"type": "library",
"installation-source": "dist",
"autoload": {
Expand Down Expand Up @@ -2226,7 +2226,7 @@
"description": "Finds files and directories via an intuitive fluent interface",
"homepage": "https:\/\/symfony.com",
"support": {
"source": "https:\/\/github.com\/symfony\/finder\/tree\/v6.3.3"
"source": "https:\/\/github.com\/symfony\/finder\/tree\/v6.3.5"
},
"funding": [
{
Expand Down
Loading

0 comments on commit d7242c6

Please sign in to comment.