-
-
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.
[Php84] Add ExplicitNullableParamTypeRector (#5724)
* [Php84] Add ExplicitNullableParamTypeRector * more fixture Signed-off-by: Abdul Malik Ikhsan <samsonasik@gmail.com> * fix phpstan * rename fixture * renaming fixture * renaming fixture * fix @see * fix * final touch: regenerate docs * final touch: more test * final touch: more test --------- Signed-off-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
- Loading branch information
1 parent
23e3da2
commit ff32c0c
Showing
19 changed files
with
296 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\Set\ValueObject\LevelSetList; | ||
use Rector\Set\ValueObject\SetList; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->sets([SetList::PHP_84, LevelSetList::UP_TO_PHP_83]); | ||
}; |
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\Php84\Rector\Param\ExplicitNullableParamTypeRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rules([ | ||
ExplicitNullableParamTypeRector::class, | ||
]); | ||
}; |
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
28 changes: 28 additions & 0 deletions
28
...hp84/Rector/Param/ExplicitNullableParamTypeRector/ExplicitNullableParamTypeRectorTest.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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Php84\Rector\Param\ExplicitNullableParamTypeRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class ExplicitNullableParamTypeRectorTest extends AbstractRectorTestCase | ||
{ | ||
#[DataProvider('provideData')] | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
public static function provideData(): Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
rules-tests/Php84/Rector/Param/ExplicitNullableParamTypeRector/Fixture/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,25 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php84\Rector\Param\ExplicitNullableParamTypeRector\Fixture; | ||
|
||
class WithParamTypeNullDefault | ||
{ | ||
public function run(string $a = null) | ||
{ | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Php84\Rector\Param\ExplicitNullableParamTypeRector\Fixture; | ||
|
||
class WithParamTypeNullDefault | ||
{ | ||
public function run(?string $a = null) | ||
{ | ||
} | ||
} | ||
|
||
?> |
10 changes: 10 additions & 0 deletions
10
.../Php84/Rector/Param/ExplicitNullableParamTypeRector/Fixture/skip_already_nullable.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,10 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php84\Rector\Param\ExplicitNullableParamTypeRector\Fixture; | ||
|
||
class SkipAlreadyNullable | ||
{ | ||
public function run(?string $a = null) | ||
{ | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...ests/Php84/Rector/Param/ExplicitNullableParamTypeRector/Fixture/skip_default_true.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,10 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php84\Rector\Param\ExplicitNullableParamTypeRector\Fixture; | ||
|
||
class SkipDefaultTrue | ||
{ | ||
public function run(bool $a = true) | ||
{ | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...-tests/Php84/Rector/Param/ExplicitNullableParamTypeRector/Fixture/skip_no_default.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,10 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php84\Rector\Param\ExplicitNullableParamTypeRector\Fixture; | ||
|
||
class SkipNoDefault | ||
{ | ||
public function run(bool $a) | ||
{ | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
rules-tests/Php84/Rector/Param/ExplicitNullableParamTypeRector/Fixture/skip_no_type.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,10 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php84\Rector\Param\ExplicitNullableParamTypeRector\Fixture; | ||
|
||
class SkipNoType | ||
{ | ||
public function run($a = null) | ||
{ | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...-tests/Php84/Rector/Param/ExplicitNullableParamTypeRector/Fixture/skip_null_param.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,10 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php84\Rector\Param\ExplicitNullableParamTypeRector\Fixture; | ||
|
||
class SkipNullParam | ||
{ | ||
public function run(null $a = null) | ||
{ | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...hp84/Rector/Param/ExplicitNullableParamTypeRector/Fixture/union_type_null_default.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,25 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php84\Rector\Param\ExplicitNullableParamTypeRector\Fixture; | ||
|
||
class UnionTypeNullDefault | ||
{ | ||
public function run(int|string $a = null) | ||
{ | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Php84\Rector\Param\ExplicitNullableParamTypeRector\Fixture; | ||
|
||
class UnionTypeNullDefault | ||
{ | ||
public function run(int|string|null $a = null) | ||
{ | ||
} | ||
} | ||
|
||
?> |
13 changes: 13 additions & 0 deletions
13
rules-tests/Php84/Rector/Param/ExplicitNullableParamTypeRector/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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\Php84\Rector\Param\ExplicitNullableParamTypeRector; | ||
use Rector\ValueObject\PhpVersion; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(ExplicitNullableParamTypeRector::class); | ||
|
||
$rectorConfig->phpVersion(PhpVersion::PHP_84); | ||
}; |
87 changes: 87 additions & 0 deletions
87
rules/Php84/Rector/Param/ExplicitNullableParamTypeRector.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,87 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Php84\Rector\Param; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\ConstFetch; | ||
use PhpParser\Node\Param; | ||
use PHPStan\Type\NullType; | ||
use PHPStan\Type\TypeCombinator; | ||
use Rector\PhpParser\Node\Value\ValueResolver; | ||
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; | ||
use Rector\Rector\AbstractRector; | ||
use Rector\StaticTypeMapper\StaticTypeMapper; | ||
use Rector\ValueObject\PhpVersionFeature; | ||
use Rector\VersionBonding\Contract\MinPhpVersionInterface; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\Php84\Rector\Param\ExplicitNullableParamTypeRector\ExplicitNullableParamTypeRectorTest | ||
*/ | ||
final class ExplicitNullableParamTypeRector extends AbstractRector implements MinPhpVersionInterface | ||
{ | ||
public function __construct( | ||
private readonly ValueResolver $valueResolver, | ||
private readonly StaticTypeMapper $staticTypeMapper | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Make implicit nullable param to explicit', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
function foo(string $param = null) {} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
function foo(?string $param = null) {} | ||
CODE_SAMPLE | ||
, | ||
), | ||
]); | ||
} | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [Param::class]; | ||
} | ||
|
||
/** | ||
* @param Param $node | ||
*/ | ||
public function refactor(Node $node): ?Param | ||
{ | ||
if (! $node->type instanceof Node) { | ||
return null; | ||
} | ||
|
||
if (! $node->default instanceof ConstFetch || ! $this->valueResolver->isNull($node->default)) { | ||
return null; | ||
} | ||
|
||
$nodeType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($node->type); | ||
if ($nodeType instanceof NullType) { | ||
return null; | ||
} | ||
|
||
$removedNullNodeType = TypeCombinator::removeNull($nodeType); | ||
|
||
if (! $nodeType->equals($removedNullNodeType)) { | ||
return null; | ||
} | ||
|
||
$newNodeType = TypeCombinator::addNull($nodeType); | ||
$node->type = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($newNodeType, TypeKind::PARAM); | ||
|
||
return $node; | ||
} | ||
|
||
public function provideMinPhpVersion(): int | ||
{ | ||
return PhpVersionFeature::DEPRECATE_IMPLICIT_NULLABLE_PARAM_TYPE; | ||
} | ||
} |
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
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
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