Skip to content

Commit d8a35d8

Browse files
committed
[dx] Rename DeprecatedAnnotationToDeprecatedAttributeRector for PHP 8.5 to explicit ConstAndTraitDeprecatedAttributeRector, add trait suport
1 parent 184dd02 commit d8a35d8

File tree

11 files changed

+133
-22
lines changed

11 files changed

+133
-22
lines changed

.github/workflows/code_analysis.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ jobs:
2929
name: 'PHPStan'
3030
run: vendor/bin/phpstan analyse --ansi
3131

32+
-
33+
name: 'Avoid duplicate short class names'
34+
run: php scripts/unique-rector-short-class-name.php
35+
3236
-
3337
name: 'Help and Version'
3438
run:

config/set/php85.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Rector\Php85\Rector\Class_\SleepToSerializeRector;
1212
use Rector\Php85\Rector\Class_\WakeupToUnserializeRector;
1313
use Rector\Php85\Rector\ClassMethod\NullDebugInfoReturnRector;
14-
use Rector\Php85\Rector\Const_\DeprecatedAnnotationToDeprecatedAttributeRector;
14+
use Rector\Php85\Rector\Const_\ConstAndTraitDeprecatedAttributeRector;
1515
use Rector\Php85\Rector\FuncCall\ArrayKeyExistsNullToEmptyStringRector;
1616
use Rector\Php85\Rector\FuncCall\ChrArgModuloRector;
1717
use Rector\Php85\Rector\FuncCall\OrdSingleByteRector;
@@ -35,7 +35,7 @@
3535
ArrayFirstLastRector::class,
3636
RemoveFinfoBufferContextArgRector::class,
3737
NullDebugInfoReturnRector::class,
38-
DeprecatedAnnotationToDeprecatedAttributeRector::class,
38+
ConstAndTraitDeprecatedAttributeRector::class,
3939
ColonAfterSwitchCaseRector::class,
4040
ArrayKeyExistsNullToEmptyStringRector::class,
4141
ChrArgModuloRector::class,

rules-tests/Php85/Rector/Const_/DeprecatedAnnotationToDeprecatedAttributeRector/DeprecatedAnnotationToDeprecatedAttributeRectorTest.php renamed to rules-tests/Php85/Rector/Const_/ConstAndTraitDeprecatedAttributeRector/ConstAndTraitDeprecatedAttributeRectorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
declare(strict_types=1);
44

5-
namespace Rector\Tests\Php85\Rector\Const_\DeprecatedAnnotationToDeprecatedAttributeRector;
5+
namespace Rector\Tests\Php85\Rector\Const_\ConstAndTraitDeprecatedAttributeRector;
66

77
use Iterator;
88
use PHPUnit\Framework\Attributes\DataProvider;
99
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
1010

11-
final class DeprecatedAnnotationToDeprecatedAttributeRectorTest extends AbstractRectorTestCase
11+
final class ConstAndTraitDeprecatedAttributeRectorTest extends AbstractRectorTestCase
1212
{
1313
#[DataProvider('provideData')]
1414
public function test(string $filePath): void

rules-tests/Php85/Rector/Const_/DeprecatedAnnotationToDeprecatedAttributeRector/Fixture/basic.php.inc renamed to rules-tests/Php85/Rector/Const_/ConstAndTraitDeprecatedAttributeRector/Fixture/basic.php.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Rector\Tests\Php85\Rector\Const_\DeprecatedAnnotationToDeprecatedAttributeRector;
3+
namespace Rector\Tests\Php85\Rector\Const_\ConstAndTraitDeprecatedAttributeRector;
44

55
/**
66
* @deprecated use new constant
@@ -16,7 +16,7 @@ const UNUSED = 'ignored';
1616
-----
1717
<?php
1818

19-
namespace Rector\Tests\Php85\Rector\Const_\DeprecatedAnnotationToDeprecatedAttributeRector;
19+
namespace Rector\Tests\Php85\Rector\Const_\ConstAndTraitDeprecatedAttributeRector;
2020

2121
#[\Deprecated(message: 'use new constant')]
2222
const CONSTANT = 'some reason';
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php85\Rector\Const_\ConstAndTraitDeprecatedAttributeRector;
4+
5+
/**
6+
* @deprecated avoid using
7+
*/
8+
trait HandleTrait
9+
{
10+
}
11+
12+
?>
13+
-----
14+
<?php
15+
16+
namespace Rector\Tests\Php85\Rector\Const_\ConstAndTraitDeprecatedAttributeRector;
17+
18+
#[\Deprecated(message: 'avoid using')]
19+
trait HandleTrait
20+
{
21+
}
22+
23+
?>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php85\Rector\Const_\ConstAndTraitDeprecatedAttributeRector;
4+
5+
final class SkipOnClassConst
6+
{
7+
/**
8+
* @deprecated use new constant
9+
*/
10+
public const CONSTANT = 'some reason';
11+
12+
/**
13+
* @deprecated 2.0.0 do not use
14+
*/
15+
public const UNUSED = 'ignored';
16+
}

rules-tests/Php85/Rector/Const_/DeprecatedAnnotationToDeprecatedAttributeRector/config/configured_rule.php renamed to rules-tests/Php85/Rector/Const_/ConstAndTraitDeprecatedAttributeRector/config/configured_rule.php

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

55
use Rector\Config\RectorConfig;
6-
use Rector\Php85\Rector\Const_\DeprecatedAnnotationToDeprecatedAttributeRector;
6+
use Rector\Php85\Rector\Const_\ConstAndTraitDeprecatedAttributeRector;
77
use Rector\ValueObject\PhpVersion;
88

99
return static function (RectorConfig $rectorConfig): void {
10-
$rectorConfig->rule(DeprecatedAnnotationToDeprecatedAttributeRector::class);
10+
$rectorConfig->rule(ConstAndTraitDeprecatedAttributeRector::class);
1111
$rectorConfig->phpVersion(PhpVersion::PHP_85);
1212
};

rules/Php85/Rector/Const_/DeprecatedAnnotationToDeprecatedAttributeRector.php renamed to rules/Php85/Rector/Const_/ConstAndTraitDeprecatedAttributeRector.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Stmt\Const_;
9+
use PhpParser\Node\Stmt\Trait_;
910
use Rector\PhpAttribute\DeprecatedAnnotationToDeprecatedAttributeConverter;
1011
use Rector\Rector\AbstractRector;
11-
use Rector\ValueObject\PhpVersionFeature;
12+
use Rector\ValueObject\PhpVersion;
1213
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
1314
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1415
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
1516

1617
/**
17-
* @see \Rector\Tests\Php85\Rector\Const_\DeprecatedAnnotationToDeprecatedAttributeRector\DeprecatedAnnotationToDeprecatedAttributeRectorTest
18+
* @see https://wiki.php.net/rfc/attributes-on-constants
19+
* @see https://wiki.php.net/rfc/deprecated_traits
20+
*
21+
* @see \Rector\Tests\Php85\Rector\Const_\ConstAndTraitDeprecatedAttributeRector\ConstAndTraitDeprecatedAttributeRectorTest
1822
*/
19-
final class DeprecatedAnnotationToDeprecatedAttributeRector extends AbstractRector implements MinPhpVersionInterface
23+
final class ConstAndTraitDeprecatedAttributeRector extends AbstractRector implements MinPhpVersionInterface
2024
{
2125
public function __construct(
2226
private readonly DeprecatedAnnotationToDeprecatedAttributeConverter $deprecatedAnnotationToDeprecatedAttributeConverter,
@@ -25,7 +29,7 @@ public function __construct(
2529

2630
public function getRuleDefinition(): RuleDefinition
2731
{
28-
return new RuleDefinition('Change @deprecated annotation to Deprecated attribute', [
32+
return new RuleDefinition('Change @deprecated annotation to #[Deprecated] attribute for constants', [
2933
new CodeSample(
3034
<<<'CODE_SAMPLE'
3135
/**
@@ -44,11 +48,11 @@ public function getRuleDefinition(): RuleDefinition
4448

4549
public function getNodeTypes(): array
4650
{
47-
return [Const_::class];
51+
return [Const_::class, Trait_::class];
4852
}
4953

5054
/**
51-
* @param Const_ $node
55+
* @param Const_|Trait_ $node
5256
*/
5357
public function refactor(Node $node): ?Node
5458
{
@@ -57,6 +61,6 @@ public function refactor(Node $node): ?Node
5761

5862
public function provideMinPhpVersion(): int
5963
{
60-
return PhpVersionFeature::DEPRECATED_ATTRIBUTE_ON_CONSTANT;
64+
return PhpVersion::PHP_85;
6165
}
6266
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
// this is part of downgrade build
4+
5+
declare(strict_types=1);
6+
7+
use Rector\Console\ExitCode;
8+
use Rector\Scripts\Finder\RectorClassFinder;
9+
10+
require __DIR__ . '/../vendor/autoload.php';
11+
12+
$rectorClassFinder = new RectorClassFinder();
13+
$rectorClassNames = $rectorClassFinder->find([
14+
__DIR__ . '/../rules',
15+
__DIR__ . '/../vendor/rector/rector-doctrine',
16+
__DIR__ . '/../vendor/rector/rector-phpunit',
17+
__DIR__ . '/../vendor/rector/rector-symfony',
18+
__DIR__ . '/../vendor/rector/rector-downgrade-php',
19+
]);
20+
21+
/**
22+
* @param string[] $classNames
23+
* @return string[]
24+
*/
25+
function getShortClassNames(array $classNames): array
26+
{
27+
$shortClassNames = [];
28+
foreach ($classNames as $className) {
29+
$shortClassNames[] = substr($className, strrpos($className, '\\') + 1);
30+
}
31+
32+
return $shortClassNames;
33+
}
34+
35+
/**
36+
* @param string[] $shortClassNames
37+
* @return string[]
38+
*/
39+
function filterDuplicatedValues(array $shortClassNames): array
40+
{
41+
$classNamesToCounts = array_count_values($shortClassNames);
42+
$duplicatedShortClassNames = [];
43+
44+
foreach ($classNamesToCounts as $className => $count) {
45+
if ($count === 1) {
46+
// unique, skip
47+
continue;
48+
}
49+
50+
$duplicatedShortClassNames[] = $className;
51+
}
52+
53+
return $duplicatedShortClassNames;
54+
}
55+
56+
$shortClassNames = getShortClassNames($rectorClassNames);
57+
$duplicatedShortClassNames = filterDuplicatedValues($shortClassNames);
58+
59+
if ($duplicatedShortClassNames === []) {
60+
echo "All Rector class names are unique!\n";
61+
exit(ExitCode::SUCCESS);
62+
}
63+
64+
echo "The following Rector class names are duplicated:\n";
65+
foreach ($duplicatedShortClassNames as $duplicatedShortClassName) {
66+
echo sprintf("- %s\n", $duplicatedShortClassName);
67+
}
68+
69+
exit(ExitCode::FAILURE);

src/PhpAttribute/DeprecatedAnnotationToDeprecatedAttributeConverter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PhpParser\Node\Stmt\ClassMethod;
1717
use PhpParser\Node\Stmt\Const_;
1818
use PhpParser\Node\Stmt\Function_;
19+
use PhpParser\Node\Stmt\Trait_;
1920
use PHPStan\PhpDocParser\Ast\PhpDoc\DeprecatedTagValueNode;
2021
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
2122
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
@@ -47,7 +48,7 @@ public function __construct(
4748
) {
4849
}
4950

50-
public function convert(ClassConst|Function_|ClassMethod|Const_ $node): ?Node
51+
public function convert(ClassConst|Function_|ClassMethod|Const_|Trait_ $node): ?Node
5152
{
5253
$hasChanged = false;
5354
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);

0 commit comments

Comments
 (0)