Skip to content

PHPLIB-1624 Generate typed value classes for nested operator arguments#1886

Draft
GromNaN wants to merge 7 commits into
mongodb:v2.xfrom
GromNaN:generator-nested-types
Draft

PHPLIB-1624 Generate typed value classes for nested operator arguments#1886
GromNaN wants to merge 7 commits into
mongodb:v2.xfrom
GromNaN:generator-nested-types

Conversation

@GromNaN
Copy link
Copy Markdown
Member

@GromNaN GromNaN commented Apr 29, 2026

Depends on mongodb/mql-specifications#33

Generates dedicated final type classes in MongoDB\Builder\Type\ for operator arguments with nested sub-fields (YAML arguments: key), replacing ad-hoc array|stdClass parameters with properly-typed value objects (e.g. RankFusionInput, RankFusionCombination, ScoreFusionInput, ScoreFusionCombination).

  • Each type class implements TypeInterface (new marker interface) and exposes a PROPERTIES constant
  • Added TypeEncoder registered in BuilderEncoder to serialize these objects, skipping Optional::Undefined properties
  • Operator constructors auto-convert plain arrays to the type class via named-argument spread (new RankFusionInput(...$input)) for ergonomic API usage
  • Refactored buildPropertyAndParam() in OperatorClassGenerator to handle all argument patterns (non-variadic, variadic:object map, variadic:array list), eliminating the duplicated logic between createClass() and createArgumentTypeClass()

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 29, 2026

Codecov Report

❌ Patch coverage is 88.57143% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.46%. Comparing base (4ae7e29) to head (86144d3).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
src/Builder/Encoder/TypeEncoder.php 75.00% 4 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               v2.x    #1886      +/-   ##
============================================
- Coverage     87.74%   87.46%   -0.28%     
- Complexity     3319     3350      +31     
============================================
  Files           451      457       +6     
  Lines          6632     6692      +60     
============================================
+ Hits           5819     5853      +34     
- Misses          813      839      +26     
Flag Coverage Δ
6.0-replica_set 86.31% <88.57%> (-0.27%) ⬇️
6.0-server 82.38% <88.57%> (-0.24%) ⬇️
6.0-sharded_cluster 86.10% <88.57%> (-0.27%) ⬇️
8.0-replica_set 87.32% <88.57%> (-0.28%) ⬇️
8.0-server 83.11% <88.57%> (-0.24%) ⬇️
8.0-sharded_cluster 87.16% <88.57%> (-0.28%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

GromNaN added 5 commits May 5, 2026 18:55
Generate dedicated final type classes in MongoDB\Builder\Type\ for operator
arguments with sub-fields (e.g., RankFusionInput, RankFusionCombination,
ScoreFusionInput, ScoreFusionCombination).

- Each type class implements TypeInterface and carries a PROPERTIES constant
- TypeEncoder (registered in BuilderEncoder) handles serialization, skipping
  Optional::Undefined properties
- Operator constructors auto-convert plain arrays to the type class via spread
  (e.g., new RankFusionInput(...$input)) for ergonomic usage
- buildPropertyAndParam() helper in OperatorClassGenerator now handles all
  argument patterns (non-variadic, variadic:object, variadic:array), eliminating
  duplication between createClass() and createArgumentTypeClass()
- Add TypeClassPrinter to allow single-line method docblocks when content
  fits on one line (fixes MultiLineDocComment for single-param constructors)
- Sort `null` last in union types to comply with NullTypeHintOnLastPosition
- Align @param annotations in type class constructors to fix
  SpacingAfterParamType/SpacingAfterParamName CS errors
- Regenerate all affected builder files
@GromNaN GromNaN force-pushed the generator-nested-types branch from f7eb903 to 9a10497 Compare May 5, 2026 19:06
Copy link
Copy Markdown

@github-advanced-security github-advanced-security AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Psalm found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.

Type classes for nested arguments are now generated in a Type sub-namespace
of their operator's namespace (e.g. MongoDB\Builder\Stage\Type instead of
the shared MongoDB\Builder\Type namespace).
@GromNaN GromNaN force-pushed the generator-nested-types branch from 43e0932 to 9032f52 Compare May 5, 2026 19:25
Each generated class now declares a @psalm-type {ClassName}Shape annotation
that describes all valid array/stdClass representations of the operator:

- Encode::Object → array{prop: type, prop?: type}|object{...}&stdClass|ClassName
- Encode::Single (variadic:array) → non-empty-list<type>|ClassName
- Encode::Single (variadic:object) → non-empty-array<string, type>|stdClass|ClassName
- Encode::Single (scalar) → type|ClassName
- Encode::Array → list{type, type}|ClassName

Field names that are Psalm parser keywords (e.g. "as") are single-quoted.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants