Skip to content

Commit d74bc26

Browse files
committed
Fix code style and tests
1 parent cefb90d commit d74bc26

File tree

17 files changed

+146
-127
lines changed

17 files changed

+146
-127
lines changed

src/phpDocumentor/Reflection/Php/Argument.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function __construct(
5353
trigger_error(
5454
'Default values for arguments should be of type Expression, support for strings will be '
5555
. 'removed in 7.x',
56-
E_USER_DEPRECATED
56+
E_USER_DEPRECATED,
5757
);
5858
$this->default = new Expression($this->default, []);
5959
}
@@ -74,8 +74,7 @@ public function getType(): Type|null
7474
return $this->type;
7575
}
7676

77-
/** */
78-
public function getDefault(bool $asString = true): string|null
77+
public function getDefault(bool $asString = true): Expression|string|null
7978
{
8079
if ($this->default === null) {
8180
return null;
@@ -84,7 +83,7 @@ public function getDefault(bool $asString = true): string|null
8483
if ($asString) {
8584
trigger_error(
8685
'The Default value will become of type Expression by default',
87-
E_USER_DEPRECATED
86+
E_USER_DEPRECATED,
8887
);
8988

9089
return (string) $this->default;

src/phpDocumentor/Reflection/Php/Constant.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ final class Constant implements Element, MetaDataContainerInterface, AttributeCo
4343

4444
/**
4545
* Initializes the object.
46-
*
47-
* @param Expression|string|null $value
4846
*/
4947
public function __construct(
5048
private readonly Fqsen $fqsen,
@@ -59,14 +57,16 @@ public function __construct(
5957
$this->endLocation = $endLocation ?: new Location(-1);
6058
$this->visibility = $visibility ?: new Visibility(Visibility::PUBLIC_);
6159

62-
if (is_string($this->value)) {
63-
trigger_error(
64-
'Constant values should be of type Expression, support for strings will be '
65-
. 'removed in 6.x',
66-
E_USER_DEPRECATED
67-
);
68-
$this->value = new Expression($this->value, []);
60+
if (!is_string($this->value)) {
61+
return;
6962
}
63+
64+
trigger_error(
65+
'Constant values should be of type Expression, support for strings will be '
66+
. 'removed in 6.x',
67+
E_USER_DEPRECATED,
68+
);
69+
$this->value = new Expression($this->value, []);
7070
}
7171

7272
/**
@@ -81,7 +81,7 @@ public function getValue(bool $asString = true): Expression|string|null
8181
if ($asString) {
8282
trigger_error(
8383
'The expression value will become of type Expression by default',
84-
E_USER_DEPRECATED
84+
E_USER_DEPRECATED,
8585
);
8686

8787
return (string) $this->value;

src/phpDocumentor/Reflection/Php/EnumCase.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,16 @@ public function __construct(
4747

4848
$this->location = $location;
4949
$this->endLocation = $endLocation;
50-
if (is_string($this->value)) {
51-
trigger_error(
52-
'Expression values for enum cases should be of type Expression, support for strings will be '
53-
. 'removed in 7.x',
54-
E_USER_DEPRECATED
55-
);
56-
$this->value = new Expression($this->value, []);
50+
if (!is_string($this->value)) {
51+
return;
5752
}
53+
54+
trigger_error(
55+
'Expression values for enum cases should be of type Expression, support for strings will be '
56+
. 'removed in 7.x',
57+
E_USER_DEPRECATED,
58+
);
59+
$this->value = new Expression($this->value, []);
5860
}
5961

6062
#[Override]
@@ -96,7 +98,7 @@ public function getValue(bool $asString = true): Expression|string|null
9698
if ($asString) {
9799
trigger_error(
98100
'The enum case value will become of type Expression by default',
99-
E_USER_DEPRECATED
101+
E_USER_DEPRECATED,
100102
);
101103

102104
return (string) $this->value;

src/phpDocumentor/Reflection/Php/Expression.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,7 @@ public static function generatePlaceholder(string $name): string
8282
return '{{ PHPDOC' . md5($name) . ' }}';
8383
}
8484

85-
/**
86-
* @param array<string, Fqsen|Type> $parts
87-
*/
85+
/** @param array<string, Fqsen|Type> $parts */
8886
public function __construct(string $expression, array $parts = [])
8987
{
9088
Assert::notEmpty($expression);

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ protected function pName_FullyQualified(Name\FullyQualified $node): string
5050
return $placeholder;
5151
}
5252

53-
/**
54-
* @return array<string, Fqsen|Type>
55-
*/
53+
/** @return array<string, Fqsen|Type> */
5654
public function getParts(): array
5755
{
5856
return $this->parts;

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

Whitespace-only changes.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
use phpDocumentor\Reflection\Php\Class_;
2020
use phpDocumentor\Reflection\Php\Constant as ConstantElement;
2121
use phpDocumentor\Reflection\Php\Enum_;
22-
use phpDocumentor\Reflection\Php\Factory\Reducer\Reducer;
2322
use phpDocumentor\Reflection\Php\Expression;
2423
use phpDocumentor\Reflection\Php\Expression\ExpressionPrinter;
24+
use phpDocumentor\Reflection\Php\Factory\Reducer\Reducer;
2525
use phpDocumentor\Reflection\Php\Interface_;
2626
use phpDocumentor\Reflection\Php\StrategyContainer;
2727
use phpDocumentor\Reflection\Php\Trait_;
@@ -109,7 +109,7 @@ protected function doCreate(
109109
return null;
110110
}
111111

112-
private function determineValue(ClassConstantIterator $value): Expression|null
112+
private function determineValue(ClassConstantIterator $value): Expression
113113
{
114114
$expression = $this->valueConverter->prettyPrintExpr($value->getValue());
115115
if ($this->valueConverter instanceof ExpressionPrinter) {

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

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
use phpDocumentor\Reflection\Location;
1212
use phpDocumentor\Reflection\Php\Class_ as ClassElement;
1313
use phpDocumentor\Reflection\Php\Factory\Reducer\Reducer;
14-
use phpDocumentor\Reflection\Php\Expression;
15-
use phpDocumentor\Reflection\Php\Expression\ExpressionPrinter;
1614
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
1715
use phpDocumentor\Reflection\Php\StrategyContainer;
1816
use PhpParser\Modifiers;
@@ -22,8 +20,6 @@
2220
use PhpParser\PrettyPrinter\Standard as PrettyPrinter;
2321
use Webmozart\Assert\Assert;
2422

25-
use function is_string;
26-
2723
final class ConstructorPromotion extends AbstractFactory
2824
{
2925
/** @param iterable<Reducer> $reducers */
@@ -80,11 +76,11 @@ private function promoteParameterToProperty(ContextStack $context, StrategyConta
8076
->visibility($param)
8177
->type($param->type)
8278
->docblock($param->getDocComment())
83-
->default($this->determineDefault($param))
79+
->default($param->default)
8480
->readOnly($this->readOnly($param->flags))
8581
->static(false)
86-
->startLocation(new Location($param->getLine(), $param->getStartFilePos()))
87-
->endLocation(new Location($param->getEndLine(), $param->getEndFilePos()))
82+
->startLocation(new Location($param->getLine()))
83+
->endLocation(new Location($param->getEndLine()))
8884
->hooks($param->hooks ?? [])
8985
->build($context);
9086

@@ -99,24 +95,6 @@ private function promoteParameterToProperty(ContextStack $context, StrategyConta
9995
$methodContainer->addProperty($property);
10096
}
10197

102-
private function determineDefault(Param $value): Expression|null
103-
{
104-
$expression = $value->default !== null ? $this->valueConverter->prettyPrintExpr($value->default) : null;
105-
if ($expression === null) {
106-
return null;
107-
}
108-
109-
if ($this->valueConverter instanceof ExpressionPrinter) {
110-
$expression = new Expression($expression, $this->valueConverter->getParts());
111-
}
112-
113-
if (is_string($expression)) {
114-
$expression = new Expression($expression, []);
115-
}
116-
117-
return $expression;
118-
}
119-
12098
private function readOnly(int $flags): bool
12199
{
122100
return (bool) ($flags & Modifiers::READONLY) === true;

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
use phpDocumentor\Reflection\Location;
1010
use phpDocumentor\Reflection\Php\Enum_ as EnumElement;
1111
use phpDocumentor\Reflection\Php\EnumCase as EnumCaseElement;
12-
use phpDocumentor\Reflection\Php\Factory\Reducer\Reducer;
1312
use phpDocumentor\Reflection\Php\Expression as ValueExpression;
1413
use phpDocumentor\Reflection\Php\Expression\ExpressionPrinter;
14+
use phpDocumentor\Reflection\Php\Factory\Reducer\Reducer;
1515
use phpDocumentor\Reflection\Php\StrategyContainer;
1616
use PhpParser\Node\Stmt\EnumCase as EnumCaseNode;
1717
use PhpParser\PrettyPrinter\Standard as PrettyPrinter;
@@ -44,13 +44,17 @@ protected function doCreate(ContextStack $context, object $object, StrategyConta
4444
$enum = $context->peek();
4545
assert($enum instanceof EnumElement);
4646

47-
$enum->addCase(new EnumCaseElement(
47+
$case = new EnumCaseElement(
4848
$object->getAttribute('fqsen'),
4949
$docBlock,
5050
new Location($object->getLine()),
5151
new Location($object->getEndLine()),
5252
$this->determineValue($object),
53-
));
53+
);
54+
55+
$enum->addCase($case);
56+
57+
return $case;
5458
}
5559

5660
private function determineValue(EnumCaseNode $value): ValueExpression|null

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
use phpDocumentor\Reflection\Location;
1919
use phpDocumentor\Reflection\Php\Class_;
2020
use phpDocumentor\Reflection\Php\Factory\Reducer\Reducer;
21-
use phpDocumentor\Reflection\Php\Expression;
22-
use phpDocumentor\Reflection\Php\Expression\ExpressionPrinter;
2321
use phpDocumentor\Reflection\Php\Property as PropertyDescriptor;
2422
use phpDocumentor\Reflection\Php\StrategyContainer;
2523
use phpDocumentor\Reflection\Php\Trait_;
@@ -88,7 +86,7 @@ protected function doCreate(
8886
->visibility($stmt)
8987
->type($stmt->getType())
9088
->docblock($stmt->getDocComment())
91-
->default($this->determineDefault($stmt))
89+
->default($stmt->getDefault())
9290
->static($stmt->isStatic())
9391
->startLocation(new Location($stmt->getLine()))
9492
->endLocation(new Location($stmt->getEndLine()))
@@ -105,8 +103,9 @@ protected function doCreate(
105103
}
106104

107105
$propertyContainer->addProperty($property);
108-
109106
}
107+
108+
return $property;
110109
}
111110

112111
private function determineDefault(PropertyIterator $value): Expression|null

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use phpDocumentor\Reflection\Location;
1010
use phpDocumentor\Reflection\NodeVisitor\FindingVisitor;
1111
use phpDocumentor\Reflection\Php\AsymmetricVisibility;
12+
use phpDocumentor\Reflection\Php\Expression;
13+
use phpDocumentor\Reflection\Php\Expression\ExpressionPrinter;
1214
use phpDocumentor\Reflection\Php\Factory\Reducer\Reducer;
1315
use phpDocumentor\Reflection\Php\Property as PropertyElement;
1416
use phpDocumentor\Reflection\Php\PropertyHook;
@@ -26,11 +28,12 @@
2628
use PhpParser\Node\Param;
2729
use PhpParser\Node\PropertyHook as PropertyHookNode;
2830
use PhpParser\NodeTraverser;
29-
use PhpParser\PrettyPrinter\Standard as PrettyPrinter;
31+
use PhpParser\PrettyPrinter;
3032

3133
use function array_filter;
3234
use function array_map;
3335
use function count;
36+
use function is_string;
3437
use function method_exists;
3538

3639
/**
@@ -159,7 +162,7 @@ public function build(ContextStack $context): PropertyElement
159162
$this->fqsen,
160163
$this->visibility,
161164
$this->docblock !== null ? $this->docBlockFactory->create($this->docblock->getText(), $context->getTypeContext()) : null,
162-
$this->default !== null ? $this->valueConverter->prettyPrintExpr($this->default) : null,
165+
$this->determineDefault(),
163166
$this->static,
164167
$this->startLocation,
165168
$this->endLocation,
@@ -341,4 +344,22 @@ private function buildHookVisibility(string $hookName, Visibility $propertyVisib
341344
default => $propertyVisibility,
342345
};
343346
}
347+
348+
private function determineDefault(): Expression|null
349+
{
350+
$expression = $this->default !== null ? $this->valueConverter->prettyPrintExpr($this->default) : null;
351+
if ($expression === null) {
352+
return null;
353+
}
354+
355+
if ($this->valueConverter instanceof ExpressionPrinter) {
356+
$expression = new Expression($expression, $this->valueConverter->getParts());
357+
}
358+
359+
if (is_string($expression)) {
360+
$expression = new Expression($expression, []);
361+
}
362+
363+
return $expression;
364+
}
344365
}

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use Override;
88
use phpDocumentor\Reflection\Php\Argument as ArgumentDescriptor;
9+
use phpDocumentor\Reflection\Php\Expression;
10+
use phpDocumentor\Reflection\Php\Expression\ExpressionPrinter;
911
use phpDocumentor\Reflection\Php\Factory\ContextStack;
1012
use phpDocumentor\Reflection\Php\Factory\Type;
1113
use phpDocumentor\Reflection\Php\Function_;
@@ -14,6 +16,7 @@
1416
use phpDocumentor\Reflection\Php\StrategyContainer;
1517
use PhpParser\Node\Expr\Variable;
1618
use PhpParser\Node\FunctionLike;
19+
use PhpParser\Node\Param;
1720
use PhpParser\PrettyPrinter\Standard as PrettyPrinter;
1821
use Webmozart\Assert\Assert;
1922

@@ -47,7 +50,7 @@ public function reduce(
4750
new ArgumentDescriptor(
4851
is_string($param->var->name) ? $param->var->name : $this->valueConverter->prettyPrintExpr($param->var->name),
4952
(new Type())->fromPhpParser($param->type),
50-
$param->default !== null ? $this->valueConverter->prettyPrintExpr($param->default) : null,
53+
$this->determineDefault($param),
5154
$param->byRef,
5255
$param->variadic,
5356
),
@@ -56,4 +59,22 @@ public function reduce(
5659

5760
return $carry;
5861
}
62+
63+
private function determineDefault(Param $value): Expression|null
64+
{
65+
$expression = $value->default !== null ? $this->valueConverter->prettyPrintExpr($value->default) : null;
66+
if ($expression === null) {
67+
return null;
68+
}
69+
70+
if ($this->valueConverter instanceof ExpressionPrinter) {
71+
$expression = new Expression($expression, $this->valueConverter->getParts());
72+
}
73+
74+
if (is_string($expression)) {
75+
$expression = new Expression($expression, []);
76+
}
77+
78+
return $expression;
79+
}
5980
}

src/phpDocumentor/Reflection/Php/ProjectFactory.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public static function createInstance(): self
7070
$expressionPrinter = new ExpressionPrinter();
7171

7272
$attributeReducer = new Attribute();
73-
$parameterReducer = new Parameter(new PrettyPrinter());
73+
$parameterReducer = new Parameter($expressionPrinter);
7474

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

@@ -79,10 +79,10 @@ public static function createInstance(): self
7979
new \phpDocumentor\Reflection\Php\Factory\Namespace_(),
8080
new Class_($docblockFactory, [$attributeReducer]),
8181
new Enum_($docblockFactory, [$attributeReducer]),
82-
new EnumCase($docblockFactory, new PrettyPrinter(), [$attributeReducer]),
83-
new Define($docblockFactory, new PrettyPrinter()),
84-
new GlobalConstant($docblockFactory, new PrettyPrinter()),
85-
new ClassConstant($docblockFactory, new PrettyPrinter(), [$attributeReducer]),
82+
new EnumCase($docblockFactory, $expressionPrinter, [$attributeReducer]),
83+
new Define($docblockFactory, $expressionPrinter),
84+
new GlobalConstant($docblockFactory, $expressionPrinter),
85+
new ClassConstant($docblockFactory, $expressionPrinter, [$attributeReducer]),
8686
new Factory\File($docblockFactory, NodesFactory::createInstance()),
8787
new Function_($docblockFactory, [$attributeReducer, $parameterReducer]),
8888
new Interface_($docblockFactory, [$attributeReducer]),

0 commit comments

Comments
 (0)