Skip to content

Commit 61e2f1f

Browse files
committed
Add attribute support to more elements
1 parent 5953c00 commit 61e2f1f

File tree

7 files changed

+84
-36
lines changed

7 files changed

+84
-36
lines changed

src/phpDocumentor/Reflection/Php/Factory/AbstractFactory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ abstract class AbstractFactory implements ProjectFactoryStrategy
3131
/** @param iterable<Reducer> $reducers */
3232
public function __construct(
3333
private readonly DocBlockFactoryInterface $docBlockFactory,
34-
private readonly iterable $reducers = [],
34+
protected readonly iterable $reducers = [],
3535
) {
3636
}
3737

src/phpDocumentor/Reflection/Php/Factory/ClassConstant.php

+20-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use phpDocumentor\Reflection\Php\Class_;
1919
use phpDocumentor\Reflection\Php\Constant as ConstantElement;
2020
use phpDocumentor\Reflection\Php\Enum_;
21+
use phpDocumentor\Reflection\Php\Factory\Reducer\Reducer;
2122
use phpDocumentor\Reflection\Php\Interface_;
2223
use phpDocumentor\Reflection\Php\StrategyContainer;
2324
use phpDocumentor\Reflection\Php\Trait_;
@@ -34,9 +35,13 @@
3435
*/
3536
final class ClassConstant extends AbstractFactory
3637
{
37-
public function __construct(DocBlockFactoryInterface $blockFactory, private readonly PrettyPrinter $valueConverter)
38-
{
39-
parent::__construct($blockFactory);
38+
/** @param iterable<Reducer> $reducers */
39+
public function __construct(
40+
DocBlockFactoryInterface $blockFactory,
41+
private readonly PrettyPrinter $valueConverter,
42+
iterable $reducers = [],
43+
) {
44+
parent::__construct($blockFactory, $reducers);
4045
}
4146

4247
public function matches(ContextStack $context, object $object): bool
@@ -73,15 +78,25 @@ protected function doCreate(
7378
$constants = new ClassConstantIterator($object);
7479

7580
foreach ($constants as $const) {
76-
$constantContainer->addConstant(new ConstantElement(
81+
$constant = new ConstantElement(
7782
$const->getFqsen(),
7883
$this->createDocBlock($const->getDocComment(), $context->getTypeContext()),
7984
$const->getValue() !== null ? $this->valueConverter->prettyPrintExpr($const->getValue()) : null,
8085
new Location($const->getLine()),
8186
new Location($const->getEndLine()),
8287
$this->buildVisibility($const),
8388
$const->isFinal(),
84-
));
89+
);
90+
91+
foreach ($this->reducers as $reducer) {
92+
$constant = $reducer->reduce($context, $const, $strategies, $constant);
93+
}
94+
95+
if ($constant === null) {
96+
continue;
97+
}
98+
99+
$constantContainer->addConstant($constant);
85100
}
86101

87102
return null;

src/phpDocumentor/Reflection/Php/Factory/ConstructorPromotion.php

+14-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use phpDocumentor\Reflection\Fqsen;
1010
use phpDocumentor\Reflection\Location;
1111
use phpDocumentor\Reflection\Php\Class_ as ClassElement;
12+
use phpDocumentor\Reflection\Php\Factory\Reducer\Reducer;
1213
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
1314
use phpDocumentor\Reflection\Php\Property;
1415
use phpDocumentor\Reflection\Php\StrategyContainer;
@@ -22,12 +23,14 @@
2223

2324
final class ConstructorPromotion extends AbstractFactory
2425
{
26+
/** @param iterable<Reducer> $reducers */
2527
public function __construct(
2628
private readonly ProjectFactoryStrategy $methodStrategy,
2729
DocBlockFactoryInterface $docBlockFactory,
2830
private readonly PrettyPrinter $valueConverter,
31+
iterable $reducers = [],
2932
) {
30-
parent::__construct($docBlockFactory);
33+
parent::__construct($docBlockFactory, $reducers);
3134
}
3235

3336
public function matches(ContextStack $context, object $object): bool
@@ -51,13 +54,13 @@ protected function doCreate(ContextStack $context, object $object, StrategyConta
5154
continue;
5255
}
5356

54-
$this->promoteParameterToProperty($context, $param);
57+
$this->promoteParameterToProperty($context, $strategies, $param);
5558
}
5659

5760
return $context->peek();
5861
}
5962

60-
private function promoteParameterToProperty(ContextStack $context, Param $param): void
63+
private function promoteParameterToProperty(ContextStack $context, StrategyContainer $strategies, Param $param): void
6164
{
6265
$methodContainer = $context->peek();
6366
Assert::isInstanceOf($methodContainer, ClassElement::class);
@@ -75,6 +78,14 @@ private function promoteParameterToProperty(ContextStack $context, Param $param)
7578
$this->readOnly($param->flags),
7679
);
7780

81+
foreach ($this->reducers as $reducer) {
82+
$property = $reducer->reduce($context, $param, $strategies, $property);
83+
}
84+
85+
if ($property === null) {
86+
return;
87+
}
88+
7889
$methodContainer->addProperty($property);
7990
}
8091

src/phpDocumentor/Reflection/Php/Factory/EnumCase.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use phpDocumentor\Reflection\Location;
99
use phpDocumentor\Reflection\Php\Enum_ as EnumElement;
1010
use phpDocumentor\Reflection\Php\EnumCase as EnumCaseElement;
11+
use phpDocumentor\Reflection\Php\Factory\Reducer\Reducer;
1112
use phpDocumentor\Reflection\Php\StrategyContainer;
1213
use PhpParser\Node\Stmt\EnumCase as EnumCaseNode;
1314
use PhpParser\PrettyPrinter\Standard as PrettyPrinter;
@@ -16,9 +17,13 @@
1617

1718
final class EnumCase extends AbstractFactory
1819
{
19-
public function __construct(DocBlockFactoryInterface $docBlockFactory, private readonly PrettyPrinter $prettyPrinter)
20-
{
21-
parent::__construct($docBlockFactory);
20+
/** @param iterable<Reducer> $reducers */
21+
public function __construct(
22+
DocBlockFactoryInterface $docBlockFactory,
23+
private readonly PrettyPrinter $prettyPrinter,
24+
iterable $reducers = [],
25+
) {
26+
parent::__construct($docBlockFactory, $reducers);
2227
}
2328

2429
public function matches(ContextStack $context, object $object): bool

src/phpDocumentor/Reflection/Php/Factory/Property.php

+28-18
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use phpDocumentor\Reflection\DocBlockFactoryInterface;
1717
use phpDocumentor\Reflection\Location;
1818
use phpDocumentor\Reflection\Php\Class_;
19+
use phpDocumentor\Reflection\Php\Factory\Reducer\Reducer;
1920
use phpDocumentor\Reflection\Php\Property as PropertyDescriptor;
2021
use phpDocumentor\Reflection\Php\StrategyContainer;
2122
use phpDocumentor\Reflection\Php\Trait_;
@@ -32,12 +33,13 @@
3233
*/
3334
final class Property extends AbstractFactory
3435
{
35-
/**
36-
* Initializes the object.
37-
*/
38-
public function __construct(DocBlockFactoryInterface $docBlockFactory, private readonly PrettyPrinter $valueConverter)
39-
{
40-
parent::__construct($docBlockFactory);
36+
/** @param iterable<Reducer> $reducers */
37+
public function __construct(
38+
DocBlockFactoryInterface $docBlockFactory,
39+
private readonly PrettyPrinter $valueConverter,
40+
iterable $reducers = [],
41+
) {
42+
parent::__construct($docBlockFactory, $reducers);
4143
}
4244

4345
public function matches(ContextStack $context, object $object): bool
@@ -75,19 +77,27 @@ protected function doCreate(
7577
$default = $this->valueConverter->prettyPrintExpr($default);
7678
}
7779

78-
$propertyContainer->addProperty(
79-
new PropertyDescriptor(
80-
$stmt->getFqsen(),
81-
$this->buildVisibility($stmt),
82-
$this->createDocBlock($stmt->getDocComment(), $context->getTypeContext()),
83-
$default,
84-
$stmt->isStatic(),
85-
new Location($stmt->getLine()),
86-
new Location($stmt->getEndLine()),
87-
(new Type())->fromPhpParser($stmt->getType()),
88-
$stmt->isReadonly(),
89-
),
80+
$property = new PropertyDescriptor(
81+
$stmt->getFqsen(),
82+
$this->buildVisibility($stmt),
83+
$this->createDocBlock($stmt->getDocComment(), $context->getTypeContext()),
84+
$default,
85+
$stmt->isStatic(),
86+
new Location($stmt->getLine()),
87+
new Location($stmt->getEndLine()),
88+
(new Type())->fromPhpParser($stmt->getType()),
89+
$stmt->isReadonly(),
9090
);
91+
92+
foreach ($this->reducers as $reducer) {
93+
$property = $reducer->reduce($context, $object, $strategies, $property);
94+
}
95+
96+
if ($property === null) {
97+
continue;
98+
}
99+
100+
$propertyContainer->addProperty($property);
91101
}
92102

93103
return null;

src/phpDocumentor/Reflection/Php/Factory/Reducer/Reducer.php

+7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99

1010
interface Reducer
1111
{
12+
/**
13+
* @param TCarry|null $carry
14+
*
15+
* @return TCarry|null
16+
*
17+
* @template TCarry of object
18+
*/
1219
public function reduce(
1320
ContextStack $context,
1421
object $object,

src/phpDocumentor/Reflection/Php/ProjectFactory.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -77,24 +77,24 @@ public static function createInstance(): self
7777
new \phpDocumentor\Reflection\Php\Factory\Namespace_(),
7878
new Class_($docblockFactory, [$attributeReducer]),
7979
new Enum_($docblockFactory, [$attributeReducer]),
80-
new EnumCase($docblockFactory, new PrettyPrinter()),
80+
new EnumCase($docblockFactory, new PrettyPrinter(), [$attributeReducer]),
8181
new Define($docblockFactory, new PrettyPrinter()),
8282
new GlobalConstant($docblockFactory, new PrettyPrinter()),
83-
new ClassConstant($docblockFactory, new PrettyPrinter()),
83+
new ClassConstant($docblockFactory, new PrettyPrinter(), [$attributeReducer]),
8484
new Factory\File($docblockFactory, NodesFactory::createInstance()),
8585
new Function_($docblockFactory, [$attributeReducer, $parameterReducer]),
86-
new Interface_($docblockFactory),
86+
new Interface_($docblockFactory, [$attributeReducer]),
8787
$methodStrategy,
88-
new Property($docblockFactory, new PrettyPrinter()),
89-
new Trait_($docblockFactory),
88+
new Property($docblockFactory, new PrettyPrinter(), [$attributeReducer]),
89+
new Trait_($docblockFactory, [$attributeReducer]),
9090

9191
new IfStatement(),
9292
new TraitUse(),
9393
],
9494
);
9595

9696
$strategies->addStrategy(
97-
new ConstructorPromotion($methodStrategy, $docblockFactory, new PrettyPrinter()),
97+
new ConstructorPromotion($methodStrategy, $docblockFactory, new PrettyPrinter(), [$attributeReducer]),
9898
1100,
9999
);
100100
$strategies->addStrategy(new Noop(), -PHP_INT_MAX);

0 commit comments

Comments
 (0)