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

Replace OperatorInterface::getOperator() with OperatorInterface::NAME constant #1541

Merged
merged 3 commits into from
Dec 3, 2024
Merged
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
3 changes: 2 additions & 1 deletion generator/config/expression/case.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ name: $case
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/switch/'
type:
- switchBranch
encode: flat_object
encode: object
wrapObject: false
description: |
Represents a single case in a $switch expression
arguments:
Expand Down
6 changes: 5 additions & 1 deletion generator/config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,18 @@
"enum": [
"array",
"object",
"flat_object",
"single"
]
},
"description": {
"$comment": "The description of the argument from MongoDB's documentation.",
"type": "string"
},
"wrapObject": {
"$comment": "Wrap the properties in an object with the operator name",
"type": "boolean",
"default": true
},
"arguments": {
"$comment": "An optional list of arguments for the operator.",
"type": "array",
Expand Down
6 changes: 5 additions & 1 deletion generator/src/Definition/OperatorDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,21 @@ public function __construct(
/** @var list<string> */
public array $type,
public string|null $description = null,
public bool $wrapObject = true,
array $arguments = [],
array $tests = [],
) {
$this->encode = match ($encode) {
'single' => Encode::Single,
'array' => Encode::Array,
'object' => Encode::Object,
'flat_object' => Encode::FlatObject,
default => throw new UnexpectedValueException(sprintf('Unexpected "encode" value for operator "%s". Got "%s"', $name, $encode)),
};

if (! $wrapObject && $this->encode !== Encode::Object) {
throw new UnexpectedValueException(sprintf('Operator "%s" cannot have wrapObject set to false when encode is not "object"', $name));
}

// Convert arguments to ArgumentDefinition objects
// Optional arguments must be after required arguments
$requiredArgs = $optionalArgs = [];
Expand Down
6 changes: 1 addition & 5 deletions generator/src/OperatorClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use function interface_exists;
use function rtrim;
use function sprintf;
use function var_export;

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

$encodeNames = [];
$constructor = $class->addMethod('__construct');
Expand Down Expand Up @@ -179,10 +179,6 @@ public function createClass(GeneratorDefinition $definition, OperatorDefinition
$class->addConstant('PROPERTIES', $encodeNames);
}

$class->addMethod('getOperator')
->setReturnType('string')
->setBody('return ' . var_export($operator->name, true) . ';');

return $namespace;
}

Expand Down
24 changes: 24 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,26 @@
<code><![CDATA[stdClass]]></code>
</TooManyTemplateParams>
</file>
<file src="src/Builder/Type/CombinedFieldQuery.php">
<MixedArgument>
<code><![CDATA[$operator]]></code>
<code><![CDATA[$operator]]></code>
</MixedArgument>
<MixedArrayOffset>
<code><![CDATA[$seenOperators[$operator]]]></code>
</MixedArrayOffset>
<MixedAssignment>
<code><![CDATA[$operator]]></code>
</MixedAssignment>
</file>
<file src="src/Builder/Type/QueryObject.php">
<MixedArgument>
<code><![CDATA[$query::NAME]]></code>
</MixedArgument>
<MixedArrayOffset>
<code><![CDATA[$seenQueryOperators[$query::NAME]]]></code>
<code><![CDATA[$seenQueryOperators[$query::NAME]]]></code>
</MixedArrayOffset>
<MixedAssignment>
<code><![CDATA[$queries[$fieldPath]]]></code>
<code><![CDATA[$query]]></code>
Expand All @@ -183,6 +202,11 @@
is_array($queriesOrArrayOfQueries[0]) &&
count($queriesOrArrayOfQueries[0]) > 0]]></code>
</RedundantConditionGivenDocblockType>
<UndefinedConstant>
<code><![CDATA[$query::NAME]]></code>
<code><![CDATA[$query::NAME]]></code>
<code><![CDATA[$query::NAME]]></code>
</UndefinedConstant>
</file>
<file src="src/ChangeStream.php">
<DeprecatedConstant>
Expand Down
6 changes: 1 addition & 5 deletions src/Builder/Accumulator/AccumulatorAccumulator.php

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

6 changes: 1 addition & 5 deletions src/Builder/Accumulator/AddToSetAccumulator.php

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

6 changes: 1 addition & 5 deletions src/Builder/Accumulator/AvgAccumulator.php

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

6 changes: 1 addition & 5 deletions src/Builder/Accumulator/BottomAccumulator.php

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

6 changes: 1 addition & 5 deletions src/Builder/Accumulator/BottomNAccumulator.php

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

6 changes: 1 addition & 5 deletions src/Builder/Accumulator/CountAccumulator.php

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

6 changes: 1 addition & 5 deletions src/Builder/Accumulator/CovariancePopAccumulator.php

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

6 changes: 1 addition & 5 deletions src/Builder/Accumulator/CovarianceSampAccumulator.php

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

6 changes: 1 addition & 5 deletions src/Builder/Accumulator/DenseRankAccumulator.php

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

6 changes: 1 addition & 5 deletions src/Builder/Accumulator/DerivativeAccumulator.php

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

6 changes: 1 addition & 5 deletions src/Builder/Accumulator/DocumentNumberAccumulator.php

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

6 changes: 1 addition & 5 deletions src/Builder/Accumulator/ExpMovingAvgAccumulator.php

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

6 changes: 1 addition & 5 deletions src/Builder/Accumulator/FirstAccumulator.php

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

6 changes: 1 addition & 5 deletions src/Builder/Accumulator/FirstNAccumulator.php

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

6 changes: 1 addition & 5 deletions src/Builder/Accumulator/IntegralAccumulator.php

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

6 changes: 1 addition & 5 deletions src/Builder/Accumulator/LastAccumulator.php

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

Loading