Skip to content

Commit 2675d07

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

File tree

19 files changed

+163
-156
lines changed

19 files changed

+163
-156
lines changed

phpstan.neon

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ parameters:
66
ignoreErrors:
77

88
- '#Method phpDocumentor\\Reflection\\File\\LocalFile::md5\(\) should return string but returns string\|false\.#'
9-
- '#Else branch is unreachable because ternary operator condition is always true\.#'
109
#
1110
# all these $fqsen errors indicate the need for a decorator class around PhpParser\Node to hold the public $fqsen that Reflection is giving it)
1211
#

psalm-baseline.xml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@
5353
<PossiblyUndefinedMethod>
5454
<code><![CDATA[addConstant]]></code>
5555
</PossiblyUndefinedMethod>
56-
<RedundantCondition>
57-
<code><![CDATA[$const->getValue() !== null]]></code>
58-
</RedundantCondition>
5956
</file>
6057
<file src="src/phpDocumentor/Reflection/Php/Factory/ClassConstantIterator.php">
6158
<MixedReturnStatement>
@@ -115,11 +112,6 @@
115112
<code><![CDATA[$object->getAttribute('fqsen')]]></code>
116113
</MixedArgument>
117114
</file>
118-
<file src="src/phpDocumentor/Reflection/Php/Factory/GlobalConstant.php">
119-
<RedundantCondition>
120-
<code><![CDATA[$const->getValue() !== null]]></code>
121-
</RedundantCondition>
122-
</file>
123115
<file src="src/phpDocumentor/Reflection/Php/Factory/GlobalConstantIterator.php">
124116
<MixedReturnStatement>
125117
<code><![CDATA[$this->constant->consts[$this->index]->getAttribute('fqsen')]]></code>

src/phpDocumentor/Reflection/Php/Argument.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function __construct(
3838
/** @var string name of the Argument */
3939
private readonly string $name,
4040
Type|null $type = null,
41-
/** @var string|null the default value for an argument or null if none is provided */
41+
/** @var Expression|string|null the default value for an argument or null if none is provided */
4242
private Expression|string|null $default = null,
4343
/** @var bool whether the argument passes the parameter by reference instead of by value */
4444
private readonly bool $byReference = false,
@@ -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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
* for the definition of expressions in PHP.
5050
* @link https://www.rfc-editor.org/rfc/rfc6570 for more information on URI Templates.
5151
* @see ExpressionPrinter how an expression coming from PHP-Parser is transformed into an expression.
52+
*
53+
* @api
5254
*/
5355
final class Expression
5456
{
@@ -82,9 +84,7 @@ public static function generatePlaceholder(string $name): string
8284
return '{{ PHPDOC' . md5($name) . ' }}';
8385
}
8486

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

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use phpDocumentor\Reflection\Fqsen;
1717
use phpDocumentor\Reflection\Php\Expression;
1818
use phpDocumentor\Reflection\Type;
19+
use PhpParser\Node\Expr;
1920
use PhpParser\Node\Name;
2021
use PhpParser\PrettyPrinter\Standard;
2122

@@ -50,9 +51,20 @@ protected function pName_FullyQualified(Name\FullyQualified $node): string
5051
return $placeholder;
5152
}
5253

53-
/**
54-
* @return array<string, Fqsen|Type>
55-
*/
54+
protected function pExpr_ClassConstFetch(Expr\ClassConstFetch $node): string
55+
{
56+
$renderedName = parent::pObjectProperty($node->name);
57+
$className = $node->class instanceof Name ? parent::pName($node->class) : $this->p($node->class);
58+
$placeholder = Expression::generatePlaceholder($renderedName);
59+
$this->parts[$placeholder] = new Fqsen(
60+
'\\' . $className . '::' . $renderedName
61+
);
62+
63+
return $placeholder;
64+
}
65+
66+
67+
/** @return array<string, Fqsen|Type> */
5668
public function getParts(): array
5769
{
5870
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: 2 additions & 22 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,26 +103,8 @@ protected function doCreate(
105103
}
106104

107105
$propertyContainer->addProperty($property);
108-
109-
}
110-
}
111-
112-
private function determineDefault(PropertyIterator $value): Expression|null
113-
{
114-
$default = $value->getDefault();
115-
$expression = $default !== null ? $this->valueConverter->prettyPrintExpr($default) : null;
116-
if ($expression === null) {
117-
return null;
118-
}
119-
120-
if ($this->valueConverter instanceof ExpressionPrinter) {
121-
$expression = new Expression($expression, $this->valueConverter->getParts());
122-
}
123-
124-
if (is_string($expression)) {
125-
$expression = new Expression($expression, []);
126106
}
127107

128-
return $expression;
108+
return null;
129109
}
130110
}

0 commit comments

Comments
 (0)