PHP Attribute based code completion (like ArrayShape)

Hi there,

we try to use JSON Schema to define objects in PHP (which are later encoded to JSON). It would be super nice, if we could use IDE code-completion based on the schema itself. I thought of using a custom attribute JsonSchema attribute (using a file with the JSON Schema as a parameter) like so:

Example JSON Schema:

{
"$id": "person.schema.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Person",
"type": "object",
"properties": {
"firstName": {
"type": "string",
"description": "The person's first name."
},
"lastName": {
"type": "string",
"description": "The person's last name."
},
"age": {
"description": "Age in years which must be equal to or greater than zero.",
"type": "integer",
"minimum": 0
}
}
}

Example Attribute usage:

class PersonSchemaTest {

#[JsonSchema('src/person.schema.json')]
public object $person;

public function test() {
// we would love code completion here...
echo $this->person->lastName;
}
}


How to tell PHPSTORM the structure of $person object in an attribute class using information provided by schema? It should work similar to ArrayShape attribute...

#[\Attribute]
final class JsonSchema {

public function __construct(string $schemaName) {

}
}

Thanks in advance, Marcin

 

4 comments
Comment actions Permalink

Correct, not supported yet.

0
Comment actions Permalink

Thank you. Leaving a comment in a YouTrack report is a correct & most efficient way of sharing your thoughts & ideas with our developers!

0

Please sign in to leave a comment.