Skip to content

Commit c082009

Browse files
committed
Updated Rector to commit ff32c0c08a89f27ea34187d00cf707734a7e39c8
rectorphp/rector-src@ff32c0c [Php84] Add ExplicitNullableParamTypeRector (#5724)
1 parent edd8901 commit c082009

File tree

14 files changed

+148
-9
lines changed

14 files changed

+148
-9
lines changed

config/set/level/up-to-php84.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare (strict_types=1);
4+
namespace RectorPrefix202403;
5+
6+
use Rector\Config\RectorConfig;
7+
use Rector\Set\ValueObject\LevelSetList;
8+
use Rector\Set\ValueObject\SetList;
9+
return static function (RectorConfig $rectorConfig) : void {
10+
$rectorConfig->sets([SetList::PHP_84, LevelSetList::UP_TO_PHP_83]);
11+
};

config/set/php84.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare (strict_types=1);
4+
namespace RectorPrefix202403;
5+
6+
use Rector\Config\RectorConfig;
7+
use Rector\Php84\Rector\Param\ExplicitNullableParamTypeRector;
8+
return static function (RectorConfig $rectorConfig) : void {
9+
$rectorConfig->rules([ExplicitNullableParamTypeRector::class]);
10+
};

docs/rector_rules_overview.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 364 Rules Overview
1+
# 365 Rules Overview
22

33
<br>
44

@@ -46,6 +46,8 @@
4646

4747
- [Php83](#php83) (3)
4848

49+
- [Php84](#php84) (1)
50+
4951
- [Privatization](#privatization) (5)
5052

5153
- [Removing](#removing) (5)
@@ -5328,6 +5330,21 @@ Combine separated host and port on `ldap_connect()` args
53285330

53295331
<br>
53305332

5333+
## Php84
5334+
5335+
### ExplicitNullableParamTypeRector
5336+
5337+
Make implicit nullable param to explicit
5338+
5339+
- class: [`Rector\Php84\Rector\Param\ExplicitNullableParamTypeRector`](../rules/Php84/Rector/Param/ExplicitNullableParamTypeRector.php)
5340+
5341+
```diff
5342+
-function foo(string $param = null) {}
5343+
+function foo(?string $param = null) {}
5344+
```
5345+
5346+
<br>
5347+
53315348
## Privatization
53325349

53335350
### FinalizeClassesWithoutChildrenRector
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
declare (strict_types=1);
4+
namespace Rector\Php84\Rector\Param;
5+
6+
use PhpParser\Node;
7+
use PhpParser\Node\Expr\ConstFetch;
8+
use PhpParser\Node\Param;
9+
use PHPStan\Type\NullType;
10+
use PHPStan\Type\TypeCombinator;
11+
use Rector\PhpParser\Node\Value\ValueResolver;
12+
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
13+
use Rector\Rector\AbstractRector;
14+
use Rector\StaticTypeMapper\StaticTypeMapper;
15+
use Rector\ValueObject\PhpVersionFeature;
16+
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
17+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
18+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
19+
/**
20+
* @see \Rector\Tests\Php84\Rector\Param\ExplicitNullableParamTypeRector\ExplicitNullableParamTypeRectorTest
21+
*/
22+
final class ExplicitNullableParamTypeRector extends AbstractRector implements MinPhpVersionInterface
23+
{
24+
/**
25+
* @readonly
26+
* @var \Rector\PhpParser\Node\Value\ValueResolver
27+
*/
28+
private $valueResolver;
29+
/**
30+
* @readonly
31+
* @var \Rector\StaticTypeMapper\StaticTypeMapper
32+
*/
33+
private $staticTypeMapper;
34+
public function __construct(ValueResolver $valueResolver, StaticTypeMapper $staticTypeMapper)
35+
{
36+
$this->valueResolver = $valueResolver;
37+
$this->staticTypeMapper = $staticTypeMapper;
38+
}
39+
public function getRuleDefinition() : RuleDefinition
40+
{
41+
return new RuleDefinition('Make implicit nullable param to explicit', [new CodeSample(<<<'CODE_SAMPLE'
42+
function foo(string $param = null) {}
43+
CODE_SAMPLE
44+
, <<<'CODE_SAMPLE'
45+
function foo(?string $param = null) {}
46+
CODE_SAMPLE
47+
)]);
48+
}
49+
public function getNodeTypes() : array
50+
{
51+
return [Param::class];
52+
}
53+
/**
54+
* @param Param $node
55+
*/
56+
public function refactor(Node $node) : ?Param
57+
{
58+
if (!$node->type instanceof Node) {
59+
return null;
60+
}
61+
if (!$node->default instanceof ConstFetch || !$this->valueResolver->isNull($node->default)) {
62+
return null;
63+
}
64+
$nodeType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($node->type);
65+
if ($nodeType instanceof NullType) {
66+
return null;
67+
}
68+
$removedNullNodeType = TypeCombinator::removeNull($nodeType);
69+
if (!$nodeType->equals($removedNullNodeType)) {
70+
return null;
71+
}
72+
$newNodeType = TypeCombinator::addNull($nodeType);
73+
$node->type = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($newNodeType, TypeKind::PARAM);
74+
return $node;
75+
}
76+
public function provideMinPhpVersion() : int
77+
{
78+
return PhpVersionFeature::DEPRECATE_IMPLICIT_NULLABLE_PARAM_TYPE;
79+
}
80+
}

src/Application/VersionResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ final class VersionResolver
1919
* @api
2020
* @var string
2121
*/
22-
public const PACKAGE_VERSION = '23e3da2d6eee90e4f2a31c31062aabaeba77ba69';
22+
public const PACKAGE_VERSION = 'ff32c0c08a89f27ea34187d00cf707734a7e39c8';
2323
/**
2424
* @api
2525
* @var string
2626
*/
27-
public const RELEASE_DATE = '2024-03-14 20:14:23';
27+
public const RELEASE_DATE = '2024-03-15 11:43:37';
2828
/**
2929
* @var int
3030
*/

src/Configuration/RectorConfigBuilder.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ public function withAttributesSets(bool $symfony = \false, bool $doctrine = \fal
313313
* What PHP sets should be applied? By default the same version
314314
* as composer.json has is used
315315
*/
316-
public function withPhpSets(bool $php83 = \false, bool $php82 = \false, bool $php81 = \false, bool $php80 = \false, bool $php74 = \false, bool $php73 = \false, bool $php72 = \false, bool $php71 = \false, bool $php70 = \false, bool $php56 = \false, bool $php55 = \false, bool $php54 = \false, bool $php53 = \false) : self
316+
public function withPhpSets(bool $php83 = \false, bool $php82 = \false, bool $php81 = \false, bool $php80 = \false, bool $php74 = \false, bool $php73 = \false, bool $php72 = \false, bool $php71 = \false, bool $php70 = \false, bool $php56 = \false, bool $php55 = \false, bool $php54 = \false, bool $php53 = \false, bool $php84 = \false) : self
317317
{
318318
$pickedArguments = \array_filter(\func_get_args());
319319
if (\count($pickedArguments) > 1) {
@@ -357,6 +357,8 @@ public function withPhpSets(bool $php83 = \false, bool $php82 = \false, bool $ph
357357
$this->sets[] = LevelSetList::UP_TO_PHP_82;
358358
} elseif ($php83) {
359359
$this->sets[] = LevelSetList::UP_TO_PHP_83;
360+
} elseif ($php84) {
361+
$this->sets[] = LevelSetList::UP_TO_PHP_84;
360362
}
361363
return $this;
362364
}

src/Set/ValueObject/LevelSetList.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
*/
1010
final class LevelSetList implements SetListInterface
1111
{
12+
/**
13+
* @var string
14+
*/
15+
public const UP_TO_PHP_84 = __DIR__ . '/../../../config/set/level/up-to-php84.php';
1216
/**
1317
* @var string
1418
*/

src/Set/ValueObject/SetList.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ final class SetList implements SetListInterface
8989
* @var string
9090
*/
9191
public const PHP_83 = __DIR__ . '/../../../config/set/php83.php';
92+
/**
93+
* @var string
94+
*/
95+
public const PHP_84 = __DIR__ . '/../../../config/set/php84.php';
9296
/**
9397
* @var string
9498
*/

src/ValueObject/PhpVersion.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ final class PhpVersion
6464
* @var int
6565
*/
6666
public const PHP_83 = 80300;
67+
/**
68+
* @var int
69+
*/
70+
public const PHP_84 = 80400;
6771
/**
6872
* @var int
6973
*/

src/ValueObject/PhpVersionFeature.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,11 @@ final class PhpVersionFeature
532532
* @var int
533533
*/
534534
public const TYPED_CLASS_CONSTANTS = \Rector\ValueObject\PhpVersion::PHP_83;
535+
/**
536+
* @see https://wiki.php.net/rfc/deprecate-implicitly-nullable-types
537+
* @var int
538+
*/
539+
public const DEPRECATE_IMPLICIT_NULLABLE_PARAM_TYPE = \Rector\ValueObject\PhpVersion::PHP_84;
535540
/**
536541
* @see https://www.php.net/manual/en/migration83.deprecated.php#migration83.deprecated.ldap
537542
* @var int

0 commit comments

Comments
 (0)