-
-
Notifications
You must be signed in to change notification settings - Fork 359
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Php84] Add ExplicitNullableParamTypeRector #5724
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
db5bd94
[Php84] Add ExplicitNullableParamTypeRector
samsonasik 2a7e462
more fixture
samsonasik 5c3c941
fix phpstan
samsonasik 463a018
rename fixture
samsonasik a2865cc
renaming fixture
samsonasik d99e19e
renaming fixture
samsonasik df58610
fix @see
samsonasik 26e29f3
fix
samsonasik d885ca2
final touch: regenerate docs
samsonasik ed48e9b
final touch: more test
samsonasik e141d8d
final touch: more test
samsonasik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this needs to be changed to PHP 7.1, as this is the minimum version that support the nullable types. Projects need to make this change in order to be fully compatible with PHP 7.4: #5848
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would we optional coding style preference. Since PHP 8.4 it's part of required upgrade path.