Skip to content

Commit 68e0635

Browse files
authored
[DowngradePhp83] Add DowngradeTypedClassConstRector (#213)
* [DowngradePhp83] Add DowngradeTypedClassConstRector * cs and docs * implemented * trigger CI * rename fixture
1 parent 8d1aab2 commit 68e0635

File tree

12 files changed

+247
-3
lines changed

12 files changed

+247
-3
lines changed

config/set/downgrade-php83.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\ValueObject\PhpVersion;
7+
use Rector\DowngradePhp83\Rector\ClassConst\DowngradeTypedClassConstRector;
8+
9+
return static function (RectorConfig $rectorConfig): void {
10+
$rectorConfig->phpVersion(PhpVersion::PHP_82);
11+
$rectorConfig->rules([
12+
DowngradeTypedClassConstRector::class,
13+
]);
14+
};

config/set/level/down-to-php81.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
declare(strict_types=1);
44

55
use Rector\Config\RectorConfig;
6+
use Rector\Set\ValueObject\DowngradeLevelSetList;
67
use Rector\Set\ValueObject\DowngradeSetList;
78

89
return static function (RectorConfig $rectorConfig): void {
9-
$rectorConfig->sets([DowngradeSetList::PHP_82]);
10+
$rectorConfig->sets([DowngradeLevelSetList::DOWN_TO_PHP_82, DowngradeSetList::PHP_82]);
1011
};

config/set/level/down-to-php82.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+
5+
use Rector\Config\RectorConfig;
6+
use Rector\Set\ValueObject\DowngradeSetList;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->sets([DowngradeSetList::PHP_83]);
10+
};

docs/rector_rules_overview.md

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 71 Rules Overview
1+
# 74 Rules Overview
22

33
## ArrowFunctionToAnonymousFunctionRector
44

@@ -403,6 +403,25 @@ Changes `fread()` or `fwrite()` compare to false to negation check
403403

404404
<br>
405405

406+
## DowngradeHashAlgorithmXxHashRector
407+
408+
Downgrade hash algorithm xxh32, xxh64, xxh3 or xxh128 by default to md5. You can configure the algorithm to downgrade.
409+
410+
- class: [`Rector\DowngradePhp81\Rector\FuncCall\DowngradeHashAlgorithmXxHashRector`](../rules/DowngradePhp81/Rector/FuncCall/DowngradeHashAlgorithmXxHashRector.php)
411+
412+
```diff
413+
class SomeClass
414+
{
415+
public function run()
416+
{
417+
- return hash('xxh128', 'some-data-to-hash');
418+
+ return hash('md5', 'some-data-to-hash');
419+
}
420+
}
421+
```
422+
423+
<br>
424+
406425
## DowngradeIsCountableRector
407426

408427
Downgrade `is_countable()` to former version
@@ -1144,6 +1163,25 @@ Add `setAccessible()` on ReflectionProperty to allow reading private properties
11441163

11451164
<br>
11461165

1166+
## DowngradeStandaloneNullTrueFalseReturnTypeRector
1167+
1168+
Downgrade standalone return null, true, or false
1169+
1170+
- class: [`Rector\DowngradePhp82\Rector\FunctionLike\DowngradeStandaloneNullTrueFalseReturnTypeRector`](../rules/DowngradePhp82/Rector/FunctionLike/DowngradeStandaloneNullTrueFalseReturnTypeRector.php)
1171+
1172+
```diff
1173+
final class SomeClass
1174+
{
1175+
- public function run(): null
1176+
+ public function run(): mixed
1177+
{
1178+
return null;
1179+
}
1180+
}
1181+
```
1182+
1183+
<br>
1184+
11471185
## DowngradeStaticTypeDeclarationRector
11481186

11491187
Remove "static" return and param type, add a `"@param` `$this"` and `"@return` `$this"` tag instead
@@ -1386,6 +1424,25 @@ Remove trailing commas in unset
13861424

13871425
<br>
13881426

1427+
## DowngradeTypedClassConstRector
1428+
1429+
Remove typed class constant
1430+
1431+
- class: [`Rector\DowngradePhp83\Rector\ClassConst\DowngradeTypedClassConstRector`](../rules/DowngradePhp83/Rector/ClassConst/DowngradeTypedClassConstRector.php)
1432+
1433+
```diff
1434+
final class SomeClass
1435+
{
1436+
- public string FOO = 'test';
1437+
+ /**
1438+
+ * @var string
1439+
+ */
1440+
+ public FOO = 'test';
1441+
}
1442+
```
1443+
1444+
<br>
1445+
13891446
## DowngradeTypedPropertyRector
13901447

13911448
Changes property type definition from type definitions to `@var` annotations.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\DowngradePhp83\Rector\ClassConst\DowngradeTypedClassConstRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class DowngradeTypedClassConstRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
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+
5+
namespace Rector\Tests\DowngradePhp83\Rector\ClassConst\DowngradeTypedClassConstRector;
6+
7+
class SkipNonTypedClassConst
8+
{
9+
public const FOO = 'foo';
10+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\DowngradePhp83\Rector\ClassConst\DowngradeTypedClassConstRector;
6+
7+
class TypedClassConstant
8+
{
9+
public const string FOO = 'foo';
10+
}
11+
12+
?>
13+
-----
14+
<?php
15+
16+
declare(strict_types=1);
17+
18+
namespace Rector\Tests\DowngradePhp83\Rector\ClassConst\DowngradeTypedClassConstRector;
19+
20+
class TypedClassConstant
21+
{
22+
/**
23+
* @var string
24+
*/
25+
public const FOO = 'foo';
26+
}
27+
28+
?>
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+
5+
use Rector\Config\RectorConfig;
6+
use Rector\DowngradePhp83\Rector\ClassConst\DowngradeTypedClassConstRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(DowngradeTypedClassConstRector::class);
10+
};
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\DowngradePhp83\Rector\ClassConst;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Stmt\ClassConst;
9+
use Rector\NodeManipulator\PropertyDecorator;
10+
use Rector\Rector\AbstractRector;
11+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
12+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
13+
14+
/**
15+
* @changelog https://wiki.php.net/rfc/typed_class_constants
16+
*
17+
* @see \Rector\Tests\DowngradePhp83\Rector\ClassConst\DowngradeTypedClassConstRector\DowngradeTypedClassConstRectorTest
18+
*/
19+
final class DowngradeTypedClassConstRector extends AbstractRector
20+
{
21+
public function __construct(
22+
private readonly PropertyDecorator $propertyDecorator
23+
) {
24+
}
25+
26+
/**
27+
* @return array<class-string<Node>>
28+
*/
29+
public function getNodeTypes(): array
30+
{
31+
return [ClassConst::class];
32+
}
33+
34+
public function getRuleDefinition(): RuleDefinition
35+
{
36+
return new RuleDefinition(
37+
'Remove typed class constant',
38+
[
39+
new CodeSample(
40+
<<<'CODE_SAMPLE'
41+
final class SomeClass
42+
{
43+
public string FOO = 'test';
44+
}
45+
CODE_SAMPLE
46+
,
47+
<<<'CODE_SAMPLE'
48+
final class SomeClass
49+
{
50+
/**
51+
* @var string
52+
*/
53+
public FOO = 'test';
54+
}
55+
CODE_SAMPLE
56+
),
57+
]
58+
);
59+
}
60+
61+
/**
62+
* @param ClassConst $node
63+
*/
64+
public function refactor(Node $node): ?Node
65+
{
66+
if (! $node->type instanceof Node) {
67+
return null;
68+
}
69+
70+
$this->propertyDecorator->decorateWithDocBlock($node, $node->type);
71+
72+
$node->type = null;
73+
return $node;
74+
}
75+
}

src/NodeManipulator/PropertyDecorator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpParser\Node\ComplexType;
88
use PhpParser\Node\Identifier;
99
use PhpParser\Node\Name;
10+
use PhpParser\Node\Stmt\ClassConst;
1011
use PhpParser\Node\Stmt\Property;
1112
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
1213
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
@@ -22,7 +23,7 @@ public function __construct(
2223
) {
2324
}
2425

25-
public function decorateWithDocBlock(Property $property, ComplexType|Identifier|Name $typeNode): void
26+
public function decorateWithDocBlock(Property|ClassConst $property, ComplexType|Identifier|Name $typeNode): void
2627
{
2728
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);
2829
if ($phpDocInfo->getVarTagValueNode() instanceof VarTagValueNode) {

0 commit comments

Comments
 (0)