2021-11-20 15:31:12 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types=1);
|
2022-06-06 16:43:29 +00:00
|
|
|
namespace RectorPrefix20220606\Rector\PhpAttribute\AnnotationToAttributeMapper;
|
2021-11-20 15:31:12 +00:00
|
|
|
|
2022-06-06 16:43:29 +00:00
|
|
|
use RectorPrefix20220606\PhpParser\Node\Expr\Array_;
|
|
|
|
use RectorPrefix20220606\PhpParser\Node\Expr\New_;
|
|
|
|
use RectorPrefix20220606\PhpParser\Node\Name;
|
|
|
|
use RectorPrefix20220606\Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
|
|
|
|
use RectorPrefix20220606\Rector\Core\Exception\ShouldNotHappenException;
|
|
|
|
use RectorPrefix20220606\Rector\Core\Php\PhpVersionProvider;
|
|
|
|
use RectorPrefix20220606\Rector\Core\ValueObject\PhpVersionFeature;
|
|
|
|
use RectorPrefix20220606\Rector\PhpAttribute\AnnotationToAttributeMapper;
|
|
|
|
use RectorPrefix20220606\Rector\PhpAttribute\AttributeArrayNameInliner;
|
|
|
|
use RectorPrefix20220606\Rector\PhpAttribute\Contract\AnnotationToAttributeMapperInterface;
|
|
|
|
use RectorPrefix20220606\Rector\PhpAttribute\Exception\InvalidNestedAttributeException;
|
|
|
|
use RectorPrefix20220606\Rector\PhpAttribute\UnwrapableAnnotationAnalyzer;
|
2022-06-06 06:00:07 +00:00
|
|
|
use RectorPrefix20220606\Symfony\Contracts\Service\Attribute\Required;
|
2021-11-20 15:31:12 +00:00
|
|
|
/**
|
|
|
|
* @implements AnnotationToAttributeMapperInterface<DoctrineAnnotationTagValueNode>
|
|
|
|
*/
|
2022-06-06 16:43:29 +00:00
|
|
|
final class DoctrineAnnotationAnnotationToAttributeMapper implements AnnotationToAttributeMapperInterface
|
2021-11-20 15:31:12 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Rector\PhpAttribute\AnnotationToAttributeMapper
|
|
|
|
*/
|
|
|
|
private $annotationToAttributeMapper;
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-11-20 15:31:12 +00:00
|
|
|
* @var \Rector\Core\Php\PhpVersionProvider
|
|
|
|
*/
|
|
|
|
private $phpVersionProvider;
|
2021-11-20 22:45:17 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2022-02-05 10:39:52 +00:00
|
|
|
* @var \Rector\PhpAttribute\UnwrapableAnnotationAnalyzer
|
2021-11-20 22:45:17 +00:00
|
|
|
*/
|
2022-02-05 10:39:52 +00:00
|
|
|
private $unwrapableAnnotationAnalyzer;
|
2021-12-19 01:17:40 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
2022-02-05 10:39:52 +00:00
|
|
|
* @var \Rector\PhpAttribute\AttributeArrayNameInliner
|
2021-12-19 01:17:40 +00:00
|
|
|
*/
|
2022-02-05 10:39:52 +00:00
|
|
|
private $attributeArrayNameInliner;
|
2022-06-06 16:43:29 +00:00
|
|
|
public function __construct(PhpVersionProvider $phpVersionProvider, UnwrapableAnnotationAnalyzer $unwrapableAnnotationAnalyzer, AttributeArrayNameInliner $attributeArrayNameInliner)
|
2021-11-20 15:31:12 +00:00
|
|
|
{
|
|
|
|
$this->phpVersionProvider = $phpVersionProvider;
|
2021-12-19 01:17:40 +00:00
|
|
|
$this->unwrapableAnnotationAnalyzer = $unwrapableAnnotationAnalyzer;
|
2022-02-05 10:39:52 +00:00
|
|
|
$this->attributeArrayNameInliner = $attributeArrayNameInliner;
|
2021-11-20 15:31:12 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Avoid circular reference
|
|
|
|
* @required
|
|
|
|
*/
|
2022-06-06 16:43:29 +00:00
|
|
|
public function autowire(AnnotationToAttributeMapper $annotationToAttributeMapper) : void
|
2021-11-20 15:31:12 +00:00
|
|
|
{
|
|
|
|
$this->annotationToAttributeMapper = $annotationToAttributeMapper;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
|
|
|
public function isCandidate($value) : bool
|
|
|
|
{
|
2022-06-06 16:43:29 +00:00
|
|
|
if (!$value instanceof DoctrineAnnotationTagValueNode) {
|
2021-12-19 01:17:40 +00:00
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
return !$this->unwrapableAnnotationAnalyzer->areUnwrappable([$value]);
|
2021-11-20 15:31:12 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param DoctrineAnnotationTagValueNode $value
|
|
|
|
*/
|
2022-06-06 16:43:29 +00:00
|
|
|
public function map($value) : \RectorPrefix20220606\PhpParser\Node\Expr
|
2021-11-20 15:31:12 +00:00
|
|
|
{
|
|
|
|
// if PHP 8.0- throw exception
|
2022-06-06 16:43:29 +00:00
|
|
|
if (!$this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::NEW_INITIALIZERS)) {
|
|
|
|
throw new InvalidNestedAttributeException();
|
2021-11-20 15:31:12 +00:00
|
|
|
}
|
|
|
|
$annotationShortName = $this->resolveAnnotationName($value);
|
|
|
|
$values = $value->getValues();
|
|
|
|
if ($values !== []) {
|
|
|
|
$argValues = $this->annotationToAttributeMapper->map($value->getValuesWithExplicitSilentAndWithoutQuotes());
|
2022-06-06 16:43:29 +00:00
|
|
|
if ($argValues instanceof Array_) {
|
2022-02-05 10:39:52 +00:00
|
|
|
// create named args
|
|
|
|
$args = $this->attributeArrayNameInliner->inlineArrayToArgs($argValues);
|
|
|
|
} else {
|
2022-06-06 16:43:29 +00:00
|
|
|
throw new ShouldNotHappenException();
|
2021-11-20 15:31:12 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$args = [];
|
|
|
|
}
|
2022-06-06 16:43:29 +00:00
|
|
|
return new New_(new Name($annotationShortName), $args);
|
2021-11-20 15:31:12 +00:00
|
|
|
}
|
2022-06-06 16:43:29 +00:00
|
|
|
private function resolveAnnotationName(DoctrineAnnotationTagValueNode $doctrineAnnotationTagValueNode) : string
|
2021-11-20 15:31:12 +00:00
|
|
|
{
|
|
|
|
$annotationShortName = $doctrineAnnotationTagValueNode->identifierTypeNode->name;
|
|
|
|
return \ltrim($annotationShortName, '@');
|
|
|
|
}
|
|
|
|
}
|