Skip to content

Commit 4dfc773

Browse files
committed
Ast: Parameter - parses promoted property modifiers
1 parent 6ae76af commit 4dfc773

11 files changed

+152
-0
lines changed

src/Ast/Parameter.php

+22
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ class Parameter implements IFunctionBody
1313
/** @var string */
1414
private $indentation;
1515

16+
/** @var IPropertyModifier[] */
17+
private $promotedPropertyModifiers;
18+
1619
/** @var NamedType|NULL */
1720
private $type;
1821

@@ -25,9 +28,11 @@ class Parameter implements IFunctionBody
2528

2629
/**
2730
* @param string $indentation
31+
* @param IPropertyModifier[] $promotedPropertyModifiers
2832
*/
2933
public function __construct(
3034
$indentation,
35+
array $promotedPropertyModifiers,
3136
NamedType $type = NULL,
3237
VariableName $name,
3338
DefaultValue $defaultValue = NULL
@@ -36,6 +41,7 @@ public function __construct(
3641
Assert::string($indentation);
3742

3843
$this->indentation = $indentation;
44+
$this->promotedPropertyModifiers = $promotedPropertyModifiers;
3945
$this->type = $type;
4046
$this->name = $name;
4147
$this->defaultValue = $defaultValue;
@@ -54,10 +60,20 @@ public function isPassedByReference(): bool
5460
}
5561

5662

63+
public function hasPromotedProperty(): bool
64+
{
65+
return count($this->promotedPropertyModifiers) > 0;
66+
}
67+
68+
5769
public function toString()
5870
{
5971
$s = $this->indentation;
6072

73+
foreach ($this->promotedPropertyModifiers as $promotedPropertyModifier) {
74+
$s .= $promotedPropertyModifier->toString();
75+
}
76+
6177
if ($this->type !== NULL) {
6278
$s .= $this->type->toString();
6379
}
@@ -78,8 +94,13 @@ public function toString()
7894
public static function parse(NodeParser $parser)
7995
{
8096
$nodeIndentation = $parser->consumeNodeIndentation();
97+
$promotedPropertyModifiers = NULL;
8198
$type = NULL;
8299

100+
if ($parser->isCurrent(T_PUBLIC, T_PROTECTED, T_PRIVATE, PhpToken::T_READONLY())) {
101+
$promotedPropertyModifiers = Modifiers::parse($parser->createSubParser());
102+
}
103+
83104
if (!$parser->isCurrent(T_VARIABLE, T_ELLIPSIS, '&', PhpToken::T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG())) {
84105
$type = NamedType::parse($parser->createSubParser());
85106
$parser->tryConsumeWhitespaceAndComments();
@@ -97,6 +118,7 @@ public static function parse(NodeParser $parser)
97118

98119
return new self(
99120
$nodeIndentation,
121+
$promotedPropertyModifiers !== NULL ? $promotedPropertyModifiers->toPropertyModifiers() : [],
100122
$type,
101123
$name,
102124
$defaultValue
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
CzProject\PhpSimpleAst\Ast\PhpString
2+
children: array (1)
3+
| 0 => CzProject\PhpSimpleAst\Ast\PhpNode
4+
| | openTag: '<?php\n'
5+
| | children: array (2)
6+
| | | 0 => CzProject\PhpSimpleAst\Ast\ClassNode
7+
| | | | indentation: '\n'
8+
| | | | keyword: 'class'
9+
| | | | name: CzProject\PhpSimpleAst\Ast\Name
10+
| | | | | indentation: ' '
11+
| | | | | name: 'Point'
12+
| | | | constructorValues: null
13+
| | | | extends: null
14+
| | | | implements: null
15+
| | | | blockOpener: ' {'
16+
| | | | children: array (1)
17+
| | | | | 0 => CzProject\PhpSimpleAst\Ast\MethodNode
18+
| | | | | | phpDocNode: null
19+
| | | | | | indentation: string
20+
| | | | | | | '\n
21+
| | | | | | | '
22+
| | | | | | modifiers: array (1)
23+
| | | | | | | 0 => CzProject\PhpSimpleAst\Ast\Visibility
24+
| | | | | | | | indentation: ''
25+
| | | | | | | | visibility: 'public'
26+
| | | | | | keywordPrefix: ' '
27+
| | | | | | keyword: 'function'
28+
| | | | | | name: CzProject\PhpSimpleAst\Ast\Name
29+
| | | | | | | indentation: ' '
30+
| | | | | | | name: '__construct'
31+
| | | | | | parameters: CzProject\PhpSimpleAst\Ast\Parameters
32+
| | | | | | | indentation: ''
33+
| | | | | | | opener: '('
34+
| | | | | | | parameters: array (2)
35+
| | | | | | | | 0 => CzProject\PhpSimpleAst\Ast\Parameter
36+
| | | | | | | | | indentation: ''
37+
| | | | | | | | | promotedPropertyModifiers: array (1)
38+
| | | | | | | | | | 0 => CzProject\PhpSimpleAst\Ast\Visibility
39+
| | | | | | | | | | | indentation: ''
40+
| | | | | | | | | | | visibility: 'protected'
41+
| | | | | | | | | type: CzProject\PhpSimpleAst\Ast\NamedType
42+
| | | | | | | | | | indentation: ' '
43+
| | | | | | | | | | nullableSign: ''
44+
| | | | | | | | | | typeIndentation: ''
45+
| | | | | | | | | | type: 'int'
46+
| | | | | | | | | name: CzProject\PhpSimpleAst\Ast\VariableName
47+
| | | | | | | | | | indentation: ' '
48+
| | | | | | | | | | referenceSign: ''
49+
| | | | | | | | | | variadic: null
50+
| | | | | | | | | | name: CzProject\PhpSimpleAst\Ast\Literal
51+
| | | | | | | | | | | indentation: ''
52+
| | | | | | | | | | | literal: '$x'
53+
| | | | | | | | | defaultValue: null
54+
| | | | | | | | 1 => CzProject\PhpSimpleAst\Ast\Parameter
55+
| | | | | | | | | indentation: ', '
56+
| | | | | | | | | promotedPropertyModifiers: array (1)
57+
| | | | | | | | | | 0 => CzProject\PhpSimpleAst\Ast\Visibility
58+
| | | | | | | | | | | indentation: ''
59+
| | | | | | | | | | | visibility: 'protected'
60+
| | | | | | | | | type: CzProject\PhpSimpleAst\Ast\NamedType
61+
| | | | | | | | | | indentation: ' '
62+
| | | | | | | | | | nullableSign: ''
63+
| | | | | | | | | | typeIndentation: ''
64+
| | | | | | | | | | type: 'int'
65+
| | | | | | | | | name: CzProject\PhpSimpleAst\Ast\VariableName
66+
| | | | | | | | | | indentation: ' '
67+
| | | | | | | | | | referenceSign: ''
68+
| | | | | | | | | | variadic: null
69+
| | | | | | | | | | name: CzProject\PhpSimpleAst\Ast\Literal
70+
| | | | | | | | | | | indentation: ''
71+
| | | | | | | | | | | literal: '$y'
72+
| | | | | | | | | defaultValue: CzProject\PhpSimpleAst\Ast\DefaultValue
73+
| | | | | | | | | | indentation: ' '
74+
| | | | | | | | | | operator: '='
75+
| | | | | | | | | | literal: CzProject\PhpSimpleAst\Ast\Literal
76+
| | | | | | | | | | | indentation: ' '
77+
| | | | | | | | | | | literal: '0'
78+
| | | | | | | closer: ')'
79+
| | | | | | returnType: null
80+
| | | | | | body: CzProject\PhpSimpleAst\Ast\FunctionBody
81+
| | | | | | | indentation: ' '
82+
| | | | | | | blockOpener: '{'
83+
| | | | | | | children: array (0)
84+
| | | | | | | blockCloser: string
85+
| | | | | | | | '\n
86+
| | | | | | | | }'
87+
| | | | blockCloser: string
88+
| | | | | '\n
89+
| | | | | }'
90+
| | | 1 => CzProject\PhpSimpleAst\Ast\UnknowNode
91+
| | | | content: '\n'
92+
| | closeTag: null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
class Point {
4+
public function __construct(protected int $x, protected int $y = 0) {
5+
}
6+
}

tests/PhpSimpleAst/fixtures/PHP-8.1/80100.readonly-properties.dump

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ CzProject\PhpSimpleAst\Ast\PhpString
5757
| | | | | | | parameters: array (1)
5858
| | | | | | | | 0 => CzProject\PhpSimpleAst\Ast\Parameter
5959
| | | | | | | | | indentation: ''
60+
| | | | | | | | | promotedPropertyModifiers: array (0)
6061
| | | | | | | | | type: CzProject\PhpSimpleAst\Ast\NamedType
6162
| | | | | | | | | | indentation: ''
6263
| | | | | | | | | | nullableSign: ''

tests/PhpSimpleAst/fixtures/Php.anonymous-classes.dump

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ CzProject\PhpSimpleAst\Ast\PhpString
4242
| | | | | | | parameters: array (1)
4343
| | | | | | | | 0 => CzProject\PhpSimpleAst\Ast\Parameter
4444
| | | | | | | | | indentation: ''
45+
| | | | | | | | | promotedPropertyModifiers: array (0)
4546
| | | | | | | | | type: null
4647
| | | | | | | | | name: CzProject\PhpSimpleAst\Ast\VariableName
4748
| | | | | | | | | | indentation: ''
@@ -112,6 +113,7 @@ CzProject\PhpSimpleAst\Ast\PhpString
112113
| | | | | | | parameters: array (1)
113114
| | | | | | | | 0 => CzProject\PhpSimpleAst\Ast\Parameter
114115
| | | | | | | | | indentation: ''
116+
| | | | | | | | | promotedPropertyModifiers: array (0)
115117
| | | | | | | | | type: null
116118
| | | | | | | | | name: CzProject\PhpSimpleAst\Ast\VariableName
117119
| | | | | | | | | | indentation: ''
@@ -241,6 +243,7 @@ CzProject\PhpSimpleAst\Ast\PhpString
241243
| | | | | | | parameters: array (1)
242244
| | | | | | | | 0 => CzProject\PhpSimpleAst\Ast\Parameter
243245
| | | | | | | | | indentation: ''
246+
| | | | | | | | | promotedPropertyModifiers: array (0)
244247
| | | | | | | | | type: null
245248
| | | | | | | | | name: CzProject\PhpSimpleAst\Ast\VariableName
246249
| | | | | | | | | | indentation: ''

tests/PhpSimpleAst/fixtures/Php.class.2.dump

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ CzProject\PhpSimpleAst\Ast\PhpString
4444
| | | | | | | parameters: array (1)
4545
| | | | | | | | 0 => CzProject\PhpSimpleAst\Ast\Parameter
4646
| | | | | | | | | indentation: ''
47+
| | | | | | | | | promotedPropertyModifiers: array (0)
4748
| | | | | | | | | type: null
4849
| | | | | | | | | name: CzProject\PhpSimpleAst\Ast\VariableName
4950
| | | | | | | | | | indentation: ''

tests/PhpSimpleAst/fixtures/Php.method.dump

+17
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ CzProject\PhpSimpleAst\Ast\PhpString
3838
| | | | | | | | | indentation: string
3939
| | | | | | | | | | '\n
4040
| | | | | | | | | | \t \t \t '
41+
| | | | | | | | | promotedPropertyModifiers: array (0)
4142
| | | | | | | | | type: CzProject\PhpSimpleAst\Ast\NamedType
4243
| | | | | | | | | | indentation: ''
4344
| | | | | | | | | | nullableSign: ''
@@ -55,6 +56,7 @@ CzProject\PhpSimpleAst\Ast\PhpString
5556
| | | | | | | | | indentation: string
5657
| | | | | | | | | | ',\n
5758
| | | | | | | | | | \t \t \t '
59+
| | | | | | | | | promotedPropertyModifiers: array (0)
5860
| | | | | | | | | type: CzProject\PhpSimpleAst\Ast\NamedType
5961
| | | | | | | | | | indentation: ''
6062
| | | | | | | | | | nullableSign: ''
@@ -72,6 +74,7 @@ CzProject\PhpSimpleAst\Ast\PhpString
7274
| | | | | | | | | indentation: string
7375
| | | | | | | | | | ',\n
7476
| | | | | | | | | | \t \t \t '
77+
| | | | | | | | | promotedPropertyModifiers: array (0)
7578
| | | | | | | | | type: CzProject\PhpSimpleAst\Ast\NamedType
7679
| | | | | | | | | | indentation: ''
7780
| | | | | | | | | | nullableSign: ''
@@ -89,6 +92,7 @@ CzProject\PhpSimpleAst\Ast\PhpString
8992
| | | | | | | | | indentation: string
9093
| | | | | | | | | | ',\n
9194
| | | | | | | | | | \t \t \t '
95+
| | | | | | | | | promotedPropertyModifiers: array (0)
9296
| | | | | | | | | type: CzProject\PhpSimpleAst\Ast\NamedType
9397
| | | | | | | | | | indentation: ''
9498
| | | | | | | | | | nullableSign: ''
@@ -106,6 +110,7 @@ CzProject\PhpSimpleAst\Ast\PhpString
106110
| | | | | | | | | indentation: string
107111
| | | | | | | | | | ',\n
108112
| | | | | | | | | | \t \t \t '
113+
| | | | | | | | | promotedPropertyModifiers: array (0)
109114
| | | | | | | | | type: CzProject\PhpSimpleAst\Ast\NamedType
110115
| | | | | | | | | | indentation: ''
111116
| | | | | | | | | | nullableSign: ''
@@ -123,6 +128,7 @@ CzProject\PhpSimpleAst\Ast\PhpString
123128
| | | | | | | | | indentation: string
124129
| | | | | | | | | | ',\n
125130
| | | | | | | | | | \t \t \t '
131+
| | | | | | | | | promotedPropertyModifiers: array (0)
126132
| | | | | | | | | type: CzProject\PhpSimpleAst\Ast\NamedType
127133
| | | | | | | | | | indentation: ''
128134
| | | | | | | | | | nullableSign: ''
@@ -140,6 +146,7 @@ CzProject\PhpSimpleAst\Ast\PhpString
140146
| | | | | | | | | indentation: string
141147
| | | | | | | | | | ',\n
142148
| | | | | | | | | | \t \t \t '
149+
| | | | | | | | | promotedPropertyModifiers: array (0)
143150
| | | | | | | | | type: CzProject\PhpSimpleAst\Ast\NamedType
144151
| | | | | | | | | | indentation: ''
145152
| | | | | | | | | | nullableSign: ''
@@ -157,6 +164,7 @@ CzProject\PhpSimpleAst\Ast\PhpString
157164
| | | | | | | | | indentation: string
158165
| | | | | | | | | | ',\n
159166
| | | | | | | | | | \t \t \t '
167+
| | | | | | | | | promotedPropertyModifiers: array (0)
160168
| | | | | | | | | type: CzProject\PhpSimpleAst\Ast\NamedType
161169
| | | | | | | | | | indentation: ''
162170
| | | | | | | | | | nullableSign: ''
@@ -174,6 +182,7 @@ CzProject\PhpSimpleAst\Ast\PhpString
174182
| | | | | | | | | indentation: string
175183
| | | | | | | | | | ',\n
176184
| | | | | | | | | | \t \t \t '
185+
| | | | | | | | | promotedPropertyModifiers: array (0)
177186
| | | | | | | | | type: CzProject\PhpSimpleAst\Ast\NamedType
178187
| | | | | | | | | | indentation: ''
179188
| | | | | | | | | | nullableSign: ''
@@ -191,6 +200,7 @@ CzProject\PhpSimpleAst\Ast\PhpString
191200
| | | | | | | | | indentation: string
192201
| | | | | | | | | | ',\n
193202
| | | | | | | | | | \t \t \t '
203+
| | | | | | | | | promotedPropertyModifiers: array (0)
194204
| | | | | | | | | type: CzProject\PhpSimpleAst\Ast\NamedType
195205
| | | | | | | | | | indentation: ''
196206
| | | | | | | | | | nullableSign: ''
@@ -208,6 +218,7 @@ CzProject\PhpSimpleAst\Ast\PhpString
208218
| | | | | | | | | indentation: string
209219
| | | | | | | | | | ',\n
210220
| | | | | | | | | | \t \t \t '
221+
| | | | | | | | | promotedPropertyModifiers: array (0)
211222
| | | | | | | | | type: CzProject\PhpSimpleAst\Ast\NamedType
212223
| | | | | | | | | | indentation: ''
213224
| | | | | | | | | | nullableSign: ''
@@ -282,6 +293,7 @@ CzProject\PhpSimpleAst\Ast\PhpString
282293
| | | | | | | parameters: array (2)
283294
| | | | | | | | 0 => CzProject\PhpSimpleAst\Ast\Parameter
284295
| | | | | | | | | indentation: ''
296+
| | | | | | | | | promotedPropertyModifiers: array (0)
285297
| | | | | | | | | type: null
286298
| | | | | | | | | name: CzProject\PhpSimpleAst\Ast\VariableName
287299
| | | | | | | | | | indentation: ''
@@ -293,6 +305,7 @@ CzProject\PhpSimpleAst\Ast\PhpString
293305
| | | | | | | | | defaultValue: null
294306
| | | | | | | | 1 => CzProject\PhpSimpleAst\Ast\Parameter
295307
| | | | | | | | | indentation: ', '
308+
| | | | | | | | | promotedPropertyModifiers: array (0)
296309
| | | | | | | | | type: null
297310
| | | | | | | | | name: CzProject\PhpSimpleAst\Ast\VariableName
298311
| | | | | | | | | | indentation: ''
@@ -362,6 +375,7 @@ CzProject\PhpSimpleAst\Ast\PhpString
362375
| | | | | | | | | indentation: string
363376
| | | | | | | | | | '\n
364377
| | | | | | | | | | \t \t \t /*A1*/'
378+
| | | | | | | | | promotedPropertyModifiers: array (0)
365379
| | | | | | | | | type: null
366380
| | | | | | | | | name: CzProject\PhpSimpleAst\Ast\VariableName
367381
| | | | | | | | | | indentation: ''
@@ -375,6 +389,7 @@ CzProject\PhpSimpleAst\Ast\PhpString
375389
| | | | | | | | | indentation: string
376390
| | | | | | | | | | '/*A2*/,\n
377391
| | | | | | | | | | \t \t \t /*B1*/'
392+
| | | | | | | | | promotedPropertyModifiers: array (0)
378393
| | | | | | | | | type: CzProject\PhpSimpleAst\Ast\NamedType
379394
| | | | | | | | | | indentation: ''
380395
| | | | | | | | | | nullableSign: ''
@@ -420,6 +435,7 @@ CzProject\PhpSimpleAst\Ast\PhpString
420435
| | | | | | | parameters: array (2)
421436
| | | | | | | | 0 => CzProject\PhpSimpleAst\Ast\Parameter
422437
| | | | | | | | | indentation: ''
438+
| | | | | | | | | promotedPropertyModifiers: array (0)
423439
| | | | | | | | | type: null
424440
| | | | | | | | | name: CzProject\PhpSimpleAst\Ast\VariableName
425441
| | | | | | | | | | indentation: ''
@@ -431,6 +447,7 @@ CzProject\PhpSimpleAst\Ast\PhpString
431447
| | | | | | | | | defaultValue: null
432448
| | | | | | | | 1 => CzProject\PhpSimpleAst\Ast\Parameter
433449
| | | | | | | | | indentation: ', '
450+
| | | | | | | | | promotedPropertyModifiers: array (0)
434451
| | | | | | | | | type: CzProject\PhpSimpleAst\Ast\NamedType
435452
| | | | | | | | | | indentation: ''
436453
| | | | | | | | | | nullableSign: ''

tests/PhpSimpleAst/fixtures/PhpExamples/traits.example-8.dump

+1
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ CzProject\PhpSimpleAst\Ast\PhpString
167167
| | | | | | | parameters: array (1)
168168
| | | | | | | | 0 => CzProject\PhpSimpleAst\Ast\Parameter
169169
| | | | | | | | | indentation: ''
170+
| | | | | | | | | promotedPropertyModifiers: array (0)
170171
| | | | | | | | | type: null
171172
| | | | | | | | | name: CzProject\PhpSimpleAst\Ast\VariableName
172173
| | | | | | | | | | indentation: ''

tests/PhpSimpleAst/fixtures/Refactoring/PhpDocParamFixer.dump

+5
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ CzProject\PhpSimpleAst\Ast\PhpString
6464
| | | | | | | | | parameters: array (5)
6565
| | | | | | | | | | 0 => CzProject\PhpSimpleAst\Ast\Parameter
6666
| | | | | | | | | | | indentation: ''
67+
| | | | | | | | | | | promotedPropertyModifiers: array (0)
6768
| | | | | | | | | | | type: null
6869
| | | | | | | | | | | name: CzProject\PhpSimpleAst\Ast\VariableName
6970
| | | | | | | | | | | | indentation: ''
@@ -75,6 +76,7 @@ CzProject\PhpSimpleAst\Ast\PhpString
7576
| | | | | | | | | | | defaultValue: null
7677
| | | | | | | | | | 1 => CzProject\PhpSimpleAst\Ast\Parameter
7778
| | | | | | | | | | | indentation: ', '
79+
| | | | | | | | | | | promotedPropertyModifiers: array (0)
7880
| | | | | | | | | | | type: null
7981
| | | | | | | | | | | name: CzProject\PhpSimpleAst\Ast\VariableName
8082
| | | | | | | | | | | | indentation: ''
@@ -86,6 +88,7 @@ CzProject\PhpSimpleAst\Ast\PhpString
8688
| | | | | | | | | | | defaultValue: null
8789
| | | | | | | | | | 2 => CzProject\PhpSimpleAst\Ast\Parameter
8890
| | | | | | | | | | | indentation: ', '
91+
| | | | | | | | | | | promotedPropertyModifiers: array (0)
8992
| | | | | | | | | | | type: null
9093
| | | | | | | | | | | name: CzProject\PhpSimpleAst\Ast\VariableName
9194
| | | | | | | | | | | | indentation: ''
@@ -97,6 +100,7 @@ CzProject\PhpSimpleAst\Ast\PhpString
97100
| | | | | | | | | | | defaultValue: null
98101
| | | | | | | | | | 3 => CzProject\PhpSimpleAst\Ast\Parameter
99102
| | | | | | | | | | | indentation: ', '
103+
| | | | | | | | | | | promotedPropertyModifiers: array (0)
100104
| | | | | | | | | | | type: CzProject\PhpSimpleAst\Ast\NamedType
101105
| | | | | | | | | | | | indentation: ''
102106
| | | | | | | | | | | | nullableSign: ''
@@ -112,6 +116,7 @@ CzProject\PhpSimpleAst\Ast\PhpString
112116
| | | | | | | | | | | defaultValue: null
113117
| | | | | | | | | | 4 => CzProject\PhpSimpleAst\Ast\Parameter
114118
| | | | | | | | | | | indentation: ', '
119+
| | | | | | | | | | | promotedPropertyModifiers: array (0)
115120
| | | | | | | | | | | type: CzProject\PhpSimpleAst\Ast\NamedType
116121
| | | | | | | | | | | | indentation: ''
117122
| | | | | | | | | | | | nullableSign: ''

0 commit comments

Comments
 (0)