Skip to content

feat(cluster): add a ProtocolVersion enum for withProtocolVersion() - #153

Open
CodeLieutenant wants to merge 1 commit into
trunkfrom
feat/narrow-stub-types-and-ide-stubs
Open

feat(cluster): add a ProtocolVersion enum for withProtocolVersion()#153
CodeLieutenant wants to merge 1 commit into
trunkfrom
feat/narrow-stub-types-and-ide-stubs

Conversation

@CodeLieutenant

Copy link
Copy Markdown
Member

What this does

withProtocolVersion() took a bare int, so a caller had to know that the native protocol version is the byte the protocol itself defines, and a typo produced a runtime error instead of a type error.

Adds Cassandra\ProtocolVersion, an int-backed enum whose cases carry that byte, so V1 through V5 match CASS_PROTOCOL_VERSION_V1 through _V5.

$cluster = Cassandra::cluster()
    ->withProtocolVersion(Cassandra\ProtocolVersion::V4)
    ->build();

Why the int overload stays

The enum names only the five native protocol versions, but the driver also understands versions it does not name, such as the DSE ones. So the signature is ProtocolVersion|int and the int path keeps its own range check.

Input Result
ProtocolVersion::V4 4
3 3, reported back as ProtocolVersion::V3
66 66, reported back as the plain int
0 InvalidArgumentException

Behaviour change worth noting

Cluster\Builder's protocolVersion property now reports the matching enum case when the value maps to one, and falls back to the plain int when it does not. Code that read that property as an int and used a named version will now see an enum. Reading ->value covers both.

Descriptor generator

The generator did not understand enums: it emitted a create_object handler and an object-allocating registration for every class it saw, which is wrong for an internal enum whose cases the engine builds and owns. It now recognises the enum kind and registers through zend_register_internal_enum() with no instance handlers.

This is the part most worth a careful read, since it changes code generation for every stub, not just this one.

Verification

  • Build clean, clang-tidy 0 findings on Cluster/Builder.c.
  • Unit suite 868 passed, 0 failures.
  • Each row of the table above checked against a built extension.

Note on the branch

This branch previously carried #152, which is merged. Its three commits were dropped during a rebase onto trunk as already upstream, so this PR is the single new commit. The branch name is left over from that work and does not describe this change.

`withProtocolVersion()` took a bare int, so a caller had to know that the
native protocol version is the byte the protocol itself defines, and a typo
produced a runtime error rather than a type error.

Add `Cassandra\ProtocolVersion`, an int-backed enum whose cases carry that
byte, so V1 through V5 match CASS_PROTOCOL_VERSION_V1 through _V5:

    ->withProtocolVersion(Cassandra\ProtocolVersion::V4)

The method now accepts the enum or an int. The int path stays because the
enum names only the five native versions, and the driver also understands
versions it does not name, such as the DSE ones. It keeps its own range check.

`Cluster\Builder`'s `protocolVersion` property now reports the matching enum
case when the value maps to one, and falls back to the plain int when it does
not. Code that read that property as an int and mapped a named version will
see an enum instead.

The descriptor generator did not understand enums: it emitted a create_object
handler and an object-allocating registration for every class it saw. Teach it
the enum kind, so an internal enum registers through
zend_register_internal_enum() with no instance handlers, since its cases are
immutable objects the engine builds.
@mergify

mergify Bot commented Aug 10, 2026

Copy link
Copy Markdown

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

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.

1 participant