Skip to content

Commit

Permalink
Add attribute support to more elements
Browse files Browse the repository at this point in the history
  • Loading branch information
jaapio committed May 23, 2024
1 parent 5953c00 commit 61e2f1f
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ abstract class AbstractFactory implements ProjectFactoryStrategy
/** @param iterable<Reducer> $reducers */
public function __construct(
private readonly DocBlockFactoryInterface $docBlockFactory,
private readonly iterable $reducers = [],
protected readonly iterable $reducers = [],
) {
}

Expand Down
25 changes: 20 additions & 5 deletions src/phpDocumentor/Reflection/Php/Factory/ClassConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use phpDocumentor\Reflection\Php\Class_;
use phpDocumentor\Reflection\Php\Constant as ConstantElement;
use phpDocumentor\Reflection\Php\Enum_;
use phpDocumentor\Reflection\Php\Factory\Reducer\Reducer;
use phpDocumentor\Reflection\Php\Interface_;
use phpDocumentor\Reflection\Php\StrategyContainer;
use phpDocumentor\Reflection\Php\Trait_;
Expand All @@ -34,9 +35,13 @@
*/
final class ClassConstant extends AbstractFactory
{
public function __construct(DocBlockFactoryInterface $blockFactory, private readonly PrettyPrinter $valueConverter)
{
parent::__construct($blockFactory);
/** @param iterable<Reducer> $reducers */
public function __construct(
DocBlockFactoryInterface $blockFactory,
private readonly PrettyPrinter $valueConverter,
iterable $reducers = [],
) {
parent::__construct($blockFactory, $reducers);
}

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

foreach ($constants as $const) {
$constantContainer->addConstant(new ConstantElement(
$constant = new ConstantElement(
$const->getFqsen(),
$this->createDocBlock($const->getDocComment(), $context->getTypeContext()),
$const->getValue() !== null ? $this->valueConverter->prettyPrintExpr($const->getValue()) : null,
new Location($const->getLine()),
new Location($const->getEndLine()),
$this->buildVisibility($const),
$const->isFinal(),
));
);

foreach ($this->reducers as $reducer) {
$constant = $reducer->reduce($context, $const, $strategies, $constant);
}

if ($constant === null) {
continue;
}

$constantContainer->addConstant($constant);
}

return null;
Expand Down
17 changes: 14 additions & 3 deletions src/phpDocumentor/Reflection/Php/Factory/ConstructorPromotion.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use phpDocumentor\Reflection\Fqsen;
use phpDocumentor\Reflection\Location;
use phpDocumentor\Reflection\Php\Class_ as ClassElement;
use phpDocumentor\Reflection\Php\Factory\Reducer\Reducer;
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
use phpDocumentor\Reflection\Php\Property;
use phpDocumentor\Reflection\Php\StrategyContainer;
Expand All @@ -22,12 +23,14 @@

final class ConstructorPromotion extends AbstractFactory
{
/** @param iterable<Reducer> $reducers */
public function __construct(
private readonly ProjectFactoryStrategy $methodStrategy,
DocBlockFactoryInterface $docBlockFactory,
private readonly PrettyPrinter $valueConverter,
iterable $reducers = [],
) {
parent::__construct($docBlockFactory);
parent::__construct($docBlockFactory, $reducers);
}

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

$this->promoteParameterToProperty($context, $param);
$this->promoteParameterToProperty($context, $strategies, $param);
}

return $context->peek();
}

private function promoteParameterToProperty(ContextStack $context, Param $param): void
private function promoteParameterToProperty(ContextStack $context, StrategyContainer $strategies, Param $param): void
{
$methodContainer = $context->peek();
Assert::isInstanceOf($methodContainer, ClassElement::class);
Expand All @@ -75,6 +78,14 @@ private function promoteParameterToProperty(ContextStack $context, Param $param)
$this->readOnly($param->flags),
);

foreach ($this->reducers as $reducer) {
$property = $reducer->reduce($context, $param, $strategies, $property);
}

if ($property === null) {
return;
}

$methodContainer->addProperty($property);
}

Expand Down
11 changes: 8 additions & 3 deletions src/phpDocumentor/Reflection/Php/Factory/EnumCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use phpDocumentor\Reflection\Location;
use phpDocumentor\Reflection\Php\Enum_ as EnumElement;
use phpDocumentor\Reflection\Php\EnumCase as EnumCaseElement;
use phpDocumentor\Reflection\Php\Factory\Reducer\Reducer;
use phpDocumentor\Reflection\Php\StrategyContainer;
use PhpParser\Node\Stmt\EnumCase as EnumCaseNode;
use PhpParser\PrettyPrinter\Standard as PrettyPrinter;
Expand All @@ -16,9 +17,13 @@

final class EnumCase extends AbstractFactory
{
public function __construct(DocBlockFactoryInterface $docBlockFactory, private readonly PrettyPrinter $prettyPrinter)
{
parent::__construct($docBlockFactory);
/** @param iterable<Reducer> $reducers */
public function __construct(
DocBlockFactoryInterface $docBlockFactory,
private readonly PrettyPrinter $prettyPrinter,
iterable $reducers = [],
) {
parent::__construct($docBlockFactory, $reducers);
}

public function matches(ContextStack $context, object $object): bool
Expand Down
46 changes: 28 additions & 18 deletions src/phpDocumentor/Reflection/Php/Factory/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use phpDocumentor\Reflection\DocBlockFactoryInterface;
use phpDocumentor\Reflection\Location;
use phpDocumentor\Reflection\Php\Class_;
use phpDocumentor\Reflection\Php\Factory\Reducer\Reducer;
use phpDocumentor\Reflection\Php\Property as PropertyDescriptor;
use phpDocumentor\Reflection\Php\StrategyContainer;
use phpDocumentor\Reflection\Php\Trait_;
Expand All @@ -32,12 +33,13 @@
*/
final class Property extends AbstractFactory
{
/**
* Initializes the object.
*/
public function __construct(DocBlockFactoryInterface $docBlockFactory, private readonly PrettyPrinter $valueConverter)
{
parent::__construct($docBlockFactory);
/** @param iterable<Reducer> $reducers */
public function __construct(
DocBlockFactoryInterface $docBlockFactory,
private readonly PrettyPrinter $valueConverter,
iterable $reducers = [],
) {
parent::__construct($docBlockFactory, $reducers);
}

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

$propertyContainer->addProperty(
new PropertyDescriptor(
$stmt->getFqsen(),
$this->buildVisibility($stmt),
$this->createDocBlock($stmt->getDocComment(), $context->getTypeContext()),
$default,
$stmt->isStatic(),
new Location($stmt->getLine()),
new Location($stmt->getEndLine()),
(new Type())->fromPhpParser($stmt->getType()),
$stmt->isReadonly(),
),
$property = new PropertyDescriptor(
$stmt->getFqsen(),
$this->buildVisibility($stmt),
$this->createDocBlock($stmt->getDocComment(), $context->getTypeContext()),
$default,
$stmt->isStatic(),
new Location($stmt->getLine()),
new Location($stmt->getEndLine()),
(new Type())->fromPhpParser($stmt->getType()),
$stmt->isReadonly(),
);

foreach ($this->reducers as $reducer) {
$property = $reducer->reduce($context, $object, $strategies, $property);
}

if ($property === null) {
continue;
}

$propertyContainer->addProperty($property);
}

return null;
Expand Down
7 changes: 7 additions & 0 deletions src/phpDocumentor/Reflection/Php/Factory/Reducer/Reducer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@

interface Reducer
{
/**
* @param TCarry|null $carry
*
* @return TCarry|null
*
* @template TCarry of object
*/
public function reduce(
ContextStack $context,
object $object,
Expand Down
12 changes: 6 additions & 6 deletions src/phpDocumentor/Reflection/Php/ProjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 +77,24 @@ public static function createInstance(): self
new \phpDocumentor\Reflection\Php\Factory\Namespace_(),
new Class_($docblockFactory, [$attributeReducer]),
new Enum_($docblockFactory, [$attributeReducer]),
new EnumCase($docblockFactory, new PrettyPrinter()),
new EnumCase($docblockFactory, new PrettyPrinter(), [$attributeReducer]),
new Define($docblockFactory, new PrettyPrinter()),
new GlobalConstant($docblockFactory, new PrettyPrinter()),
new ClassConstant($docblockFactory, new PrettyPrinter()),
new ClassConstant($docblockFactory, new PrettyPrinter(), [$attributeReducer]),
new Factory\File($docblockFactory, NodesFactory::createInstance()),
new Function_($docblockFactory, [$attributeReducer, $parameterReducer]),
new Interface_($docblockFactory),
new Interface_($docblockFactory, [$attributeReducer]),
$methodStrategy,
new Property($docblockFactory, new PrettyPrinter()),
new Trait_($docblockFactory),
new Property($docblockFactory, new PrettyPrinter(), [$attributeReducer]),
new Trait_($docblockFactory, [$attributeReducer]),

new IfStatement(),
new TraitUse(),
],
);

$strategies->addStrategy(
new ConstructorPromotion($methodStrategy, $docblockFactory, new PrettyPrinter()),
new ConstructorPromotion($methodStrategy, $docblockFactory, new PrettyPrinter(), [$attributeReducer]),
1100,
);
$strategies->addStrategy(new Noop(), -PHP_INT_MAX);
Expand Down

0 comments on commit 61e2f1f

Please sign in to comment.