Skip to content

Commit e352fb3

Browse files
authored
PHPLIB-1597 Accept a $-prefixed string anywhere an expression is accepted (#1571)
1 parent dacd06e commit e352fb3

File tree

136 files changed

+2183
-1066
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

136 files changed

+2183
-1066
lines changed

generator/config/accumulator/accumulator.yaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ tests:
115115
}
116116
return state;
117117
}
118-
accumulateArgs:
119-
- '$name'
118+
accumulateArgs: ['$name']
120119
merge:
121120
$code: |-
122121
function(state1, state2) {

generator/src/OperatorClassGenerator.php

+12
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,18 @@ public function createClass(GeneratorDefinition $definition, OperatorDefinition
135135
$constructorParam->setDefaultValue($argument->default);
136136
}
137137

138+
if ($type->dollarPrefixedString) {
139+
$namespace->addUseFunction('is_string');
140+
$namespace->addUseFunction('str_starts_with');
141+
$namespace->addUse(InvalidArgumentException::class);
142+
$constructor->addBody(<<<PHP
143+
if (is_string(\${$argument->propertyName}) && ! str_starts_with(\${$argument->propertyName}, '$')) {
144+
throw new InvalidArgumentException('Argument \${$argument->propertyName} can be an expression, field paths and variable names must be prefixed by "$" or "$$".');
145+
}
146+
147+
PHP);
148+
}
149+
138150
// List type must be validated with array_is_list()
139151
if ($type->list) {
140152
$namespace->addUseFunction('is_array');

generator/src/OperatorGenerator.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use function ltrim;
2727
use function sort;
2828
use function sprintf;
29+
use function str_starts_with;
2930
use function ucfirst;
3031
use function usort;
3132

@@ -71,13 +72,19 @@ final protected function getType(string $type): ExpressionDefinition
7172
* Expression types can contain class names, interface, native types or "list".
7273
* PHPDoc types are more precise than native types, so we use them systematically even if redundant.
7374
*
74-
* @return object{native:string,doc:string,use:list<class-string>,list:bool,query:bool,javascript:bool}
75+
* @return object{native:string,doc:string,use:list<class-string>,list:bool,query:bool,javascript:bool,dollarPrefixedString:bool}
7576
*/
7677
final protected function getAcceptedTypes(ArgumentDefinition $arg): stdClass
7778
{
7879
$nativeTypes = [];
7980

81+
$dollarPrefixedString = false;
82+
8083
foreach ($arg->type as $type) {
84+
if (str_starts_with($type, 'resolvesTo')) {
85+
$dollarPrefixedString = true;
86+
}
87+
8188
$type = $this->getType($type);
8289
$nativeTypes = array_merge($nativeTypes, $type->acceptedTypes);
8390

@@ -91,6 +98,14 @@ final protected function getAcceptedTypes(ArgumentDefinition $arg): stdClass
9198
$nativeTypes[] = Optional::class;
9299
}
93100

101+
// If the argument accepts an expression, a $-prefixed string is accepted (field path or variable)
102+
// Checked only if the argument does not already accept a string
103+
if (in_array('string', $nativeTypes, true)) {
104+
$dollarPrefixedString = false;
105+
} elseif ($dollarPrefixedString) {
106+
$nativeTypes[] = 'string';
107+
}
108+
94109
$docTypes = $nativeTypes = array_unique($nativeTypes);
95110
$use = [];
96111

@@ -131,6 +146,7 @@ final protected function getAcceptedTypes(ArgumentDefinition $arg): stdClass
131146
'list' => $listCheck,
132147
'query' => $isQuery,
133148
'javascript' => $isJavascript,
149+
'dollarPrefixedString' => $dollarPrefixedString,
134150
];
135151
}
136152

src/Builder/Accumulator/AccumulatorAccumulator.php

+17-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/Accumulator/AvgAccumulator.php

+12-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/Accumulator/BottomNAccumulator.php

+12-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/Accumulator/CovariancePopAccumulator.php

+20-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/Accumulator/CovarianceSampAccumulator.php

+20-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)