8000 [PHP84] Deprecated annotation to Deprecated attribute by peterfox · Pull Request #6923 · rectorphp/rector-src · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[PHP84] Deprecated annotation to Deprecated attribute #6923

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

Merged
merged 11 commits into from
May 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/set/php84.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php84\Rector\Class_\DeprecatedAnnotationToDeprecatedAttributeRector;
use Rector\Php84\Rector\FuncCall\AddEscapeArgumentRector;
use Rector\Php84\Rector\FuncCall\RoundingModeEnumRector;
use Rector\Php84\Rector\MethodCall\NewMethodCallWithoutParenthesesRector;
Expand All @@ -15,6 +16,7 @@
RoundingModeEnumRector::class,
AddEscapeArgumentRector::class,
NewMethodCallWithoutParenthesesRector::class,
DeprecatedAnnotationToDeprecatedAttributeRector::class,
]
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php84\Rector\Class_\DeprecatedAnnotationToDeprecatedAttributeRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class DeprecatedAnnotationToDeprecatedAttributeRectorTest 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';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Rector\Tests\Php84\Rector\Class_\DeprecatedAnnotationToDeprecatedAttributeRector;

final class Fixture
{
/**
* @deprecated use new constant.
*/
public const CONSTANT = 'some reason.';

/**
* @deprecated 1.0.1 use new method.
*/
public function run()
{
}
}

?>
-----
<?php

namespace Rector\Tests\Php84\Rector\Class_\DeprecatedAnnotationToDeprecatedAttributeRector;

final class Fixture
{
#[\Deprecated(message: 'use new constant.')]
public const CONSTANT = 'some reason.';

#[\Deprecated(message: 'use new method.', since: '1.0.1')]
public function run()
{
}
}

?>
8000
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Rector\Tests\Php84\Rector\Class_\DeprecatedAnnotationToDeprecatedAttributeRector;

final class GracefullyRemovesAnnotation
{
/**
* @deprecated some reason.
* @see https://getrector.com
*/
public const FOO = 'foo';
}

?>
-----
<?php

namespace Rector\Tests\Php84\Rector\Class_\DeprecatedAnnotationToDeprecatedAttributeRector;

final class GracefullyRemovesAnnotation
{
/**
* @see https://getrector.com
*/
#[\Deprecated(message: 'some reason.')]
public const FOO = 'foo';
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Rector\Tests\Php84\Rector\Class_\DeprecatedAnnotationToDeprecatedAttributeRector;

/**
* @deprecated some reason.
*/
function works_with_functions()
{
}

?>
-----
<?php

namespace Rector\Tests\Php84\Rector\Class_\DeprecatedAnnotationToDeprecatedAttributeRector;

#[\Deprecated(message: 'some reason.')]
function works_with_functions()
{
}

?>
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\Class_\DeprecatedAnnotationToDeprecatedAttributeRector;
use Rector\ValueObject\PhpVersion;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(DeprecatedAnnotationToDeprecatedAttributeRector::class);

$rectorConfig->phpVersion(PhpVersion::PHP_84);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?php

declare(strict_types=1);

namespace Rector\Php84\Rector\Class_;

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Stmt\ClassConst;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PHPStan\PhpDocParser\Ast\PhpDoc\DeprecatedTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\PhpAttribute\NodeFactory\PhpAttributeGroupFactory;
use Rector\Rector\AbstractRector;
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\Class_\DeprecatedAnnotationToDeprecatedAttributeRector\DeprecatedAnnotationToDeprecatedAttributeRectorTest
*/
final class DeprecatedAnnotationToDeprecatedAttributeRector extends AbstractRector implements MinPhpVersionInterface
{
/**
* @see https://regex101.com/r/qNytVk/1
* @var string
*/
private const VERSION_MATCH_REGEX = '/^(?:(\d+\.\d+\.\d+)\s+)?(.*)$/';

public function __construct(
private readonly PhpDocTagRemover $phpDocTagRemover,
private readonly PhpAttributeGroupFactory $phpAttributeGroupFactory,
private readonly DocBlockUpdater $docBlockUpdater,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change @deprecated annotation to Deprecated attribute', [
new CodeSample(
<<<'CODE_SAMPLE'
/**
* @deprecated 1.0.0 Use SomeOtherClass instead
*/
class SomeClass
{
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
#[\Deprecated(message: 'Use SomeOtherClass instead', since: '1.0.0')]
class SomeClass
{
}
CODE_SAMPLE
),
new CodeSample(
<<<'CODE_SAMPLE'
/**
* @deprecated 1.0.0 Use SomeOtherFunction instead
*/
function someFunction()
{
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
#[\Deprecated(message: 'Use SomeOtherFunction instead', since: '1.0.0')]
function someFunction()
{
}
CODE_SAMPLE
),
]);
}

public function getNodeTypes(): array
{
return [Function_::class, ClassMethod::class, ClassConst::class];
}

/**
* @param ClassConst|Function_|ClassMethod $node
*/
public function refactor(Node $node): ?Node
{
$hasChanged = false;
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
if ($phpDocInfo instanceof PhpDocInfo) {
$deprecatedAttributeGroup = $this->handleDeprecated($phpDocInfo);
if ($deprecatedAttributeGroup instanceof AttributeGroup) {
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
$node->attrGroups = array_merge($node->attrGroups, [$deprecatedAttributeGroup]);
$this->removeDeprecatedAnnotations($phpDocInfo);
$hasChanged = true;
}
}

return $hasChanged ? $node : null;
}

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::DEPRECATED_ATTRIBUTE;
}

private function handleDeprecated(PhpDocInfo $phpDocInfo): ?AttributeGroup
{
$attributeGroup = null;
$desiredTagValueNodes = $phpDocInfo->getTagsByName('deprecated');
foreach ($desiredTagValueNodes as $desiredTagValueNode) {
if (! $desiredTagValueNode->value instanceof DeprecatedTagValueNode) {
continue;
}

$attributeGroup = $this->createAttributeGroup($desiredTagValueNode->value->description);
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $desiredTagValueNode);

break;
}

return $attributeGroup;
}

private function createAttributeGroup(string $annotationValue): AttributeGroup
{
$matches = Strings::match($annotationValue, self::VERSION_MATCH_REGEX);

$since = $matches[1] ?? null;
$message = $matches[2] ?? null;

return $this->phpAttributeGroupFactory->createFromClassWithItems('Deprecated', array_filter([
'message' => $message,
'since' => $since,
]));
}

private function removeDeprecatedAnnotations(PhpDocInfo $phpDocInfo): bool
{
$hasChanged = false;

$desiredTagValueNodes = $phpDocInfo->getTagsByName('deprecated');
foreach ($desiredTagValueNodes as $desiredTagValueNode) {
if (! $desiredTagValueNode->value instanceof GenericTagValueNode) {
continue;
}

$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $desiredTagValueNode);
$hasChanged = true;
}

return $hasChanged;
}
}
6 changes: 6 additions & 0 deletions src/ValueObject/PhpVersionFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -732,4 +732,10 @@ final class PhpVersionFeature
* @var int
*/
public const DEPRECATE_GET_CLASS_WITHOUT_ARGS = PhpVersion::PHP_83;

/**
* @see https://wiki.php.net/rfc/deprecated_attribute
* @var int
*/
public const DEPRECATED_ATTRIBUTE = PhpVersion::PHP_84;
}
0