THRIFT-6004: Emit native types on generated PHP service-level methods#3483
Merged
Conversation
Client: php Update t_php_generator to emit native PHP parameter and return types on all generated service-level code: * Interface (*If.php): public methods now declare typed params and return — `foo(?string $thing): ?string`. * Client (*Client.php): typed constructor `__construct(TProtocol $input, ?TProtocol $output = null)`; typed `$input`/`$output`/`$seqid` properties; typed RPC methods, typed `send_*` (synthetic t_function with g_type_void return), typed `recv_*` (already synthetic; now inherits the parent function_signature return-type machinery). * Processor (*Processor.php): typed `__construct(object $handler)`; typed `protected ?object $handler_ = null`; typed `process(TProtocol $input, TProtocol $output): bool` (changed the early "method not found" `return;` to `return false;` to satisfy the declared return type); typed `process_NAME(int $seqid, TProtocol $input, TProtocol $output): void`. * Rest (*Rest.php): typed `__construct(object $impl)`; typed `protected object $impl;`; typed method signatures `methodName(array $request): ?T`; emit `return` only when the underlying impl method returns non-void. Implementation -------------- * New helper `type_to_return(t_type*)` renders `: void` or `: ?T` for return-type clauses, sharing the existing `type_to_native()` mapping. * `function_signature()` extended to append `type_to_return()` for the return type; `argument_list()` extended to emit `?type_to_native()` on every parameter (previously only struct/container types were typed). * `send_*` emission now constructs a synthetic `t_function(g_type_void, ...)` so `function_signature()` produces the correct `: void` for the writer half of the RPC. Drive-by cleanup ---------------- `ThriftClassLoader::findFileInApcu()` (merged in THRIFT-6001) was flagged by PHPStan: the trailing `$file !== false ? $file : null` arm is unreachable because the if-branch reassigns `$file` to the findFile() result (`?string`). Rewrote the body to a clear two-step fetch-or-recompute and narrowed the return through `is_string()` to satisfy the analyser without relying on a phantom else-arm. Generated fixtures regenerate via `make stubs` in CI; nothing checked in changes. Part of umbrella THRIFT-5960 (PHP modernization). 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.
Update the PHP generator to emit native parameter and return types on every generated service-level method. Completes the typing pass on the generator side after THRIFT-5986 (declare strict_types), THRIFT-5990 (struct return types), and THRIFT-5991 (struct property types).
Generated signatures (before → after)
Implementation
type_to_return(t_type*)returning: voidor: ?T. Shares the existingtype_to_native()mapping introduced by THRIFT-5991.function_signature()now appendstype_to_return()for the return clause.argument_list()now emits?type_to_native()on every parameter — previously only struct/container parameters were typed.send_*constructs a synthetict_function(g_type_void, ...)sofunction_signature()produces the correct: voidfor the writer half.process()early "method not found" path nowreturn false;instead of barereturn;to satisfy: bool.returnon whether the underlying impl is void.Drive-by cleanup
ThriftClassLoader::findFileInApcu()(merged in THRIFT-6001) was flagged by PHPStan: the trailing$file !== false ? $file : nullwas unreachable because the if-branch reassigns$filetofindFile()(?string). Rewrote the body to a clear two-step fetch-or-recompute and narrowed the return viais_string().Validation (Docker
thrift-php-dev:local+thrift/thrift-build:ubuntu-focal)lib/php/test/Resources/packages/(gitignored — regen happens in CI).phpcs— 0 errorsphpstan(level 5) — 0 errorsphpunitUnit Suite — 637 tests, 0 failuresphpunitIntegration Suite — 108 tests, 0 failuresPart of umbrella THRIFT-5960.
Generated-by: Claude Opus 4.7