Skip to content

Commit 9f5a3fb

Browse files
committed
Validate variable name starts with a $
1 parent a6bde35 commit 9f5a3fb

File tree

174 files changed

+1762
-693
lines changed

Some content is hidden

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

174 files changed

+1762
-693
lines changed

generator/config/expressions.php

-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use MongoDB\Model\BSONArray;
1414
use stdClass;
1515

16-
use function in_array;
1716
use function ucfirst;
1817

1918
$bsonTypes = [
@@ -46,11 +45,6 @@
4645
$expressions = [];
4746
$resolvesToInterfaces = [];
4847
foreach ($bsonTypes as $name => $acceptedTypes) {
49-
// an expression can be a string with a $-prefixed field name
50-
if (! in_array('string', $acceptedTypes)) {
51-
$acceptedTypes[] = 'string';
52-
}
53-
5448
$expressions[$name] = ['acceptedTypes' => $acceptedTypes];
5549

5650
$resolvesTo = 'resolvesTo' . ucfirst($name);

generator/src/OperatorClassGenerator.php

+12
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,18 @@ public function createClass(GeneratorDefinition $definition, OperatorDefinition
169169
170170
PHP);
171171
}
172+
173+
if ($type->dollarPrefixedString) {
174+
$namespace->addUseFunction('is_string');
175+
$namespace->addUseFunction('str_starts_with');
176+
$namespace->addUse(InvalidArgumentException::class);
177+
$constructor->addBody(<<<PHP
178+
if (is_string(\${$argument->propertyName}) && ! str_starts_with(\${$argument->propertyName}, '$')) {
179+
throw new InvalidArgumentException('Argument \${$argument->propertyName} can be an expression, field paths and variable names must be prefixed by "$" or "$$".');
180+
}
181+
182+
PHP);
183+
}
172184
}
173185

174186
// Set property from constructor argument

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

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

src/Builder/Accumulator/AvgAccumulator.php

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

src/Builder/Accumulator/BottomNAccumulator.php

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

src/Builder/Accumulator/CovariancePopAccumulator.php

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

src/Builder/Accumulator/CovarianceSampAccumulator.php

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

src/Builder/Accumulator/DerivativeAccumulator.php

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

src/Builder/Accumulator/ExpMovingAvgAccumulator.php

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

src/Builder/Accumulator/FactoryTrait.php

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

src/Builder/Accumulator/FirstNAccumulator.php

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

src/Builder/Accumulator/IntegralAccumulator.php

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

0 commit comments

Comments
 (0)