rector/packages/PhpAttribute/Printer/PhpAttributeGroupFactory.php

114 lines
3.2 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace Rector\PhpAttribute\Printer;
use PhpParser\BuilderHelpers;
use PhpParser\Node\Arg;
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantFloatType;
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
use Rector\Php80\ValueObject\AnnotationToAttribute;
final class PhpAttributeGroupFactory
{
public function createFromSimpleTag(AnnotationToAttribute $annotationToAttribute): AttributeGroup
{
$fullyQualified = new FullyQualified($annotationToAttribute->getAttributeClass());
$attribute = new Attribute($fullyQualified);
return new AttributeGroup([$attribute]);
}
public function create(
DoctrineAnnotationTagValueNode $doctrineAnnotationTagValueNode,
AnnotationToAttribute $annotationToAttribute
): AttributeGroup {
$fullyQualified = new FullyQualified($annotationToAttribute->getAttributeClass());
$values = $doctrineAnnotationTagValueNode->getValuesWithExplicitSilentAndWithoutQuotes();
$args = $this->createArgsFromItems($values);
$attribute = new Attribute($fullyQualified, $args);
return new AttributeGroup([$attribute]);
}
/**
* @param mixed[] $items
* @return Arg[]
*/
private function createArgsFromItems(array $items, ?string $silentKey = null): array
{
$args = [];
if ($silentKey !== null && isset($items[$silentKey])) {
$silentValue = BuilderHelpers::normalizeValue($items[$silentKey]);
$args[] = new Arg($silentValue);
unset($items[$silentKey]);
}
if ($this->isArrayArguments($items)) {
foreach ($items as $key => $value) {
$argumentName = new Identifier($key);
$value = $this->normalizeNodeValue($value);
$value = BuilderHelpers::normalizeValue($value);
$args[] = new Arg($value, false, false, [], $argumentName);
}
} else {
[Naming] Register RenameForeachValueVariableToMatchExprVariableRector to naming config set (#5696) * [Naming] Register RenameForeachValueVariableToMatchExprVariableRector to naming config set * fix property fetch not from this * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * fix * [ci-review] Rector Rectify * [ci-review] Rector Rectify * fixture fix * phpstan * [ci-review] Rector Rectify * phpstan * extract to separate method for collect assigns by name * adding InflectorSingularResolver service * skip single prefix and length >= 40 * add failing fixture to skip plural camel case * use regex to get camel cases * implemented singularize camel cased * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * phpstan * handle singular verb news -> new * [ci-review] Rector Rectify * fixture fix * handle has * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * phpstan * phpstan * handle left side By * [ci-review] Rector Rectify * re-use resolve call * [ci-review] Rector Rectify * phpstan * [ci-review] Rector Rectify * final touch * final touch * [ci-review] Rector Rectify * [ci-review] Rector Rectify * use previous By in the middle * update $childClassReflection->hasProperty($propertyName) * [ci-review] Rector Rectify * catchKeys * regex fix * fixture * [ci-review] Rector Rectify * try use check array * Revert "try use check array" This reverts commit adb9f767f20ea2544e5ccfc9cfe361ecc929912a. * use files Co-authored-by: kaizen-ci <info@kaizen-ci.org>
2021-03-05 17:55:40 +07:00
foreach ($items as $item) {
$item = BuilderHelpers::normalizeValue($item);
$args[] = new Arg($item);
}
}
return $args;
}
/**
* @param mixed[] $items
*/
private function isArrayArguments(array $items): bool
{
foreach (array_keys($items) as $key) {
if (! is_int($key)) {
return true;
}
}
return false;
}
/**
* @param mixed $value
* @return bool|float|int|string
*/
private function normalizeNodeValue($value)
{
if ($value instanceof ConstExprIntegerNode) {
return (int) $value->value;
}
if ($value instanceof ConstantFloatType) {
return $value->getValue();
}
if ($value instanceof ConstantBooleanType) {
return $value->getValue();
}
if ($value instanceof Node) {
return (string) $value;
}
return $value;
}
}