Skip to content
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"nikic/php-parser": "~4.18 || ^5.0",
"phpdocumentor/reflection-common": "^2.1",
"phpdocumentor/reflection-docblock": "^5",
"phpdocumentor/type-resolver": "^1.2",
"phpdocumentor/type-resolver": "^1.4",
"symfony/polyfill-php80": "^1.28",
"webmozart/assert": "^1.7"
},
Expand Down
18 changes: 9 additions & 9 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 13 additions & 4 deletions src/phpDocumentor/Reflection/Php/Expression/ExpressionPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
use phpDocumentor\Reflection\FqsenResolver;
use phpDocumentor\Reflection\Php\Expression;
use phpDocumentor\Reflection\Type;
use phpDocumentor\Reflection\TypeResolver;
use phpDocumentor\Reflection\Types\Context;
use phpDocumentor\Reflection\Types\Object_;
use PhpParser\Node\Expr;
use PhpParser\Node\Name;
use PhpParser\PrettyPrinter\Standard;
Expand All @@ -27,14 +29,16 @@ final class ExpressionPrinter extends Standard
/** @var array<string, Fqsen|Type> */
private array $parts = [];
private Context|null $context = null;
private FqsenResolver $fqsenResolver;
private TypeResolver $typeResolver;

/** {@inheritDoc} */
public function __construct(array $options = [])
{
parent::__construct($options);

$this->fqsenResolver = new FqsenResolver();
$this->typeResolver = new TypeResolver(
new FqsenResolver(),
);
}

protected function resetState(): void
Expand All @@ -53,9 +57,14 @@ public function prettyPrintExpr(Expr $node, Context|null $context = null): strin

protected function pName(Name $node): string
{
$renderedName = $this->fqsenResolver->resolve(parent::pName($node), $this->context);
$renderedName = $this->typeResolver->resolve(parent::pName($node), $this->context);
$placeholder = Expression::generatePlaceholder((string) $renderedName);
$this->parts[$placeholder] = $renderedName;

if ($renderedName instanceof Object_ && $renderedName->getFqsen() !== null) {
$this->parts[$placeholder] = $renderedName->getFqsen();
} else {
$this->parts[$placeholder] = $renderedName;
}

return $placeholder;
}
Expand Down
14 changes: 14 additions & 0 deletions tests/integration/ProjectCreationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use phpDocumentor\Reflection\Php\Function_;
use phpDocumentor\Reflection\Php\ProjectFactory;
use phpDocumentor\Reflection\Types\Integer;
use phpDocumentor\Reflection\Types\Null_;
use phpDocumentor\Reflection\Types\Object_;
use phpDocumentor\Reflection\Types\String_;

Expand Down Expand Up @@ -129,6 +130,19 @@ public function testWithNamespacedClass() : void
new Object_(new Fqsen('\\Luigi\\Pizza\Style')),
$methods['\\Luigi\\Pizza::__construct()']->getArguments()[0]->getType()
);

$sauceArgument = $methods['\\Luigi\\Pizza::__construct()']->getArguments()[1];
$this->assertEquals('sauce', $sauceArgument->getName());
$this->assertEquals(
new Php\Expression(
'{{ PHPDOC37a6259cc0c1dae299a7866489dff0bd }}',
[
'{{ PHPDOC37a6259cc0c1dae299a7866489dff0bd }}' => new Null_(),
],
),
$sauceArgument->getDefault(false)
);

}

public function testDocblockOfMethodIsProcessed() : void
Expand Down
5 changes: 4 additions & 1 deletion tests/integration/data/Luigi/Pizza.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

namespace Luigi;

use Luigi\Pizza\Sauce;
use Luigi\Pizza\TomatoSauce;

#[\Food("Pizza")]
#[\Food(country: "Italy", originDate: Pizza::class)]
class Pizza extends \Pizza
Expand Down Expand Up @@ -46,7 +49,7 @@ class Pizza extends \Pizza
/** @var string $deliveryMethod Is the customer picking this pizza up or must it be delivered? */
$deliveryMethod;

private function __construct(Pizza\Style $style)
private function __construct(Pizza\Style $style, Sauce|null $sauce = null)
{
$this->style = $style;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

declare(strict_types=1);

namespace unit\phpDocumentor\Reflection\Php\Expression;

use phpDocumentor\Reflection\Fqsen;
use phpDocumentor\Reflection\Php\Expression\ExpressionPrinter;
use phpDocumentor\Reflection\PseudoTypes\True_;
use phpDocumentor\Reflection\Types\Null_;
use PhpParser\ParserFactory;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

#[CoversClass(ExpressionPrinter::class)]
final class ExpressionPrinterTest extends TestCase
{
/** @param array<string, Type> $expectedParts */
#[DataProvider('argumentProvider')]
public function testArgumentIsParsed(string $code, string $expectedExpression, array $expectedParts): void
{
$parser = (new ParserFactory())->createForHostVersion();
$node = $parser->parse($code);
$printer = new ExpressionPrinter();

$expression = $printer->prettyPrintExpr($node[0]->params[0]->default);

self::assertSame($expectedExpression, $expression);
self::assertEquals($expectedParts, $printer->getParts());
}

/**
* @return array<string, array{
* 'code': string,
* 'expectedExpression': string,
* 'expectedParts': array<string, Type>
* }>
*/
public static function argumentProvider(): array
{
return [
'myClassDefault' => [
'code' => '<?php function foo(MyClass $arg = new MyClass()) {}',
'expectedExpression' => 'new {{ PHPDOC9f1f93179bc4ea54e11fc3cda63a284f }}()',
'expectedParts' => [
'{{ PHPDOC9f1f93179bc4ea54e11fc3cda63a284f }}' => new Fqsen('\MyClass'),
],
],
// Enum case default.
// After first run, replace ENUM_PLACEHOLDER below with the actual placeholder shown in the failure output.
'enumCaseDefault' => [
'code' => '<?php function foo(MyEnum $arg = MyEnum::CaseA) {}',
'expectedExpression' => '{{ PHPDOC8844445ee68bb81ea3fd9529f906598b }}',
'expectedParts' => [
'{{ PHPDOC8844445ee68bb81ea3fd9529f906598b }}' => new Fqsen('\MyEnum::CaseA'),
],
],
'classConstantDefault' => [
'code' => '<?php function foo(MyClass $arg = MyClass::SOME_CONST) {}',
'expectedExpression' => '{{ PHPDOCe54b7c24dd0f847c3193039223751b3d }}',
'expectedParts' => [
'{{ PHPDOCe54b7c24dd0f847c3193039223751b3d }}' => new Fqsen('\MyClass::SOME_CONST'),
],
],
'stringDefault' => [
'code' => '<?php function foo(string $arg = \'hello\') {}',
'expectedExpression' => "'hello'",
'expectedParts' => [],
],
'intDefault' => [
'code' => '<?php function foo(int $arg = 42) {}',
'expectedExpression' => '42',
'expectedParts' => [],
],
'booleanDefault' => [
'code' => '<?php function foo(bool $arg = true) {}',
'expectedExpression' => '{{ PHPDOCb326b5062b2f0e69046810717534cb09 }}',
'expectedParts' => [
'{{ PHPDOCb326b5062b2f0e69046810717534cb09 }}' => new True_(),
],
],
'nullDefault' => [
'code' => '<?php function foo(bool|null $arg = null) {}',
'expectedExpression' => '{{ PHPDOC37a6259cc0c1dae299a7866489dff0bd }}',
'expectedParts' => [
'{{ PHPDOC37a6259cc0c1dae299a7866489dff0bd }}' => new Null_(),
],
],
'emptyArrayDefault' => [
'code' => '<?php function foo(array $arg = []) {}',
'expectedExpression' => '[]',
'expectedParts' => [],
],
'intArrayDefault' => [
'code' => '<?php function foo(array $arg = [1, 2]) {}',
'expectedExpression' => '[1, 2]',
'expectedParts' => [],
],
'stringArrayDefault' => [
'code' => '<?php function foo(array $arg = [\'hello\', \'world\']) {}',
'expectedExpression' => "['hello', 'world']",
'expectedParts' => [],
],
'objectArrayDefault' => [
'code' => '<?php function foo(array $arg = [new MyClass(), new MyClass()]) {}',
'expectedExpression' => '[new {{ PHPDOC9f1f93179bc4ea54e11fc3cda63a284f }}(), new {{ PHPDOC9f1f93179bc4ea54e11fc3cda63a284f }}()]',
'expectedParts' => [
'{{ PHPDOC9f1f93179bc4ea54e11fc3cda63a284f }}' => new Fqsen('\MyClass'),
],
],
];
}
}