quicktype icon indicating copy to clipboard operation
quicktype copied to clipboard

[BUG]: php validation code never executes

Open uuf6429 opened this issue 5 months ago • 2 comments

Issue Type

quicktype PHP output

Context (Environment, Version, Language)

Input Format: JSON Schema Output Language: PHP

CLI, npm, or app.quicktype.io: app.quicktype.io Version: n/a

Description

Just gave this tool a quick look and tried the online example, which generates code like so:

    /**
     * @param float|null
     * @return bool
     * @throws Exception
     */
    public static function validateLatitude(?float $value): bool {
        if (!is_null($value)) {
            if (!is_float($value)) {
                throw new Exception("Attribute Error:Coordinate::latitude");
            }
        }
        return true;
    }

PHP already verifies the type of value passed to $value, and in non-strict-type mode, it automatically typecasts the value. In short $value is always guaranteed to match ?float, and therefore the rest of the method doesn't make any sense.

Without strict_types: https://3v4l.org/hmk19 and with strict_types: https://3v4l.org/mNaCv

(note: there seems to be other redundant code, such defining the return type and then adding a /* some_type */ after return - totally non-standard and unhelpful)

Input Data

Used PHP demo

Expected Behaviour / Output

Less redundant / dead code should be generated.

Current Behaviour / Output

A validation guard is generated that never executes.

Steps to Reproduce

Use PHP demo

Possible Solution

IMO the public static validation method should not be generated, but at least it should not contain the aforementioned dead code.

uuf6429 avatar May 28 '25 22:05 uuf6429