Skip to content

Commit 131c6ee

Browse files
committed
Context aware fqsen resolving on parameters
1 parent d5aea24 commit 131c6ee

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

src/phpDocumentor/Reflection/Php/Expression/ExpressionPrinter.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
namespace phpDocumentor\Reflection\Php\Expression;
1515

1616
use phpDocumentor\Reflection\Fqsen;
17+
use phpDocumentor\Reflection\FqsenResolver;
1718
use phpDocumentor\Reflection\Php\Expression;
1819
use phpDocumentor\Reflection\Type;
20+
use phpDocumentor\Reflection\Types\Context;
1921
use PhpParser\Node\Expr;
2022
use PhpParser\Node\Name;
2123
use PhpParser\PrettyPrinter\Standard;
@@ -24,6 +26,15 @@ final class ExpressionPrinter extends Standard
2426
{
2527
/** @var array<string, Fqsen|Type> */
2628
private array $parts = [];
29+
private Context|null $context = null;
30+
private FqsenResolver $fqsenResolver;
31+
32+
public function __construct(array $options = [])
33+
{
34+
parent::__construct($options);
35+
36+
$this->fqsenResolver = new FqsenResolver();
37+
}
2738

2839
protected function resetState(): void
2940
{
@@ -32,11 +43,18 @@ protected function resetState(): void
3243
$this->parts = [];
3344
}
3445

46+
public function prettyPrintExpr(Expr $node, Context|null $context = null): string
47+
{
48+
$this->context = $context;
49+
50+
return parent::prettyPrintExpr($node);
51+
}
52+
3553
protected function pName(Name $node): string
3654
{
37-
$renderedName = parent::pName($node);
38-
$placeholder = Expression::generatePlaceholder($renderedName);
39-
$this->parts[$placeholder] = new Fqsen('\\' . $renderedName);
55+
$renderedName = $this->fqsenResolver->resolve(parent::pName($node), $this->context);
56+
$placeholder = Expression::generatePlaceholder((string) $renderedName);
57+
$this->parts[$placeholder] = $renderedName;
4058

4159
return $placeholder;
4260
}

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use phpDocumentor\Reflection\Php\PropertyHook;
1717
use phpDocumentor\Reflection\Php\StrategyContainer;
1818
use phpDocumentor\Reflection\Php\Visibility;
19+
use phpDocumentor\Reflection\Types\Context;
1920
use PhpParser\Comment\Doc;
2021
use PhpParser\Modifiers;
2122
use PhpParser\Node;
@@ -163,7 +164,7 @@ public function build(ContextStack $context): PropertyElement
163164
$this->fqsen,
164165
$this->visibility,
165166
$this->docblock !== null ? $this->docBlockFactory->create($this->docblock->getText(), $context->getTypeContext()) : null,
166-
$this->determineDefault(),
167+
$this->determineDefault($context->getTypeContext()),
167168
$this->static,
168169
$this->startLocation,
169170
$this->endLocation,
@@ -346,9 +347,14 @@ private function buildHookVisibility(string $hookName, Visibility $propertyVisib
346347
};
347348
}
348349

349-
private function determineDefault(): Expression|null
350+
private function determineDefault(Context|null $context): Expression|null
350351
{
351-
$expression = $this->default !== null ? $this->valueConverter->prettyPrintExpr($this->default) : null;
352+
if ($this->valueConverter instanceof ExpressionPrinter) {
353+
$expression = $this->default !== null ? $this->valueConverter->prettyPrintExpr($this->default, $context) : null;
354+
} else {
355+
$expression = $this->default !== null ? $this->valueConverter->prettyPrintExpr($this->default) : null;
356+
}
357+
352358
if ($expression === null) {
353359
return null;
354360
}

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use phpDocumentor\Reflection\Php\Method;
1515
use phpDocumentor\Reflection\Php\PropertyHook;
1616
use phpDocumentor\Reflection\Php\StrategyContainer;
17+
use phpDocumentor\Reflection\Types\Context;
1718
use PhpParser\Node\Expr\Variable;
1819
use PhpParser\Node\FunctionLike;
1920
use PhpParser\Node\Param;
@@ -50,7 +51,7 @@ public function reduce(
5051
new ArgumentDescriptor(
5152
is_string($param->var->name) ? $param->var->name : $this->valueConverter->prettyPrintExpr($param->var->name),
5253
(new Type())->fromPhpParser($param->type),
53-
$this->determineDefault($param),
54+
$this->determineDefault($param, $context->getTypeContext()),
5455
$param->byRef,
5556
$param->variadic,
5657
),
@@ -60,9 +61,14 @@ public function reduce(
6061
return $carry;
6162
}
6263

63-
private function determineDefault(Param $value): Expression|null
64+
private function determineDefault(Param $value, Context|null $context): Expression|null
6465
{
65-
$expression = $value->default !== null ? $this->valueConverter->prettyPrintExpr($value->default) : null;
66+
if ($this->valueConverter instanceof ExpressionPrinter) {
67+
$expression = $value->default !== null ? $this->valueConverter->prettyPrintExpr($value->default, $context) : null;
68+
} else {
69+
$expression = $value->default !== null ? $this->valueConverter->prettyPrintExpr($value->default) : null;
70+
}
71+
6672
if ($expression === null) {
6773
return null;
6874
}

0 commit comments

Comments
 (0)