Add property support

This commit is contained in:
Jaapio
2022-10-28 21:07:34 +02:00
parent 1dd491c8ec
commit cfe9845260
9 changed files with 288 additions and 6 deletions
@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
use phpDocumentor\Reflection\DocBlock\Tag;
use phpDocumentor\Reflection\DocBlock\Tags\Property;
use phpDocumentor\Reflection\Types\Context;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PropertyTagValueNode;
use Webmozart\Assert\Assert;
use function trim;
/**
* @internal This class is not part of the BC promise of this library.
*/
final class PropertyFactory implements PHPStanFactory
{
private TypeFactory $typeFactory;
private DescriptionFactory $descriptionFactory;
public function __construct(TypeFactory $typeFactory, DescriptionFactory $descriptionFactory)
{
$this->typeFactory = $typeFactory;
$this->descriptionFactory = $descriptionFactory;
}
public function create(PhpDocTagNode $node, ?Context $context): Tag
{
$tagValue = $node->value;
Assert::isInstanceOf($tagValue, PropertyTagValueNode::class);
return new Property(
trim($tagValue->propertyName, '$'),
$this->typeFactory->createType($tagValue->type, $context),
$this->descriptionFactory->create($tagValue->description, $context)
);
}
public function supports(PhpDocTagNode $node, ?Context $context): bool
{
return $node->value instanceof PropertyTagValueNode && $node->name === '@property';
}
}
@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
use phpDocumentor\Reflection\DocBlock\Tag;
use phpDocumentor\Reflection\DocBlock\Tags\PropertyRead;
use phpDocumentor\Reflection\Types\Context;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PropertyTagValueNode;
use Webmozart\Assert\Assert;
use function trim;
/**
* @internal This class is not part of the BC promise of this library.
*/
final class PropertyReadFactory implements PHPStanFactory
{
private TypeFactory $typeFactory;
private DescriptionFactory $descriptionFactory;
public function __construct(TypeFactory $typeFactory, DescriptionFactory $descriptionFactory)
{
$this->typeFactory = $typeFactory;
$this->descriptionFactory = $descriptionFactory;
}
public function create(PhpDocTagNode $node, ?Context $context): Tag
{
$tagValue = $node->value;
Assert::isInstanceOf($tagValue, PropertyTagValueNode::class);
return new PropertyRead(
trim($tagValue->propertyName, '$'),
$this->typeFactory->createType($tagValue->type, $context),
$this->descriptionFactory->create($tagValue->description, $context)
);
}
public function supports(PhpDocTagNode $node, ?Context $context): bool
{
return $node->value instanceof PropertyTagValueNode && $node->name === '@property-read';
}
}
@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
use phpDocumentor\Reflection\DocBlock\Tag;
use phpDocumentor\Reflection\DocBlock\Tags\PropertyWrite;
use phpDocumentor\Reflection\Types\Context;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PropertyTagValueNode;
use Webmozart\Assert\Assert;
use function trim;
/**
* @internal This class is not part of the BC promise of this library.
*/
final class PropertyWriteFactory implements PHPStanFactory
{
private TypeFactory $typeFactory;
private DescriptionFactory $descriptionFactory;
public function __construct(TypeFactory $typeFactory, DescriptionFactory $descriptionFactory)
{
$this->typeFactory = $typeFactory;
$this->descriptionFactory = $descriptionFactory;
}
public function create(PhpDocTagNode $node, ?Context $context): Tag
{
$tagValue = $node->value;
Assert::isInstanceOf($tagValue, PropertyTagValueNode::class);
return new PropertyWrite(
trim($tagValue->propertyName, '$'),
$this->typeFactory->createType($tagValue->type, $context),
$this->descriptionFactory->create($tagValue->description, $context)
);
}
public function supports(PhpDocTagNode $node, ?Context $context): bool
{
return $node->value instanceof PropertyTagValueNode && $node->name === '@property-write';
}
}
+9
View File
@@ -22,6 +22,9 @@ use phpDocumentor\Reflection\DocBlock\TagFactory;
use phpDocumentor\Reflection\DocBlock\Tags\Factory\AbstractPHPStanFactory; use phpDocumentor\Reflection\DocBlock\Tags\Factory\AbstractPHPStanFactory;
use phpDocumentor\Reflection\DocBlock\Tags\Factory\Factory; use phpDocumentor\Reflection\DocBlock\Tags\Factory\Factory;
use phpDocumentor\Reflection\DocBlock\Tags\Factory\ParamFactory; use phpDocumentor\Reflection\DocBlock\Tags\Factory\ParamFactory;
use phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyFactory;
use phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyReadFactory;
use phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyWriteFactory;
use phpDocumentor\Reflection\DocBlock\Tags\Factory\ReturnFactory; use phpDocumentor\Reflection\DocBlock\Tags\Factory\ReturnFactory;
use phpDocumentor\Reflection\DocBlock\Tags\Factory\TypeFactory; use phpDocumentor\Reflection\DocBlock\Tags\Factory\TypeFactory;
use phpDocumentor\Reflection\DocBlock\Tags\Factory\VarFactory; use phpDocumentor\Reflection\DocBlock\Tags\Factory\VarFactory;
@@ -73,6 +76,9 @@ final class DocBlockFactory implements DocBlockFactoryInterface
new ParamFactory($typeFactory, $descriptionFactory), new ParamFactory($typeFactory, $descriptionFactory),
new VarFactory($typeFactory, $descriptionFactory), new VarFactory($typeFactory, $descriptionFactory),
new ReturnFactory($typeFactory, $descriptionFactory), new ReturnFactory($typeFactory, $descriptionFactory),
new PropertyFactory($typeFactory, $descriptionFactory),
new PropertyReadFactory($typeFactory, $descriptionFactory),
new PropertyWriteFactory($typeFactory, $descriptionFactory),
); );
$tagFactory->addService($descriptionFactory); $tagFactory->addService($descriptionFactory);
@@ -80,6 +86,9 @@ final class DocBlockFactory implements DocBlockFactoryInterface
$tagFactory->registerTagHandler('param', $phpstanTagFactory); $tagFactory->registerTagHandler('param', $phpstanTagFactory);
$tagFactory->registerTagHandler('var', $phpstanTagFactory); $tagFactory->registerTagHandler('var', $phpstanTagFactory);
$tagFactory->registerTagHandler('return', $phpstanTagFactory); $tagFactory->registerTagHandler('return', $phpstanTagFactory);
$tagFactory->registerTagHandler('property', $phpstanTagFactory);
$tagFactory->registerTagHandler('property-read', $phpstanTagFactory);
$tagFactory->registerTagHandler('property-write', $phpstanTagFactory);
$docBlockFactory = new self($descriptionFactory, $tagFactory); $docBlockFactory = new self($descriptionFactory, $tagFactory);
foreach ($additionalTags as $tagName => $tagHandler) { foreach ($additionalTags as $tagName => $tagHandler) {
@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link http://phpdoc.org
*/
namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;
use phpDocumentor\Reflection\DocBlock\Description;
use phpDocumentor\Reflection\DocBlock\Tags\Property;
use phpDocumentor\Reflection\Types\Context;
use phpDocumentor\Reflection\Types\String_;
final class PropertyFactoryTest extends TagFactoryTestCase
{
/**
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyFactory::__construct
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyFactory::create
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyFactory::supports
*/
public function testParamIsCreated(): void
{
$ast = $this->parseTag('@property string $var');
$factory = new PropertyFactory($this->giveTypeFactory(), $this->givenDescriptionFactory());
$context = new Context('global');
self::assertTrue($factory->supports($ast, $context));
self::assertEquals(
new Property(
'var',
new String_(),
new Description('')
),
$factory->create($ast, $context)
);
}
}
@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link http://phpdoc.org
*/
namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;
use phpDocumentor\Reflection\DocBlock\Description;
use phpDocumentor\Reflection\DocBlock\Tags\PropertyRead;
use phpDocumentor\Reflection\Types\Context;
use phpDocumentor\Reflection\Types\String_;
final class PropertyReadFactoryTest extends TagFactoryTestCase
{
/**
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyReadFactory::__construct
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyReadFactory::create
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyReadFactory::supports
*/
public function testParamIsCreated(): void
{
$ast = $this->parseTag('@property-read string $var');
$factory = new PropertyReadFactory($this->giveTypeFactory(), $this->givenDescriptionFactory());
$context = new Context('global');
self::assertTrue($factory->supports($ast, $context));
self::assertEquals(
new PropertyRead(
'var',
new String_(),
new Description('')
),
$factory->create($ast, $context)
);
}
}
@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link http://phpdoc.org
*/
namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;
use phpDocumentor\Reflection\DocBlock\Description;
use phpDocumentor\Reflection\DocBlock\Tags\PropertyWrite;
use phpDocumentor\Reflection\Types\Context;
use phpDocumentor\Reflection\Types\String_;
final class PropertyWriteFactoryTest extends TagFactoryTestCase
{
/**
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyWriteFactory::__construct
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyWriteFactory::create
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyWriteFactory::supports
*/
public function testParamIsCreated(): void
{
$ast = $this->parseTag('@property-write string $var');
$factory = new PropertyWriteFactory($this->giveTypeFactory(), $this->givenDescriptionFactory());
$context = new Context('global');
self::assertTrue($factory->supports($ast, $context));
self::assertEquals(
new PropertyWrite(
'var',
new String_(),
new Description('')
),
$factory->create($ast, $context)
);
}
}
@@ -21,9 +21,9 @@ use phpDocumentor\Reflection\Types\String_;
final class ReturnFactoryTest extends TagFactoryTestCase final class ReturnFactoryTest extends TagFactoryTestCase
{ {
/** /**
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\ParamFactory::__construct * @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\ReturnFactory::__construct
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\ParamFactory::create * @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\ReturnFactory::create
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\ParamFactory::supports * @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\ReturnFactory::supports
*/ */
public function testParamIsCreated(): void public function testParamIsCreated(): void
{ {
@@ -21,9 +21,9 @@ use phpDocumentor\Reflection\Types\String_;
final class VarFactoryTest extends TagFactoryTestCase final class VarFactoryTest extends TagFactoryTestCase
{ {
/** /**
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\ParamFactory::__construct * @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\VarFactory::__construct
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\ParamFactory::create * @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\VarFactory::create
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\ParamFactory::supports * @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\VarFactory::supports
*/ */
public function testParamIsCreated(): void public function testParamIsCreated(): void
{ {