Skip to content
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
8 changes: 7 additions & 1 deletion config/set/php85.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node\Expr\Cast\Int_;
use PhpParser\Node\Expr\Cast\String_;
use Rector\Config\RectorConfig;
use Rector\Php85\Rector\Const_\DeprecatedAnnotationToDeprecatedAttributeRector;
use Rector\Php85\Rector\ArrayDimFetch\ArrayFirstLastRector;
use Rector\Php85\Rector\ClassMethod\NullDebugInfoReturnRector;
use Rector\Php85\Rector\FuncCall\RemoveFinfoBufferContextArgRector;
Expand All @@ -22,7 +23,12 @@

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules(
[ArrayFirstLastRector::class, RemoveFinfoBufferContextArgRector::class, NullDebugInfoReturnRector::class]
[
ArrayFirstLastRector::class,
RemoveFinfoBufferContextArgRector::class,
NullDebugInfoReturnRector::class,
DeprecatedAnnotationToDeprecatedAttributeRector::class,
]
);

$rectorConfig->ruleWithConfiguration(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php85\Rector\Const_\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,27 @@
<?php

namespace Rector\Tests\Php85\Rector\Const_\DeprecatedAnnotationToDeprecatedAttributeRector;

/**
* @deprecated use new constant
*/
const CONSTANT = 'some reason';

/**
* @deprecated 2.0.0 do not use
*/
const UNUSED = 'ignored';

?>
-----
<?php

namespace Rector\Tests\Php85\Rector\Const_\DeprecatedAnnotationToDeprecatedAttributeRector;

#[\Deprecated(message: 'use new constant')]
const CONSTANT = 'some reason';

#[\Deprecated(message: 'do not use', since: '2.0.0')]
const UNUSED = 'ignored';

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

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(DeprecatedAnnotationToDeprecatedAttributeRector::class);
$rectorConfig->phpVersion(PhpVersion::PHP_85);
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,11 @@

namespace Rector\Php84\Rector\Class_;

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
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\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpAttribute\NodeFactory\PhpAttributeGroupFactory;
use Rector\PhpAttribute\DeprecatedAnnotationToDeprecatedAttributeConverter;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
Expand All @@ -34,23 +20,8 @@
*/
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+)?(.*)$/';

/**
* @see https://regex101.com/r/SVDPOB/1
* @var string
*/
private const START_STAR_SPACED_REGEX = '#^ *\*#ms';

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

Expand Down Expand Up @@ -104,88 +75,11 @@ public function getNodeTypes(): array
*/
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;
return $this->converter->convert($node);
}

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);

if ($matches === null) {
$annotationValue = Strings::replace($annotationValue, self::START_STAR_SPACED_REGEX, '');

return new AttributeGroup([
new Attribute(
new FullyQualified('Deprecated'),
[new Arg(
value: new String_($annotationValue, [
AttributeKey::KIND => String_::KIND_NOWDOC,
AttributeKey::DOC_LABEL => 'TXT',
]),
name: new Identifier('message')
)]
),
]);
}

$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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace Rector\Php85\Rector\Const_;

use PhpParser\Node;
use PhpParser\Node\Stmt\Const_;
use Rector\PhpAttribute\DeprecatedAnnotationToDeprecatedAttributeConverter;
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\Php85\Rector\Const_\DeprecatedAnnotationToDeprecatedAttributeRector\DeprecatedAnnotationToDeprecatedAttributeRectorTest
*/
final class DeprecatedAnnotationToDeprecatedAttributeRector extends AbstractRector implements MinPhpVersionInterface
{

public function __construct(
private readonly DeprecatedAnnotationToDeprecatedAttributeConverter $converter,
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change @deprecated annotation to Deprecated attribute', [
new CodeSample(
<<<'CODE_SAMPLE'
/**
* @deprecated 1.0.0 Use SomeOtherConstant instead
*/
const SomeConstant = 'irrelevant';
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
#[\Deprecated(message: 'Use SomeOtherConstant instead', since: '1.0.0')]
const SomeConstant = 'irrelevant';
CODE_SAMPLE
),
]);
}

public function getNodeTypes(): array
{
return [Const_::class];
}

/**
* @param Const_ $node
*/
public function refactor(Node $node): ?Node
{
return $this->converter->convert($node);
}

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