Skip to content

Commit

Permalink
[TypeDeclaration] Add AddParamTypeBasedOnPHPUnitDataProviderRector + …
Browse files Browse the repository at this point in the history
…remove too narrow KnownArrayParamTypeInferer (#3104)
  • Loading branch information
TomasVotruba committed Nov 27, 2022
1 parent 2af3c7f commit 48febdb
Show file tree
Hide file tree
Showing 15 changed files with 308 additions and 266 deletions.
32 changes: 30 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 @@
# 402 Rules Overview
# 403 Rules Overview

<br>

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

- [Transform](#transform) (34)

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

- [Visibility](#visibility) (3)

Expand Down Expand Up @@ -8805,6 +8805,34 @@ Change private method param type to strict type, based on passed strict types

<br>

### AddParamTypeBasedOnPHPUnitDataProviderRector

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

final class SomeTest extends TestCase
{
/**
* @dataProvider provideData()
*/
- public function test($value)
+ public function test(string $value)
{
}

public function provideData()
{
yield ['name'];
}
}
```

<br>

### AddParamTypeDeclarationRector

Add param types where needed
Expand Down
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -784,3 +784,6 @@ parameters:

# returns bool for notifications
- '#Method "renamePropertyPromotion\(\)" returns bool type, so the name should start with is/has/was#'

# false positive
- '#Method Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddParamTypeBasedOnPHPUnitDataProviderRector\:\:resolveDataProviderPhpDocTagNode\(\) should return PHPStan\\PhpDocParser\\Ast\\PhpDoc\\PhpDocTagNode\|null but returns PHPStan\\PhpDocParser\\Ast\\Node\|null#'

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\DoubleImportedTraitUse;
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamTypeBasedOnPHPUnitDataProviderRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class DoubleImportedTraitUseTest extends AbstractRectorTestCase
final class AddParamTypeBasedOnPHPUnitDataProviderRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
Expand All @@ -27,6 +27,6 @@ public function provideData(): Iterator

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/double_imported_trait_use.php';
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamTypeBasedOnPHPUnitDataProviderRector\Fixture;

use Iterator;
use PHPUnit\Framework\TestCase;

final class SomeTestWithDataProvider extends TestCase
{
/**
* @dataProvider provideData()
*/
public function test($name)
{
}

public function provideData(): Iterator
{
yield ['some'];
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamTypeBasedOnPHPUnitDataProviderRector\Fixture;

use Iterator;
use PHPUnit\Framework\TestCase;

final class SomeTestWithDataProvider extends TestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $name)
{
}

public function provideData(): Iterator
{
yield ['some'];
}
}

?>
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector\Fixture;
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamTypeBasedOnPHPUnitDataProviderRector\Fixture;

use PHPUnit\Framework\TestCase;

/**
* Test case including a dataProvider using an array that lead to an incorrect types being added.
* `array` was added for $one, but should be `string`, or the parameter should be left alone entirely.
* The $two and $three parameters did not get hints added at all.
* See: https://phpunit.readthedocs.io/en/9.3/writing-tests-for-phpunit.html#data-providers
*/
final class DemoArrayTest
final class ManyScalars extends TestCase
{
public function provideThings(): array {
return [
Expand All @@ -23,18 +25,22 @@ final class DemoArrayTest
*/
public function testGetFromId( $one, $two, $three, $four, $five ): void {}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector\Fixture;
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamTypeBasedOnPHPUnitDataProviderRector\Fixture;

use PHPUnit\Framework\TestCase;

/**
* Test case including a dataProvider using an array that lead to an incorrect types being added.
* `array` was added for $one, but should be `string`, or the parameter should be left alone entirely.
* The $two and $three parameters did not get hints added at all.
* See: https://phpunit.readthedocs.io/en/9.3/writing-tests-for-phpunit.html#data-providers
*/
final class DemoArrayTest
final class ManyScalars extends TestCase
{
public function provideThings(): array {
return [
Expand All @@ -49,3 +55,5 @@ final class DemoArrayTest
*/
public function testGetFromId( int $one, bool $two, string $three, ?int $four, $five ): void {}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamTypeBasedOnPHPUnitDataProviderRector\Fixture;

use Iterator;
use PHPUnit\Framework\TestCase;

final class ReturnArray extends TestCase
{
/**
* @dataProvider provideData()
*/
public function test($name, $number)
{
}

public function provideData()
{
return [['some', 100]];
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamTypeBasedOnPHPUnitDataProviderRector\Fixture;

use Iterator;
use PHPUnit\Framework\TestCase;

final class ReturnArray extends TestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $name, int $number)
{
}

public function provideData()
{
return [['some', 100]];
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamTypeBasedOnPHPUnitDataProviderRector\Fixture;

use Iterator;
use PHPUnit\Framework\TestCase;

final class SkipNoType extends TestCase
{
/**
* @dataProvider provideData()
*/
public function test($name)
{
}

public function provideData($some): Iterator
{
yield [$some];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeBasedOnPHPUnitDataProviderRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(AddParamTypeBasedOnPHPUnitDataProviderRector::class);

$rectorConfig->phpVersion(PhpVersionFeature::UNION_TYPES - 1);
};

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use PhpParser\Node\Param;
use PHPStan\Type\Type;

/**
* @deprecated This interface is not used anymore. Use the exact Rector rules instead.
*/
interface ParamTypeInfererInterface
{
public function inferParam(Param $param): Type;
Expand Down
Loading

0 comments on commit 48febdb

Please sign in to comment.