Skip to content

Commit cfe9845

Browse files
committed
Add property support
1 parent 1dd491c commit cfe9845

File tree

9 files changed

+288
-6
lines changed

9 files changed

+288
-6
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;
6+
7+
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
8+
use phpDocumentor\Reflection\DocBlock\Tag;
9+
use phpDocumentor\Reflection\DocBlock\Tags\Property;
10+
use phpDocumentor\Reflection\Types\Context;
11+
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
12+
use PHPStan\PhpDocParser\Ast\PhpDoc\PropertyTagValueNode;
13+
use Webmozart\Assert\Assert;
14+
15+
use function trim;
16+
17+
/**
18+
* @internal This class is not part of the BC promise of this library.
19+
*/
20+
final class PropertyFactory implements PHPStanFactory
21+
{
22+
private TypeFactory $typeFactory;
23+
private DescriptionFactory $descriptionFactory;
24+
25+
public function __construct(TypeFactory $typeFactory, DescriptionFactory $descriptionFactory)
26+
{
27+
$this->typeFactory = $typeFactory;
28+
$this->descriptionFactory = $descriptionFactory;
29+
}
30+
31+
public function create(PhpDocTagNode $node, ?Context $context): Tag
32+
{
33+
$tagValue = $node->value;
34+
Assert::isInstanceOf($tagValue, PropertyTagValueNode::class);
35+
36+
return new Property(
37+
trim($tagValue->propertyName, '$'),
38+
$this->typeFactory->createType($tagValue->type, $context),
39+
$this->descriptionFactory->create($tagValue->description, $context)
40+
);
41+
}
42+
43+
public function supports(PhpDocTagNode $node, ?Context $context): bool
44+
{
45+
return $node->value instanceof PropertyTagValueNode && $node->name === '@property';
46+
}
47+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;
6+
7+
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
8+
use phpDocumentor\Reflection\DocBlock\Tag;
9+
use phpDocumentor\Reflection\DocBlock\Tags\PropertyRead;
10+
use phpDocumentor\Reflection\Types\Context;
11+
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
12+
use PHPStan\PhpDocParser\Ast\PhpDoc\PropertyTagValueNode;
13+
use Webmozart\Assert\Assert;
14+
15+
use function trim;
16+
17+
/**
18+
* @internal This class is not part of the BC promise of this library.
19+
*/
20+
final class PropertyReadFactory implements PHPStanFactory
21+
{
22+
private TypeFactory $typeFactory;
23+
private DescriptionFactory $descriptionFactory;
24+
25+
public function __construct(TypeFactory $typeFactory, DescriptionFactory $descriptionFactory)
26+
{
27+
$this->typeFactory = $typeFactory;
28+
$this->descriptionFactory = $descriptionFactory;
29+
}
30+
31+
public function create(PhpDocTagNode $node, ?Context $context): Tag
32+
{
33+
$tagValue = $node->value;
34+
Assert::isInstanceOf($tagValue, PropertyTagValueNode::class);
35+
36+
return new PropertyRead(
37+
trim($tagValue->propertyName, '$'),
38+
$this->typeFactory->createType($tagValue->type, $context),
39+
$this->descriptionFactory->create($tagValue->description, $context)
40+
);
41+
}
42+
43+
public function supports(PhpDocTagNode $node, ?Context $context): bool
44+
{
45+
return $node->value instanceof PropertyTagValueNode && $node->name === '@property-read';
46+
}
47+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;
6+
7+
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
8+
use phpDocumentor\Reflection\DocBlock\Tag;
9+
use phpDocumentor\Reflection\DocBlock\Tags\PropertyWrite;
10+
use phpDocumentor\Reflection\Types\Context;
11+
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
12+
use PHPStan\PhpDocParser\Ast\PhpDoc\PropertyTagValueNode;
13+
use Webmozart\Assert\Assert;
14+
15+
use function trim;
16+
17+
/**
18+
* @internal This class is not part of the BC promise of this library.
19+
*/
20+
final class PropertyWriteFactory implements PHPStanFactory
21+
{
22+
private TypeFactory $typeFactory;
23+
private DescriptionFactory $descriptionFactory;
24+
25+
public function __construct(TypeFactory $typeFactory, DescriptionFactory $descriptionFactory)
26+
{
27+
$this->typeFactory = $typeFactory;
28+
$this->descriptionFactory = $descriptionFactory;
29+
}
30+
31+
public function create(PhpDocTagNode $node, ?Context $context): Tag
32+
{
33+
$tagValue = $node->value;
34+
Assert::isInstanceOf($tagValue, PropertyTagValueNode::class);
35+
36+
return new PropertyWrite(
37+
trim($tagValue->propertyName, '$'),
38+
$this->typeFactory->createType($tagValue->type, $context),
39+
$this->descriptionFactory->create($tagValue->description, $context)
40+
);
41+
}
42+
43+
public function supports(PhpDocTagNode $node, ?Context $context): bool
44+
{
45+
return $node->value instanceof PropertyTagValueNode && $node->name === '@property-write';
46+
}
47+
}

src/DocBlockFactory.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
use phpDocumentor\Reflection\DocBlock\Tags\Factory\AbstractPHPStanFactory;
2323
use phpDocumentor\Reflection\DocBlock\Tags\Factory\Factory;
2424
use phpDocumentor\Reflection\DocBlock\Tags\Factory\ParamFactory;
25+
use phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyFactory;
26+
use phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyReadFactory;
27+
use phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyWriteFactory;
2528
use phpDocumentor\Reflection\DocBlock\Tags\Factory\ReturnFactory;
2629
use phpDocumentor\Reflection\DocBlock\Tags\Factory\TypeFactory;
2730
use phpDocumentor\Reflection\DocBlock\Tags\Factory\VarFactory;
@@ -73,13 +76,19 @@ public static function createInstance(array $additionalTags = []): self
7376
new ParamFactory($typeFactory, $descriptionFactory),
7477
new VarFactory($typeFactory, $descriptionFactory),
7578
new ReturnFactory($typeFactory, $descriptionFactory),
79+
new PropertyFactory($typeFactory, $descriptionFactory),
80+
new PropertyReadFactory($typeFactory, $descriptionFactory),
81+
new PropertyWriteFactory($typeFactory, $descriptionFactory),
7682
);
7783

7884
$tagFactory->addService($descriptionFactory);
7985
$tagFactory->addService($typeResolver);
8086
$tagFactory->registerTagHandler('param', $phpstanTagFactory);
8187
$tagFactory->registerTagHandler('var', $phpstanTagFactory);
8288
$tagFactory->registerTagHandler('return', $phpstanTagFactory);
89+
$tagFactory->registerTagHandler('property', $phpstanTagFactory);
90+
$tagFactory->registerTagHandler('property-read', $phpstanTagFactory);
91+
$tagFactory->registerTagHandler('property-write', $phpstanTagFactory);
8392

8493
$docBlockFactory = new self($descriptionFactory, $tagFactory);
8594
foreach ($additionalTags as $tagName => $tagHandler) {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of phpDocumentor.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*
11+
* @link http://phpdoc.org
12+
*/
13+
14+
namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;
15+
16+
use phpDocumentor\Reflection\DocBlock\Description;
17+
use phpDocumentor\Reflection\DocBlock\Tags\Property;
18+
use phpDocumentor\Reflection\Types\Context;
19+
use phpDocumentor\Reflection\Types\String_;
20+
21+
final class PropertyFactoryTest extends TagFactoryTestCase
22+
{
23+
/**
24+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyFactory::__construct
25+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyFactory::create
26+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyFactory::supports
27+
*/
28+
public function testParamIsCreated(): void
29+
{
30+
$ast = $this->parseTag('@property string $var');
31+
$factory = new PropertyFactory($this->giveTypeFactory(), $this->givenDescriptionFactory());
32+
$context = new Context('global');
33+
34+
self::assertTrue($factory->supports($ast, $context));
35+
self::assertEquals(
36+
new Property(
37+
'var',
38+
new String_(),
39+
new Description('')
40+
),
41+
$factory->create($ast, $context)
42+
);
43+
}
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of phpDocumentor.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*
11+
* @link http://phpdoc.org
12+
*/
13+
14+
namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;
15+
16+
use phpDocumentor\Reflection\DocBlock\Description;
17+
use phpDocumentor\Reflection\DocBlock\Tags\PropertyRead;
18+
use phpDocumentor\Reflection\Types\Context;
19+
use phpDocumentor\Reflection\Types\String_;
20+
21+
final class PropertyReadFactoryTest extends TagFactoryTestCase
22+
{
23+
/**
24+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyReadFactory::__construct
25+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyReadFactory::create
26+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyReadFactory::supports
27+
*/
28+
public function testParamIsCreated(): void
29+
{
30+
$ast = $this->parseTag('@property-read string $var');
31+
$factory = new PropertyReadFactory($this->giveTypeFactory(), $this->givenDescriptionFactory());
32+
$context = new Context('global');
33+
34+
self::assertTrue($factory->supports($ast, $context));
35+
self::assertEquals(
36+
new PropertyRead(
37+
'var',
38+
new String_(),
39+
new Description('')
40+
),
41+
$factory->create($ast, $context)
42+
);
43+
}
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of phpDocumentor.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*
11+
* @link http://phpdoc.org
12+
*/
13+
14+
namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;
15+
16+
use phpDocumentor\Reflection\DocBlock\Description;
17+
use phpDocumentor\Reflection\DocBlock\Tags\PropertyWrite;
18+
use phpDocumentor\Reflection\Types\Context;
19+
use phpDocumentor\Reflection\Types\String_;
20+
21+
final class PropertyWriteFactoryTest extends TagFactoryTestCase
22+
{
23+
/**
24+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyWriteFactory::__construct
25+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyWriteFactory::create
26+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\PropertyWriteFactory::supports
27+
*/
28+
public function testParamIsCreated(): void
29+
{
30+
$ast = $this->parseTag('@property-write string $var');
31+
$factory = new PropertyWriteFactory($this->giveTypeFactory(), $this->givenDescriptionFactory());
32+
$context = new Context('global');
33+
34+
self::assertTrue($factory->supports($ast, $context));
35+
self::assertEquals(
36+
new PropertyWrite(
37+
'var',
38+
new String_(),
39+
new Description('')
40+
),
41+
$factory->create($ast, $context)
42+
);
43+
}
44+
}

tests/unit/DocBlock/Tags/Factory/ReturnFactoryTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
final class ReturnFactoryTest extends TagFactoryTestCase
2222
{
2323
/**
24-
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\ParamFactory::__construct
25-
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\ParamFactory::create
26-
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\ParamFactory::supports
24+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\ReturnFactory::__construct
25+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\ReturnFactory::create
26+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\ReturnFactory::supports
2727
*/
2828
public function testParamIsCreated(): void
2929
{

tests/unit/DocBlock/Tags/Factory/VarFactoryTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
final class VarFactoryTest extends TagFactoryTestCase
2222
{
2323
/**
24-
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\ParamFactory::__construct
25-
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\ParamFactory::create
26-
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\ParamFactory::supports
24+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\VarFactory::__construct
25+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\VarFactory::create
26+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\VarFactory::supports
2727
*/
2828
public function testParamIsCreated(): void
2929
{

0 commit comments

Comments
 (0)