Skip to content
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

[Ignore] Running tests for attributes while waiting for merge #276

Closed
wants to merge 9 commits into from
1 change: 1 addition & 0 deletions config/drupal-10/drupal-10-all-deprecations.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
$rectorConfig->sets([
Drupal10SetList::DRUPAL_100,
Drupal10SetList::DRUPAL_101,
Drupal10SetList::DRUPAL_102,
]);

$rectorConfig->bootstrapFiles([
Expand Down
11 changes: 11 additions & 0 deletions config/drupal-10/drupal-10.2-deprecations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use DrupalRector\Drupal10\Rector\Deprecation\ActionAnnotationToAttributeRector;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
// @see https://www.drupal.org/node/3395575
$rectorConfig->rule(ActionAnnotationToAttributeRector::class);
};
190 changes: 190 additions & 0 deletions src/Drupal10/Rector/Deprecation/ActionAnnotationToAttributeRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
<?php

declare(strict_types=1);

namespace DrupalRector\Drupal10\Rector\Deprecation;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use Rector\BetterPhpDocParser\PhpDoc\ArrayItemNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\PhpDocInfo\TokenIteratorFactory;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
use Rector\BetterPhpDocParser\PhpDocParser\StaticDoctrineAnnotationParser\ArrayParser;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersion;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use RectorPrefix202310\PHPUnit\Framework\Attributes\Ticket;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @changelog https://docs.phpunit.de/en/10.0/annotations.html#ticket
*
* @see \Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\Class_\TicketAnnotationToAttributeRector\TicketAnnotationToAttributeRectorTest
*/
final class ActionAnnotationToAttributeRector extends AbstractRector implements MinPhpVersionInterface
{
/**
* @readonly
*
* @var \Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover
*/
private $phpDocTagRemover;
/**
* @readonly
*
* @var \Rector\Comments\NodeDocBlock\DocBlockUpdater
*/
private $docBlockUpdater;
/**
* @readonly
*
* @var \Rector\BetterPhpDocParser\PhpDocParser\StaticDoctrineAnnotationParser\ArrayParser
*/
private $arrayParser;
/**
* @readonly
*
* @var \Rector\BetterPhpDocParser\PhpDocInfo\TokenIteratorFactory
*/
private $tokenIteratorFactory;
/**
* @readonly
*
* @var \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory
*/
private $phpDocInfoFactory;

public function __construct(PhpDocTagRemover $phpDocTagRemover, DocBlockUpdater $docBlockUpdater, PhpDocInfoFactory $phpDocInfoFactory, ArrayParser $arrayParser, TokenIteratorFactory $tokenIteratorFactory)
{
$this->phpDocTagRemover = $phpDocTagRemover;
$this->docBlockUpdater = $docBlockUpdater;
$this->phpDocInfoFactory = $phpDocInfoFactory;
$this->arrayParser = $arrayParser;
$this->tokenIteratorFactory = $tokenIteratorFactory;
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change annotations with value to attribute', [new CodeSample(<<<'CODE_SAMPLE'

namespace Drupal\Core\Action\Plugin\Action;

use Drupal\Core\Session\AccountInterface;

/**
* Publishes an entity.
*
* @Action(
* id = "entity:publish_action",
* action_label = @Translation("Publish"),
* deriver = "Drupal\Core\Action\Plugin\Action\Derivative\EntityPublishedActionDeriver",
* )
*/
class PublishAction extends EntityActionBase {
CODE_SAMPLE
, <<<'CODE_SAMPLE'

namespace Drupal\Core\Action\Plugin\Action;

use Drupal\Core\Action\Plugin\Action\Derivative\EntityPublishedActionDeriver;
use Drupal\Core\Action\Attribute\Action;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
* Publishes an entity.
*/
#[Action(
id: 'entity:publish_action',
action_label: new TranslatableMarkup('Publish'),
deriver: EntityPublishedActionDeriver::class
)]
class PublishAction extends EntityActionBase {
CODE_SAMPLE
)]);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Class_::class];
}

public function provideMinPhpVersion(): int
{
return PhpVersion::PHP_81;
}

/**
* @param Class_|ClassMethod $node
*/
public function refactor(Node $node): ?Node
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
if (!$phpDocInfo instanceof PhpDocInfo) {
return null;
}
/** @var PhpDocTagNode[] $tagsByName */
$tagsByName = $phpDocInfo->getTagsByName('Action');
if ($tagsByName === []) {
return null;
}
$hasChanged = \false;
foreach ($tagsByName as $valueNode) {
if (!$valueNode->value instanceof GenericTagValueNode) {
continue;
}
$stringValue = $valueNode->value->value;
$stringValue = '{'.trim($stringValue, '()').'}';
$tokenIterator = $this->tokenIteratorFactory->create($stringValue);
$data = $this->arrayParser->parseCurlyArray($tokenIterator, $node);
$attribute = $this->createAttribute($data);
$node->attrGroups[] = new AttributeGroup([$attribute]);
// cleanup
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $valueNode);
$hasChanged = \true;
}
if ($hasChanged) {
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);

return $node;
}

return null;
}

/**
* @param array|ArrayItemNode[] $parsedArgs
*
* @return \PhpParser\Node\Attribute
*/
private function createAttribute(array $parsedArgs): Attribute
{
$fullyQualified = new FullyQualified('Drupal\Core\Action\Attribute\Action');
$args = [];
foreach ($parsedArgs as $value) {
if ($value->key === 'action_label') {
$arg = new Node\Expr\New_(new Node\Name('Drupal\Core\StringTranslation\TranslatableMarkup'), [new Arg(new String_($value->value->values[0]->value->value))]);
} else {
$arg = new String_($value->value->value);
}
$args[] = new Arg($arg, \false, \false, [], new Node\Identifier($value->key));
}

return new Attribute($fullyQualified, $args);
}
}
1 change: 1 addition & 0 deletions src/Set/Drupal10SetList.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ final class Drupal10SetList implements SetListInterface
public const DRUPAL_10 = __DIR__.'/../../config/drupal-10/drupal-10-all-deprecations.php';
public const DRUPAL_100 = __DIR__.'/../../config/drupal-10/drupal-10.0-deprecations.php';
public const DRUPAL_101 = __DIR__.'/../../config/drupal-10/drupal-10.1-deprecations.php';
public const DRUPAL_102 = __DIR__.'/../../config/drupal-10/drupal-10.2-deprecations.php';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace DrupalRector\Tests\Drupal10\Rector\Deprecation\ActionAnnotationToAttributeRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

class ActionAnnotationToAttributeRectorTest extends AbstractRectorTestCase
{
/**
* @covers ::refactor
*
* @dataProvider provideData
*/
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

/**
* @return Iterator<<string>>
*/
public static function provideData(): \Iterator
{
return self::yieldFilesFromDirectory(__DIR__.'/fixture');
}

public function provideConfigFilePath(): string
{
// must be implemented
return __DIR__.'/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use DrupalRector\Drupal10\Rector\Deprecation\ActionAnnotationToAttributeRector;
use DrupalRector\Tests\Rector\Deprecation\DeprecationBase;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
DeprecationBase::addClass(ActionAnnotationToAttributeRector::class, $rectorConfig, false);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* A basic example action that does nothing.
*
* @Action(
* id = "action_example_basic_action",
* action_label = @Translation("Action Example: A basic example action that does nothing"),
* type = "system"
* )
*/
class BasicExample extends ActionBase implements ContainerFactoryPluginInterface {

}
?>

-----
<?php

/**
* A basic example action that does nothing.
*/
#[\Drupal\Core\Action\Attribute\Action(id: 'action_example_basic_action', action_label: new Drupal\Core\StringTranslation\TranslatableMarkup('Action Example: A basic example action that does nothing'), type: 'system')]
class BasicExample extends ActionBase implements ContainerFactoryPluginInterface {

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

/**
* A basic example action that does nothing.
*
* @Action(
* id = "action_example_basic_action",
* action_label = @Translation("Action Example: A basic example action that does nothing"),
* type = "system"
* )
*/
#[\Drupal\Core\Action\Attribute\Action(id: 'action_example_basic_action', action_label: new Drupal\Core\StringTranslation\TranslatableMarkup('Action Example: A basic example action that does nothing'), type: 'system')]
class BasicExample extends ActionBase implements ContainerFactoryPluginInterface {

}
?>

-----
<?php

/**
* A basic example action that does nothing.
*/
#[\Drupal\Core\Action\Attribute\Action(id: 'action_example_basic_action', action_label: new Drupal\Core\StringTranslation\TranslatableMarkup('Action Example: A basic example action that does nothing'), type: 'system')]
class BasicExample extends ActionBase implements ContainerFactoryPluginInterface {

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

/**
* A basic example action that does nothing.
*
* @Action(
* id = "action_example_basic_action",
* action_label = @Translation("Action Example: A basic example action that does nothing and has an @argument", context = "Validation", arguments = {"@argument" = "Argument"}),
* type = "system"
* )
*/
class BasicExample extends ActionBase implements ContainerFactoryPluginInterface {

}
?>

-----
<?php

/**
* A basic example action that does nothing.
*/
#[\Drupal\Core\Action\Attribute\Action(id: 'action_example_basic_action', action_label: new Drupal\Core\StringTranslation\TranslatableMarkup('Action Example: A basic example action that does nothing', ['@argument' => 'Argument'], ['context' => 'Validation']), type: 'system')]
class BasicExample extends ActionBase implements ContainerFactoryPluginInterface {

}
?>
Loading