Skip to content

Commit 3c9b7cd

Browse files
committed
Replace getOperator with Operator::NAME constant
Fix static analysis Rename noName to wrapObject
1 parent 9fc0d42 commit 3c9b7cd

File tree

10 files changed

+58
-23
lines changed

10 files changed

+58
-23
lines changed

generator/config/expression/case.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ name: $case
33
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/switch/'
44
type:
55
- switchBranch
6-
encode: flat_object
6+
encode: object
7+
wrapObject: false
78
description: |
89
Represents a single case in a $switch expression
910
arguments:

generator/config/schema.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,18 @@
6060
"enum": [
6161
"array",
6262
"object",
63-
"flat_object",
6463
"single"
6564
]
6665
},
6766
"description": {
6867
"$comment": "The description of the argument from MongoDB's documentation.",
6968
"type": "string"
7069
},
70+
"wrapObject": {
71+
"$comment": "Wrap the properties in an object with the operator name",
72+
"type": "boolean",
73+
"default": true
74+
},
7175
"arguments": {
7276
"$comment": "An optional list of arguments for the operator.",
7377
"type": "array",

generator/src/Definition/OperatorDefinition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ public function __construct(
3232
/** @var list<string> */
3333
public array $type,
3434
public string|null $description = null,
35+
public bool $wrapObject = true,
3536
array $arguments = [],
3637
array $tests = [],
3738
) {
3839
$this->encode = match ($encode) {
3940
'single' => Encode::Single,
4041
'array' => Encode::Array,
4142
'object' => Encode::Object,
42-
'flat_object' => Encode::FlatObject,
4343
default => throw new UnexpectedValueException(sprintf('Unexpected "encode" value for operator "%s". Got "%s"', $name, $encode)),
4444
};
4545

generator/src/OperatorClassGenerator.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
use function interface_exists;
2323
use function rtrim;
2424
use function sprintf;
25-
use function var_export;
2625

2726
/**
2827
* Generates a value object class for stages and operators.
@@ -62,6 +61,7 @@ public function createClass(GeneratorDefinition $definition, OperatorDefinition
6261
$class->addComment('@internal');
6362
$namespace->addUse(Encode::class);
6463
$class->addConstant('ENCODE', new Literal('Encode::' . $operator->encode->name));
64+
$class->addConstant('NAME', $operator->wrapObject ? $operator->name : null);
6565

6666
$encodeNames = [];
6767
$constructor = $class->addMethod('__construct');
@@ -179,10 +179,6 @@ public function createClass(GeneratorDefinition $definition, OperatorDefinition
179179
$class->addConstant('PROPERTIES', $encodeNames);
180180
}
181181

182-
$class->addMethod('getOperator')
183-
->setReturnType('string')
184-
->setBody('return ' . var_export($operator->name, true) . ';');
185-
186182
return $namespace;
187183
}
188184

psalm-baseline.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@
7575
<code><![CDATA[$val]]></code>
7676
<code><![CDATA[$val]]></code>
7777
</MixedAssignment>
78+
<MixedInferredReturnType>
79+
<code><![CDATA[stdClass]]></code>
80+
</MixedInferredReturnType>
81+
<MixedReturnStatement>
82+
<code><![CDATA[$result]]></code>
83+
</MixedReturnStatement>
7884
<PossibleRawObjectIteration>
7985
<code><![CDATA[$val]]></code>
8086
</PossibleRawObjectIteration>
@@ -172,7 +178,26 @@
172178
<code><![CDATA[stdClass]]></code>
173179
</TooManyTemplateParams>
174180
</file>
181+
<file src="src/Builder/Type/CombinedFieldQuery.php">
182+
<MixedArgument>
183+
<code><![CDATA[$operator]]></code>
184+
<code><![CDATA[$operator]]></code>
185+
</MixedArgument>
186+
<MixedArrayOffset>
187+
<code><![CDATA[$seenOperators[$operator]]]></code>
188+
</MixedArrayOffset>
189+
<MixedAssignment>
190+
<code><![CDATA[$operator]]></code>
191+
</MixedAssignment>
192+
</file>
175193
<file src="src/Builder/Type/QueryObject.php">
194+
<MixedArgument>
195+
<code><![CDATA[$query::NAME]]></code>
196+
</MixedArgument>
197+
<MixedArrayOffset>
198+
<code><![CDATA[$seenQueryOperators[$query::NAME]]]></code>
199+
<code><![CDATA[$seenQueryOperators[$query::NAME]]]></code>
200+
</MixedArrayOffset>
176201
<MixedAssignment>
177202
<code><![CDATA[$queries[$fieldPath]]]></code>
178203
<code><![CDATA[$query]]></code>
@@ -183,6 +208,11 @@
183208
is_array($queriesOrArrayOfQueries[0]) &&
184209
count($queriesOrArrayOfQueries[0]) > 0]]></code>
185210
</RedundantConditionGivenDocblockType>
211+
<UndefinedConstant>
212+
<code><![CDATA[$query::NAME]]></code>
213+
<code><![CDATA[$query::NAME]]></code>
214+
<code><![CDATA[$query::NAME]]></code>
215+
</UndefinedConstant>
186216
</file>
187217
<file src="src/ChangeStream.php">
188218
<DeprecatedConstant>

src/Builder/Encoder/OperatorEncoder.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function encode(mixed $value): stdClass
3737
return match ($value::ENCODE) {
3838
Encode::Single => $this->encodeAsSingle($value),
3939
Encode::Array => $this->encodeAsArray($value),
40-
Encode::Object, Encode::FlatObject => $this->encodeAsObject($value),
40+
Encode::Object => $this->encodeAsObject($value),
4141
default => throw new LogicException(sprintf('Class "%s" does not have a valid ENCODE constant.', $value::class)),
4242
};
4343
}
@@ -89,9 +89,7 @@ private function encodeAsObject(OperatorInterface $value): stdClass
8989
}
9090
}
9191

92-
return $value::ENCODE === Encode::FlatObject
93-
? $result
94-
: $this->wrap($value, $result);
92+
return $this->wrap($value, $result);
9593
}
9694

9795
/**
@@ -108,10 +106,19 @@ private function encodeAsSingle(OperatorInterface $value): stdClass
108106
throw new LogicException(sprintf('Class "%s" does not have a single property.', $value::class));
109107
}
110108

109+
/**
110+
* Wrap the result in an object if the operator has a name.
111+
* The operator name is NULL, and the result is returned as is
112+
* when wrapObject is false in the YAML configuration of the operator.
113+
*/
111114
private function wrap(OperatorInterface $value, mixed $result): stdClass
112115
{
116+
if ($value::NAME === null) {
117+
return $result;
118+
}
119+
113120
$object = new stdClass();
114-
$object->{$value->getOperator()} = $result;
121+
$object->{$value::NAME} = $result;
115122

116123
return $object;
117124
}

src/Builder/Type/CombinedFieldQuery.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,15 @@ static function (array $fieldQueries, QueryInterface|FieldQueryInterface|Type|st
5959
);
6060

6161
// Validate FieldQuery types and non-duplicate operators
62+
/** @var array<string, true> $seenOperators */
6263
$seenOperators = [];
6364
foreach ($this->fieldQueries as $fieldQuery) {
6465
if ($fieldQuery instanceof stdClass) {
6566
$fieldQuery = get_object_vars($fieldQuery);
6667
}
6768

6869
if ($fieldQuery instanceof FieldQueryInterface && $fieldQuery instanceof OperatorInterface) {
69-
$operator = $fieldQuery->getOperator();
70+
$operator = $fieldQuery::NAME;
7071
} elseif (is_array($fieldQuery)) {
7172
if (count($fieldQuery) !== 1) {
7273
throw new InvalidArgumentException(sprintf('Operator must contain exactly one key, %d given', count($fieldQuery)));

src/Builder/Type/Encode.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@ enum Encode
2424
*/
2525
case Object;
2626

27-
/**
28-
* Same as Object, but only parameters are returned. The operator name will not be used.
29-
*/
30-
case FlatObject;
31-
3227
/**
3328
* Get the single parameter value
3429
*/

src/Builder/Type/OperatorInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ interface OperatorInterface
1515
/** @var array<string, string|null> */
1616
public const PROPERTIES = [];
1717

18-
public function getOperator(): string;
18+
/** @var string|null */
19+
public const NAME = null;
1920
}

src/Builder/Type/QueryObject.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ private function __construct(array $queriesOrArrayOfQueries)
5656
foreach ($queriesOrArrayOfQueries as $fieldPath => $query) {
5757
if ($query instanceof QueryInterface) {
5858
if ($query instanceof OperatorInterface) {
59-
if (isset($seenQueryOperators[$query->getOperator()])) {
60-
throw new InvalidArgumentException(sprintf('Query operator "%s" cannot be used multiple times in the same query.', $query->getOperator()));
59+
if (isset($seenQueryOperators[$query::NAME])) {
60+
throw new InvalidArgumentException(sprintf('Query operator "%s" cannot be used multiple times in the same query.', $query::NAME));
6161
}
6262

63-
$seenQueryOperators[$query->getOperator()] = true;
63+
$seenQueryOperators[$query::NAME] = true;
6464
}
6565

6666
$queries[] = $query;

0 commit comments

Comments
 (0)