Merge pull request #2979 from rectorphp/fix-spacing

fix spacing for array shape item
This commit is contained in:
Tomas Votruba 2020-03-01 10:53:53 +01:00 committed by GitHub
commit 3b74b47a97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 4 deletions

View File

@ -4,11 +4,45 @@ declare(strict_types=1);
namespace Rector\AttributeAwarePhpDoc\Ast\Type;
use Nette\Utils\Strings;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeItemNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use Rector\BetterPhpDocParser\Attributes\Attribute\AttributeTrait;
use Rector\BetterPhpDocParser\Contract\PhpDocNode\AttributeAwareNodeInterface;
final class AttributeAwareArrayShapeItemNode extends ArrayShapeItemNode implements AttributeAwareNodeInterface
{
use AttributeTrait;
/**
* @var bool
*/
private $hasSpaceAfterDoubleColon = false;
/**
* @param ConstExprIntegerNode|IdentifierTypeNode|null $keyName
*/
public function __construct($keyName, bool $optional, TypeNode $typeNode, string $docComment = '')
{
parent::__construct($keyName, $optional, $typeNode);
$this->hasSpaceAfterDoubleColon = (bool) Strings::match($docComment, '#\:\s+#');
}
public function __toString(): string
{
if ($this->keyName === null) {
return (string) $this->valueType;
}
return sprintf(
'%s%s:%s%s',
(string) $this->keyName,
$this->optional ? '?' : '',
$this->hasSpaceAfterDoubleColon ? ' ' : '',
(string) $this->valueType
);
}
}

View File

@ -7,11 +7,18 @@ namespace Rector\AttributeAwarePhpDoc\AttributeAwareNodeFactory\Type;
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeItemNode;
use Rector\AttributeAwarePhpDoc\Ast\Type\AttributeAwareArrayShapeItemNode;
use Rector\AttributeAwarePhpDoc\Contract\AttributeNodeAwareFactory\AttributeAwareNodeFactoryAwareInterface;
use Rector\AttributeAwarePhpDoc\Contract\AttributeNodeAwareFactory\AttributeNodeAwareFactoryInterface;
use Rector\BetterPhpDocParser\Attributes\Ast\AttributeAwareNodeFactory;
use Rector\BetterPhpDocParser\Contract\PhpDocNode\AttributeAwareNodeInterface;
final class AttributeAwareArrayShapeItemNodeFactory implements AttributeNodeAwareFactoryInterface
final class AttributeAwareArrayShapeItemNodeFactory implements AttributeNodeAwareFactoryInterface, AttributeAwareNodeFactoryAwareInterface
{
/**
* @var AttributeAwareNodeFactory
*/
private $attributeAwareNodeFactory;
public function getOriginalNodeClass(): string
{
return ArrayShapeItemNode::class;
@ -27,6 +34,13 @@ final class AttributeAwareArrayShapeItemNodeFactory implements AttributeNodeAwar
*/
public function create(Node $node, string $docContent): AttributeAwareNodeInterface
{
return new AttributeAwareArrayShapeItemNode($node->keyName, $node->optional, $node->valueType);
$node->valueType = $this->attributeAwareNodeFactory->createFromNode($node->valueType, $docContent);
return new AttributeAwareArrayShapeItemNode($node->keyName, $node->optional, $node->valueType, $docContent);
}
public function setAttributeAwareNodeFactory(AttributeAwareNodeFactory $attributeAwareNodeFactory): void
{
$this->attributeAwareNodeFactory = $attributeAwareNodeFactory;
}
}

View File

@ -36,8 +36,7 @@ final class AttributeAwareArrayShapeNodeFactory implements AttributeNodeAwareFac
{
$items = [];
foreach ($node->items as $item) {
$item->valueType = $this->attributeAwareNodeFactory->createFromNode($item->valueType, $docContent);
$items[] = $item;
$items[] = $this->attributeAwareNodeFactory->createFromNode($item, $docContent);
}
return new AttributeAwareArrayShapeNode($items);

View File

@ -0,0 +1,5 @@
/**
* normal view template will be rendered.
*
* @var array{serialize:string|bool|null}
*/