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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ release-by-release.
- `HookConvertRector` no longer imports the hook class into itself when `importNames()` is on, which was a fatal "name already in use".
- Files with test classes no longer abort with "Call to a member function getFunctionCallStackWithParameters() on null".
- The Drupal 8 and 9 rules and `HookConvertRector` no longer use Rector APIs removed in 2.8.0.
- **`AnnotationToAttributeRector` on Rector dev-main:** it no longer depends on Rector's `ArrayParser`, which was removed in [rectorphp/rector-src#8881](https://github.com/rectorphp/rector-src/pull/8881) and made the rule fail to load. Annotations Rector cannot parse, such as arguments starting on the line after the tag, are now left alone instead of being converted to a broken attribute.

### Removed

Expand Down
25 changes: 11 additions & 14 deletions src/Drupal10/Rector/Deprecation/AnnotationToAttributeRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
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\PhpAttribute\AnnotationToAttributeMapper;
use Rector\ValueObject\PhpVersion;
Expand All @@ -47,25 +45,19 @@ final class AnnotationToAttributeRector extends AbstractDrupalCoreRector impleme

private DocBlockUpdater $docBlockUpdater;

private ArrayParser $arrayParser;

private TokenIteratorFactory $tokenIteratorFactory;

private PhpDocInfoFactory $phpDocInfoFactory;

/**
* @var AnnotationToAttributeMapper
*/
private AnnotationToAttributeMapper $annotationToAttributeMapper;

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

Expand Down Expand Up @@ -206,11 +198,16 @@ protected function refactorWithConfiguration(Node $node, VersionedConfigurationI

if ($hasAttribute === false) {
if ($valueNode->value instanceof GenericTagValueNode) {
$stringValue = $valueNode->value->value;
$stringValue = '{'.trim($stringValue, '()').'}';
$tokenIterator = $this->tokenIteratorFactory->create($stringValue);
$data = $this->arrayParser->parseCurlyArray($tokenIterator, $node);
$attribute = $this->createAttribute($configuration->getAttributeClass(), $data);
// Rector only leaves a GenericTagValueNode for a tag it could
// not parse as a Doctrine annotation. The only shape we can
// convert reliably is the argument-less `@Action`; anything
// else (e.g. arguments starting on the next line) is left
// untouched, annotation included, rather than guessed at.
if (trim($valueNode->value->value, " \t\r\n*()") !== '') {
continue;
}

$attribute = $this->createAttribute($configuration->getAttributeClass(), []);
$node->attrGroups[] = new AttributeGroup([$attribute]);
$attributeAdded = \true;
} elseif ($valueNode->value instanceof DoctrineAnnotationTagValueNode) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* An action annotation without arguments.
*
* @Action
*/
class NoArgumentsExample extends ActionBase {

}
?>
-----
<?php

/**
* An action annotation without arguments.
*/
#[\Drupal\Core\Action\Attribute\Action]
class NoArgumentsExample extends ActionBase {

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

/**
* Arguments that do not start on the tag line are not parsed as an annotation.
*
* @Action
* (
* id = "next_line",
* )
*/
class NextLineExample extends ActionBase {

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

/**
* An action annotation without arguments.
*
* @Action
*/
class NoArgumentsExample extends ActionBase {

}
?>
-----
<?php

/**
* An action annotation without arguments.
*
* @Action
*/
#[\Drupal\Core\Action\Attribute\Action]
class NoArgumentsExample extends ActionBase {

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

/**
* Arguments that do not start on the tag line are not parsed as an annotation.
*
* @Action
* (
* id = "next_line",
* )
*/
class NextLineExample extends ActionBase {

}
?>
Loading