fix(php): stop re-checking scalar types PHP already enforces#3013
Merged
Conversation
Co-Authored-By: gpt-5.6-sol via pi <noreply@openai.com>
Reformat the scalarUnion schema literal to Biome's canonical layout and move the "method found" assertion out of the validationMethod helper (noMisplacedAssertion) by throwing instead, so `npm run lint` passes. Co-Authored-By: Claude <noreply@anthropic.com>
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.
Bug
quicktype's PHP output generates a
validate<Property>()static method forevery property. For primitive-typed properties (
bool,int,float,string), the generated body contains runtimeis_bool/is_integer/is_float/is_int/is_stringguards that can never fire, because PHPitself already enforces the parameter's type hint (e.g.
?float $value)before the method body ever runs — PHP throws a
TypeErrorfirst, or, innon-strict-types mode, coerces the value to match the type. Example (before
the fix):
Root cause
emitValidateMethodinpackages/quicktype-core/src/language/Php/PhpRenderer.tscalls
phpValidate()unconditionally for the property's declared type. Forscalar (
bool/int/float/string) branches,phpValidateemits anis_*check on the parameter — but the parameter tovalidate<Property>()already carries that exact scalar type hint, so the check is dead code that
can never throw.
Fix
phpValidatenow accepts askipPrimitiveTypeCheckflag. Thevalidate<Property>()call site passestrue, so the top-levelbool/int/float/string checks are skipped there (PHP's own type hint already
guarantees them), while validation for nested/recursive shapes — array
elements, union members, transformed strings (e.g. UUID format) — is left
untouched, since those still need runtime checks that PHP's type system
cannot express.
Test coverage
test/unit/php-validation.test.ts: generates PHP for a schema withboolean/integer/number/nullable-number/string/array/union properties and
asserts the scalar
validate<Property>()bodies no longer containis_bool/is_integer/is_float/is_stringchecks, while confirmingchecks are still emitted for array elements and union members.
test/inputs/json/priority/php-validation.jsonwas added to the PHPfixture's JSON inputs (
test/languages.ts) so the generated code is alsoexercised end-to-end through the PHP fixture's round-trip test.
Verification
npm run buildpasses.npx vitest run test/unit/php-validation.test.tspasses.node dist/index.js --lang php --src-lang schema coord.schema.json)and confirmed
validateLatitude/validateLongitudenow generate as:FIXTURE=php script/test) requires a local PHP/composer toolchain that isn't available in this environment, so it was not
run locally; CI will exercise it.
Fixes #2770
🤖 Generated with Claude Code