THRIFT-5990: Emit native return types on generated PHP struct methods#3472
Merged
Conversation
Client: php Phase G2 of THRIFT-5960 PHP modernization. The PHP code generator now emits native return types on the public struct surface: - getName(): string - read(TProtocol $input): int / read($input): int for binary_inline mode (which reads from a raw TTransport buffer, not a TProtocol) - write(TProtocol $output): int / write(string &$output): int for binary_inline mode - validateForRead(): void / validateForWrite(): void - jsonSerialize(): mixed (drops the now-unnecessary #[ReturnTypeWillChange] attribute since the project targets PHP 8.1+) To make the TProtocol parameter type on read/write compatible with LSP for generated subclasses extending TBase, the abstract signatures on `TBase::read` / `TBase::write` are tightened in lock-step: abstract public function read(TProtocol $input): int; abstract public function write(TProtocol $output): int; Three hand-written test fixtures extending TBase (TestSerializerStruct, ComplexStruct, NestedStruct) are updated to match the new TBase signatures. Generator fixtures under lib/php/test/Resources/packages/ are git-ignored and regenerated by `make stubs` in CI. Generated-by: Claude Opus 4.7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Phase G2 of the THRIFT-5960 PHP modernization. The PHP code generator now emits native return types on the public struct surface.
Generated method signatures:
getName(): string: stringread()(TProtocol $input): int($input): int(1)write()(TProtocol $output): int(string &$output): intvalidateForRead/Write(): void: voidjsonSerialize(): mixed(2)(1) Binary-inline mode reads from a raw
TTransportbuffer, not aTProtocol, so the param remains untyped to avoid pulling another import into every generated file in this PR. (Can be typed in a follow-up.)(2) The previously-emitted
#[\ReturnTypeWillChange]attribute is dropped since the project targets PHP 8.1+, which natively supports theJsonSerializable::jsonSerialize(): mixedinterface.Library change: tighten
TBaseabstracts in lock-step.To make
TProtocol $input/TProtocol $outputin the generated subclass methods compatible with PHP's LSP rules, the abstract methods onThrift\Base\TBaseare tightened:Any third-party generated PHP code from an older Thrift compiler that extends
TBasewill need to be regenerated against this compiler.Hand-written test fixtures updated:
lib/php/test/Unit/Lib/Fixture/TestSerializerStruct.phplib/php/test/Unit/Lib/Base/Fixture/ComplexStruct.phplib/php/test/Unit/Lib/Base/Fixture/NestedStruct.phpThese extend TBase and were updated to match the new abstract signatures.
Generator fixtures regeneration:
lib/php/test/Resources/packages/is git-ignored and is regenerated bymake stubsin CI before tests run. No fixture files appear in this diff.Validation (Docker
thrift/thrift-build:ubuntu-focal+thrift-php-dev:local):phpcs— 0 errors.phpstan(level 1) — 0 errors.phpunitUnit Suite — 635 tests, 0 failures.phpunitIntegration Suite — 108 tests, 0 failures.Scope:
This is phase G2. The remaining phase G3 will emit native types on generated property declarations and the
__construct(?array $vals = null)parameter, completing the generator side of THRIFT-5960.TBaseabstract signature change is a deliberate, scoped break that affects only direct extenders ofTBase. In practice that's only Thrift-generated code, which is regenerated.Generated-by: Claude Opus 4.7