Skip to content

Commit d526ab8

Browse files
authored
Fix Javascript type & other PHP types (mongodb#4)
1 parent 9d46af7 commit d526ab8

Some content is hidden

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

71 files changed

+314
-125
lines changed

generator/config/expression/bsonSize.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ type:
55
- resolvesToInt
66
encode: single
77
description: |
8-
Returns the size in bytes of a given document (i.e. bsontype Object) when encoded as BSON.
8+
Returns the size in bytes of a given document (i.e. BSON type Object) when encoded as BSON.
99
arguments:
1010
-
1111
name: object

generator/config/expression/function.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ arguments:
1111
-
1212
name: body
1313
type:
14-
- string
14+
- javascript
1515
description: |
16-
The function definition. You can specify the function definition as either BSON type Code or String.
16+
The function definition. You can specify the function definition as either BSON\JavaScript or string.
17+
function(arg1, arg2, ...) { ... }
1718
-
1819
name: args
1920
type:
@@ -24,3 +25,4 @@ arguments:
2425
name: lang
2526
type:
2627
- string
28+
default: js

generator/config/query/natural.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# $schema: ../schema.json
22
name: $natural
3-
link: 'https://www.mongodb.com/docs/v7.0/reference/operator/meta/natural/'
3+
link: 'https://www.mongodb.com/docs/manual/reference/operator/meta/natural/'
44
type:
5-
- expression
5+
- projection # @todo: used in sort
66
encode: object
77
description: |
88
A special hint that can be provided via the sort() or hint() methods that can be used to force either a forward or reverse collection scan.

generator/config/query/rand.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name: $rand
33
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/rand/'
44
type:
5-
- expression
5+
- resolvesToDouble
66
encode: object
77
description: |
88
Generates a random float between 0 and 1.

generator/config/query/where.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ arguments:
1010
-
1111
name: function
1212
type:
13-
- string
13+
- javascript

generator/config/schema.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"name": {
1010
"$comment": "The name of the operator. Must start with a $",
1111
"type": "string",
12-
"pattern": "^\\$[a-z][a-zA-Z]+$"
12+
"pattern": "^\\$[a-z0-9][a-zA-Z0-9]*$"
1313
},
1414
"link": {
1515
"$comment": "The link to the operator's documentation on MongoDB's website.",
@@ -28,6 +28,7 @@
2828
"projection",
2929
"stage",
3030
"query",
31+
"fieldQuery",
3132
"filter",
3233
"window",
3334
"geometry",
@@ -92,7 +93,7 @@
9293
"properties": {
9394
"name": {
9495
"type": "string",
95-
"pattern": "^([a-z][a-zA-Z0-9]*|N)$"
96+
"pattern": "^(_?[a-z][a-zA-Z0-9]*|N)$"
9697
},
9798
"type": {
9899
"type": "array",
@@ -105,6 +106,8 @@
105106
"window",
106107
"expression",
107108
"geometry",
109+
"projection",
110+
"fieldPath",
108111
"any",
109112
"resolvesToNumber", "numberFieldPath", "number",
110113
"resolvesToDouble", "doubleFieldPath", "double",
@@ -120,7 +123,7 @@
120123
"resolvesToJavascript", "javascriptFieldPath", "javascript",
121124
"resolvesToInt", "intFieldPath", "int",
122125
"resolvesToTimestamp", "timestampFieldPath", "timestamp",
123-
"resolvesTolong", "longFieldPath", "long",
126+
"resolvesToLong", "longFieldPath", "long",
124127
"resolvesToDecimal", "decimalFieldPath", "decimal"
125128
]
126129
}
@@ -153,6 +156,10 @@
153156
"$comment": "The minimum number of arguments for a variadic parameter.",
154157
"type": "integer",
155158
"minimum": 0
159+
},
160+
"default": {
161+
"$comment": "The default value for the argument.",
162+
"type": ["string", "number", "boolean"]
156163
}
157164
},
158165
"required": [

generator/config/stage/changeStream.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ arguments:
1717
-
1818
name: fullDocument
1919
type:
20-
- FullDocument
20+
- string # FullDocument
2121
optional: true
2222
description: |
2323
Specifies whether change notifications include a copy of the full document when modified by update operations.
2424
-
2525
name: fullDocumentBeforeChange
2626
type:
27-
- FullDocumentBeforeChange
27+
- string # FullDocumentBeforeChange
2828
optional: true
2929
description: |
3030
Valid values are "off", "whenAvailable", or "required". If set to "off", the "fullDocumentBeforeChange" field of the output document is always omitted. If set to "whenAvailable", the "fullDocumentBeforeChange" field will be populated with the pre-image of the document modified by the current change event if such a pre-image is available, and will be omitted otherwise. If set to "required", then the "fullDocumentBeforeChange" field is always populated and an exception is thrown if the pre-image is not available.

generator/config/stage/lookup.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ arguments:
4444
optional: true
4545
description: |
4646
Specifies the pipeline to run on the joined collection. The pipeline determines the resulting documents from the joined collection. To return all documents, specify an empty pipeline [].
47-
The pipeline cannot include the $out stage or the $mergestage. Starting in v6.0, the pipeline can contain the Atlas Search $search stage as the first stage inside the pipeline.
47+
The pipeline cannot include the $out stage or the $merge stage. Starting in v6.0, the pipeline can contain the Atlas Search $search stage as the first stage inside the pipeline.
4848
The pipeline cannot directly access the joined document fields. Instead, define variables for the joined document fields using the let option and then reference the variables in the pipeline stages.
4949
-
5050
name: as

generator/config/stage/merge.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ arguments:
4040
-
4141
name: whenNotMatched
4242
type:
43-
- WhenNotMatched
43+
- string # WhenNotMatched
4444
optional: true
4545
description: |
4646
The behavior of $merge if a result document does not match an existing document in the out collection.

generator/config/stage/setWindowFields.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ arguments:
1717
-
1818
name: sortBy
1919
type:
20-
- SortSpec
20+
- object # SortSpec
2121
description: |
2222
Specifies the field(s) to sort the documents by in the partition. Uses the same syntax as the $sort stage. Default is no sorting.
2323
-
@@ -30,7 +30,7 @@ arguments:
3030
-
3131
name: window
3232
type:
33-
- Window
33+
- window
3434
optional: true
3535
description: |
3636
Specifies the window boundaries and parameters. Window boundaries are inclusive. Default is an unbounded window, which includes all documents in the partition.

generator/config/stage/sort.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ arguments:
1010
-
1111
name: sort
1212
type:
13-
- SortSpec
13+
- object # SortSpec

generator/src/Definition/ArgumentDefinition.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ final class ArgumentDefinition
1818

1919
public function __construct(
2020
public string $name,
21-
/** @psalm-assert list<string> $type */
21+
/** @var list<string> */
2222
public array $type,
2323
public string|null $description = null,
2424
public bool $optional = false,
2525
string|null $variadic = null,
2626
int|null $variadicMin = null,
27+
public mixed $default = null,
2728
) {
29+
assert($this->optional === false || $this->default === null, 'Optional arguments cannot have a default value');
2830
if (is_array($type)) {
2931
assert(array_is_list($type), 'Type must be a list or a single string');
3032
foreach ($type as $t) {

generator/src/Definition/OperatorDefinition.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function __construct(
2323
public string $name,
2424
public string $link,
2525
string $encode,
26+
/** @var list<string> */
2627
public array $type,
2728
public string|null $description = null,
2829
array $arguments = [],

generator/src/ExpressionFactoryGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
final class ExpressionFactoryGenerator extends AbstractGenerator
1515
{
16-
/** @param array<class-string, ExpressionDefinition> $definitions */
16+
/** @param array<string, ExpressionDefinition> $expressions */
1717
public function generate(array $expressions): void
1818
{
1919
$this->writeFile($this->createFactoryClass($expressions));

generator/src/OperatorClassGenerator.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function createClass(GeneratorDefinition $definition, OperatorDefinition
8686

8787
if ($argument->variadic === VariadicType::Array) {
8888
$property->setType('array');
89-
$property->addComment('@var list<' . $type->doc . '> ...$' . $argument->name . rtrim(' ' . $argument->description));
89+
$property->addComment('@var list<' . $type->doc . '> $' . $argument->name . rtrim(' ' . $argument->description));
9090
// Warn that named arguments are not supported
9191
// @see https://psalm.dev/docs/running_psalm/issues/NamedArgumentNotAllowed/
9292
$constuctor->addComment('@no-named-arguments');
@@ -100,7 +100,7 @@ public function createClass(GeneratorDefinition $definition, OperatorDefinition
100100
} elseif ($argument->variadic === VariadicType::Object) {
101101
$namespace->addUse(stdClass::class);
102102
$property->setType(stdClass::class);
103-
$property->addComment('@var stdClass<' . $type->doc . '> ...$' . $argument->name . rtrim(' ' . $argument->description));
103+
$property->addComment('@var stdClass<' . $type->doc . '> $' . $argument->name . rtrim(' ' . $argument->description));
104104
$namespace->addUseFunction('is_string');
105105
$namespace->addUse(InvalidArgumentException::class);
106106
$constuctor->addBody(<<<PHP
@@ -121,6 +121,8 @@ public function createClass(GeneratorDefinition $definition, OperatorDefinition
121121
if ($argument->optional) {
122122
// We use a special Optional::Undefined type to differentiate between null and undefined
123123
$constuctorParam->setDefaultValue(new Literal('Optional::Undefined'));
124+
} elseif ($argument->default !== null) {
125+
$constuctorParam->setDefaultValue($argument->default);
124126
}
125127

126128
// List type must be validated with array_is_list()
@@ -162,6 +164,8 @@ public function createClass(GeneratorDefinition $definition, OperatorDefinition
162164

163165
/**
164166
* Operator classes interfaces are defined by their return type as a MongoDB expression.
167+
*
168+
* @return list<class-string>
165169
*/
166170
private function getInterfaces(OperatorDefinition $definition): array
167171
{

generator/src/OperatorGenerator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ final public function __construct(
4545

4646
abstract public function generate(GeneratorDefinition $definition): void;
4747

48+
/** @return list<array<string, mixed>> */
4849
final protected function getOperators(GeneratorDefinition $definition): array
4950
{
5051
// Remove unsupported operators

phpcs.xml.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
<!-- Import rules from doctrine/coding-standard -->
2525
<!-- ****************************************** -->
2626
<rule ref="Doctrine">
27+
<exclude name="SlevomatCodingStandard.Classes.SuperfluousInterfaceNaming.SuperfluousSuffix"/>
28+
<exclude name="SlevomatCodingStandard.Classes.SuperfluousAbstractClassNaming.SuperfluousPrefix"/>
29+
2730
<!-- **************************************** -->
2831
<!-- Exclude sniffs that force unwanted style -->
2932
<!-- **************************************** -->

psalm-baseline.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<files psalm-version="5.15.0@5c774aca4746caf3d239d9c8cadb9f882ca29352">
3+
</files>

psalm.xml.dist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
>
1313
<projectFiles>
1414
<directory name="src" />
15-
<directory name="examples" />
1615
<ignoreFiles>
1716
<directory name="vendor" />
1817
</ignoreFiles>

src/Builder/Accumulator.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ enum Accumulator
2121
{
2222
use Accumulator\FactoryTrait;
2323

24+
/**
25+
* @param Document|Serializable|WindowInterface|array<string, mixed>|stdClass $operator Window operator to use in the $setWindowFields stage.
26+
* @param Optional|array{string|int,string|int} $documents A window where the lower and upper boundaries are specified relative to the position of the current document read from the collection.
27+
* @param Optional|array{string|numeric,string|numeric} $range Arguments passed to the init function.
28+
* @param Optional|non-empty-string $unit Specifies the units for time range window boundaries. If omitted, default numeric range window boundaries are used.
29+
*/
2430
public static function outputWindow(
2531
Document|Serializable|WindowInterface|stdClass|array $operator,
2632
Optional|array $documents = Optional::Undefined,

src/Builder/Accumulator/MergeObjectsAccumulator.php

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/BuilderEncoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ private function encodeOutputWindow(OutputWindow $outputWindow): stdClass
244244
// Transform the result into an stdClass if a document is provided
245245
if (! $outputWindow->operator instanceof WindowInterface && (is_array($result) || is_object($result))) {
246246
if (! is_first_key_operator($result)) {
247-
throw new LogicException(sprintf('Expected OutputWindow::$operator to be an operator. Got "%s"', array_key_first($result)));
247+
throw new LogicException(sprintf('Expected OutputWindow::$operator to be an operator. Got "%s"', array_key_first((array) $result)));
248248
}
249249

250250
$result = (object) $result;

src/Builder/Expression/AddOperator.php

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/Expression/AllElementsTrueOperator.php

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/Expression/AndOperator.php

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/Expression/AvgOperator.php

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/Expression/BitAndOperator.php

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/Expression/BitOrOperator.php

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/Expression/BitXorOperator.php

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/Expression/BsonSizeOperator.php

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/Expression/ConcatArraysOperator.php

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/Expression/ConcatOperator.php

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)