Add @mixin support from PHPStan

This commit is contained in:
TomasVotruba 2020-05-12 01:07:22 +02:00
parent 6e9592e152
commit da09614b2e
5 changed files with 94 additions and 0 deletions

View File

@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace Rector\AttributeAwarePhpDoc\Ast\PhpDoc;
use PHPStan\PhpDocParser\Ast\PhpDoc\MixinTagValueNode;
use Rector\BetterPhpDocParser\Attributes\Attribute\AttributeTrait;
use Rector\BetterPhpDocParser\Contract\PhpDocNode\AttributeAwareNodeInterface;
final class AttributeAwareMixinTagValueNode extends MixinTagValueNode implements AttributeAwareNodeInterface
{
use AttributeTrait;
}

View File

@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace Rector\AttributeAwarePhpDoc\Ast\Type;
use PHPStan\PhpDocParser\Ast\Type\ConstTypeNode;
use Rector\BetterPhpDocParser\Attributes\Attribute\AttributeTrait;
use Rector\BetterPhpDocParser\Contract\PhpDocNode\AttributeAwareNodeInterface;
final class AttributeAwareConstTypeNode extends ConstTypeNode implements AttributeAwareNodeInterface
{
use AttributeTrait;
}

View File

@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace Rector\AttributeAwarePhpDoc\AttributeAwareNodeFactory\PhpDoc;
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\PhpDoc\MixinTagValueNode;
use Rector\AttributeAwarePhpDoc\Ast\PhpDoc\AttributeAwareMixinTagValueNode;
use Rector\AttributeAwarePhpDoc\Contract\AttributeNodeAwareFactory\AttributeNodeAwareFactoryInterface;
use Rector\BetterPhpDocParser\Contract\PhpDocNode\AttributeAwareNodeInterface;
final class AttributeAwareMixinTagValueNodeFactory implements AttributeNodeAwareFactoryInterface
{
public function getOriginalNodeClass(): string
{
return MixinTagValueNode::class;
}
public function isMatch(Node $node): bool
{
return is_a($node, MixinTagValueNode::class, true);
}
/**
* @param MixinTagValueNode $node
*/
public function create(Node $node, string $docContent): AttributeAwareNodeInterface
{
return new AttributeAwareMixinTagValueNode($node->type, $node->description);
}
}

View File

@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace Rector\AttributeAwarePhpDoc\AttributeAwareNodeFactory\Type;
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\Type\ConstTypeNode;
use Rector\AttributeAwarePhpDoc\Ast\Type\AttributeAwareConstTypeNode;
use Rector\AttributeAwarePhpDoc\Contract\AttributeNodeAwareFactory\AttributeNodeAwareFactoryInterface;
use Rector\BetterPhpDocParser\Contract\PhpDocNode\AttributeAwareNodeInterface;
final class AttributeAwareConstTypeNodeFactory implements AttributeNodeAwareFactoryInterface
{
public function getOriginalNodeClass(): string
{
return ConstTypeNode::class;
}
public function isMatch(Node $node): bool
{
return is_a($node, ConstTypeNode::class, true);
}
/**
* @param ConstTypeNode $node
*/
public function create(Node $node, string $docContent): AttributeAwareNodeInterface
{
return new AttributeAwareConstTypeNode($node->constExpr);
}
}

View File

@ -124,11 +124,13 @@ final class AttributeAwareClassFactoryFactory
$paramBuilder = new Param('docContent');
$paramBuilder->setType('string');
$docContentParam = $paramBuilder->getNode();
$createClassMethod->addParam($nodeParam);
$createClassMethod->addParam($docContentParam);
$createClassMethod->makePublic();
$createClassMethod->setReturnType(new FullyQualified(AttributeAwareNodeInterface::class));
// add @param doc with more precise type