Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHPLIB-1597 Accept a $-prefixed string anywhere an expression is accepted #1571

Open
wants to merge 3 commits into
base: v1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions generator/config/accumulator/accumulator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ tests:
function(state, numCopies) {
return { count: state.count + 1, sum: state.sum + numCopies }
}
accumulateArgs: [ "$copies" ],
accumulateArgs: [ "$copies" ]
merge:
$code: |-
function(state1, state2) {
Expand Down Expand Up @@ -115,8 +115,7 @@ tests:
}
return state;
}
accumulateArgs:
- '$name'
accumulateArgs: ['$name']
merge:
$code: |-
function(state1, state2) {
Expand Down
12 changes: 12 additions & 0 deletions generator/src/OperatorClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ public function createClass(GeneratorDefinition $definition, OperatorDefinition
$constructorParam->setDefaultValue($argument->default);
}

if ($type->dollarPrefixedString) {
$namespace->addUseFunction('is_string');
$namespace->addUseFunction('str_starts_with');
$namespace->addUse(InvalidArgumentException::class);
$constructor->addBody(<<<PHP
if (is_string(\${$argument->propertyName}) && ! str_starts_with(\${$argument->propertyName}, '$')) {
throw new InvalidArgumentException('Argument \${$argument->propertyName} can be an expression, field paths and variable names must be prefixed by "$" or "$$".');
}

PHP);
}

// List type must be validated with array_is_list()
if ($type->list) {
$namespace->addUseFunction('is_array');
Expand Down
18 changes: 17 additions & 1 deletion generator/src/OperatorGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use function ltrim;
use function sort;
use function sprintf;
use function str_starts_with;
use function ucfirst;
use function usort;

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

$dollarPrefixedString = false;

foreach ($arg->type as $type) {
if (str_starts_with($type, 'resolvesTo')) {
$dollarPrefixedString = true;
}

$type = $this->getType($type);
$nativeTypes = array_merge($nativeTypes, $type->acceptedTypes);

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

// If the argument accepts an expression, a $-prefixed string is accepted (field path or variable)
// Checked only if the argument does not already accept a string
if (in_array('string', $nativeTypes, true)) {
$dollarPrefixedString = false;
} elseif ($dollarPrefixedString) {
$nativeTypes[] = 'string';
}

$docTypes = $nativeTypes = array_unique($nativeTypes);
$use = [];

Expand Down Expand Up @@ -131,6 +146,7 @@ final protected function getAcceptedTypes(ArgumentDefinition $arg): stdClass
'list' => $listCheck,
'query' => $isQuery,
'javascript' => $isJavascript,
'dollarPrefixedString' => $dollarPrefixedString,
];
}

Expand Down
25 changes: 17 additions & 8 deletions src/Builder/Accumulator/AccumulatorAccumulator.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 12 additions & 4 deletions src/Builder/Accumulator/AvgAccumulator.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 12 additions & 4 deletions src/Builder/Accumulator/BottomNAccumulator.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 20 additions & 8 deletions src/Builder/Accumulator/CovariancePopAccumulator.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 20 additions & 8 deletions src/Builder/Accumulator/CovarianceSampAccumulator.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading