Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move parameter building to reducer #484

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,12 @@ private function resetState(string|null $namespace = null): void
*/
private function buildName(): string
{
$name = null;
$name = '';
foreach ($this->parts as $part) {
$name .= $part;
}

return rtrim((string) $name, '\\');
return rtrim($name, '\\');
}

private function setFqsen(Node $node): void
Expand Down
6 changes: 4 additions & 2 deletions src/phpDocumentor/Reflection/Php/Factory/AbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
abstract class AbstractFactory implements ProjectFactoryStrategy
{
/** @param iterable<Reducer> $reducers */
public function __construct(private readonly DocBlockFactoryInterface $docBlockFactory, private readonly iterable $reducers = [])
{
public function __construct(
private readonly DocBlockFactoryInterface $docBlockFactory,
private readonly iterable $reducers = [],
) {
}

/**
Expand Down
83 changes: 0 additions & 83 deletions src/phpDocumentor/Reflection/Php/Factory/Argument.php

This file was deleted.

9 changes: 2 additions & 7 deletions src/phpDocumentor/Reflection/Php/Factory/Function_.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,11 @@ protected function doCreate(

$file->addFunction($function);

$thisContext = $context->push($function);
foreach ($object->params as $param) {
$strategy = $strategies->findMatching($thisContext, $param);
$strategy->create($thisContext, $param, $strategies);
}

if (!is_array($object->stmts)) {
return null;
return $function;
}

$thisContext = $context->push($function);
foreach ($object->stmts as $stmt) {
$strategy = $strategies->findMatching($thisContext, $stmt);
$strategy->create($thisContext, $stmt, $strategies);
Expand Down
9 changes: 2 additions & 7 deletions src/phpDocumentor/Reflection/Php/Factory/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,11 @@ protected function doCreate(
);
$methodContainer->addMethod($method);

$thisContext = $context->push($method);
foreach ($object->params as $param) {
$strategy = $strategies->findMatching($thisContext, $param);
$strategy->create($thisContext, $param, $strategies);
}

if (!is_array($object->stmts)) {
return null;
return $method;
}

$thisContext = $context->push($method);
foreach ($object->stmts as $stmt) {
$strategy = $strategies->findMatching($thisContext, $stmt);
$strategy->create($thisContext, $stmt, $strategies);
Expand Down
56 changes: 56 additions & 0 deletions src/phpDocumentor/Reflection/Php/Factory/Reducer/Parameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace phpDocumentor\Reflection\Php\Factory\Reducer;

use phpDocumentor\Reflection\Php\Argument as ArgumentDescriptor;
use phpDocumentor\Reflection\Php\Factory\ContextStack;
use phpDocumentor\Reflection\Php\Factory\Type;
use phpDocumentor\Reflection\Php\Function_;
use phpDocumentor\Reflection\Php\Method;
use phpDocumentor\Reflection\Php\StrategyContainer;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\FunctionLike;
use PhpParser\PrettyPrinter\Standard as PrettyPrinter;
use Webmozart\Assert\Assert;

use function is_string;

class Parameter implements Reducer
{
public function __construct(private readonly PrettyPrinter $valueConverter)
{
}

public function reduce(
ContextStack $context,
object $object,
StrategyContainer $strategies,
object|null $carry,
): object|null {
if ($object instanceof FunctionLike === false) {
return $carry;
}

if ($carry instanceof Method === false && $carry instanceof Function_ === false) {
return null;
}

foreach ($object->getParams() as $param) {
Assert::isInstanceOf($param->var, Variable::class);

$carry->addArgument(
new ArgumentDescriptor(
is_string($param->var->name) ? $param->var->name : $this->valueConverter->prettyPrintExpr($param->var->name),
(new Type())->fromPhpParser($param->type),
$param->default !== null ? $this->valueConverter->prettyPrintExpr($param->default) : null,
$param->byRef,
$param->variadic,
),
);
}

return $carry;
}
}
18 changes: 9 additions & 9 deletions src/phpDocumentor/Reflection/Php/ProjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use phpDocumentor\Reflection\Exception;
use phpDocumentor\Reflection\File as SourceFile;
use phpDocumentor\Reflection\Fqsen;
use phpDocumentor\Reflection\Php\Factory\Argument;
use phpDocumentor\Reflection\Php\Factory\Class_;
use phpDocumentor\Reflection\Php\Factory\ClassConstant;
use phpDocumentor\Reflection\Php\Factory\ConstructorPromotion;
Expand All @@ -33,6 +32,7 @@
use phpDocumentor\Reflection\Php\Factory\Noop;
use phpDocumentor\Reflection\Php\Factory\Property;
use phpDocumentor\Reflection\Php\Factory\Reducer\Attribute;
use phpDocumentor\Reflection\Php\Factory\Reducer\Parameter;
use phpDocumentor\Reflection\Php\Factory\Trait_;
use phpDocumentor\Reflection\Php\Factory\TraitUse;
use phpDocumentor\Reflection\Project as ProjectInterface;
Expand Down Expand Up @@ -67,20 +67,22 @@ public static function createInstance(): self
{
$docblockFactory = DocBlockFactory::createInstance();

$methodStrategy = new Method($docblockFactory);
$attributeReducer = new Attribute();
$parameterReducer = new Parameter(new PrettyPrinter());

$methodStrategy = new Method($docblockFactory, [$attributeReducer, $parameterReducer]);

$strategies = new ProjectFactoryStrategies(
[
new \phpDocumentor\Reflection\Php\Factory\Namespace_(),
new Argument(new PrettyPrinter()),
new Class_($docblockFactory, [new Attribute()]),
new Enum_($docblockFactory),
new Class_($docblockFactory, [$attributeReducer]),
new Enum_($docblockFactory, [$attributeReducer]),
new EnumCase($docblockFactory, new PrettyPrinter()),
new Define($docblockFactory, new PrettyPrinter()),
new GlobalConstant($docblockFactory, new PrettyPrinter()),
new ClassConstant($docblockFactory, new PrettyPrinter()),
new Factory\File($docblockFactory, NodesFactory::createInstance()),
new Function_($docblockFactory),
new Function_($docblockFactory, [$attributeReducer, $parameterReducer]),
new Interface_($docblockFactory),
$methodStrategy,
new Property($docblockFactory, new PrettyPrinter()),
Expand All @@ -97,9 +99,7 @@ public static function createInstance(): self
);
$strategies->addStrategy(new Noop(), -PHP_INT_MAX);

return new static(
$strategies,
);
return new self($strategies);
}

public function addStrategy(
Expand Down
77 changes: 0 additions & 77 deletions tests/unit/phpDocumentor/Reflection/Php/Factory/ArgumentTest.php

This file was deleted.

37 changes: 0 additions & 37 deletions tests/unit/phpDocumentor/Reflection/Php/Factory/Function_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
use phpDocumentor\Reflection\Php\StrategyContainer;
use PhpParser\Comment\Doc;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Expression;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
Expand Down Expand Up @@ -87,41 +85,6 @@ public function testCreateWithoutParameters(): void
$this->assertEquals('\SomeSpace::function()', (string) $function->getFqsen());
}

/** @covers ::create */
public function testCreateWithParameters(): void
{
$param1 = new Param(new Variable('param1'));
$functionMock = $this->prophesize(\PhpParser\Node\Stmt\Function_::class);
$functionMock->getAttribute('fqsen')->willReturn(new Fqsen('\SomeSpace::function()'));
$functionMock->params = [$param1];
$functionMock->getDocComment()->willReturn(null);
$functionMock->getLine()->willReturn(1);
$functionMock->getEndLine()->willReturn(2);
$functionMock->getReturnType()->willReturn(null);

$argumentStrategy = $this->prophesize(ProjectFactoryStrategy::class);
$containerMock = $this->prophesize(StrategyContainer::class);
$containerMock->findMatching(Argument::type(ContextStack::class), $param1)->willReturn($argumentStrategy);
$argumentStrategy->create(
Argument::that(fn ($agument): bool => $agument->peek() instanceof FunctionDescriptor),
$param1,
$containerMock->reveal(),
)->shouldBeCalled();

$file = new File('hash', 'path');

$this->fixture->create(
self::createContext(null)->push($file),
$functionMock->reveal(),
$containerMock->reveal(),
);

$function = current($file->getFunctions());

self::assertInstanceOf(FunctionDescriptor::class, $function);
$this->assertEquals('\SomeSpace::function()', (string) $function->getFqsen());
}

/** @covers ::create */
public function testCreateWithDocBlock(): void
{
Expand Down
Loading