cebe / php-openapi

Read and write OpenAPI yaml/json files and make the content accessible in PHP objects.
MIT License
466 stars 87 forks source link

Viability to add typed setters #112

Closed ryok90 closed 2 years ago

ryok90 commented 3 years ago

Hello! I was messing with an implementation of this package and came across this situation. I wonder if it would be possible to add typed setters for some properties, like Paths have with addPath. Just to have more fluid and intuitive implementation. If yes, would be willing to accept pull request with these kind of modifications?

Something like:

$pathItem = new PathItem();
$post = new Operation();
$pathItem->setPost($post);

I used a typescript documentation package that made a little bit easier to write. That's where my idea comes from.

ryok90 commented 3 years ago

I just saw that there is the magic method __set in the abstraction. Would it be possible to add @method annotations for them?

cebe commented 3 years ago

Not sure I understand exactly how this would look like.

I just saw that there is the magic method __set in the abstraction.

This is for properties and these are declared by @property annotations.

Would it be possible to add @method annotations for them?

Would be possible to implement this via __call() and @method annotations.

// current ways of creating elements
$pathItem = new PathItem();
$post = new Operation();
$pathItem->post = $post;

// alternatively:
$post = new Operation();
$pathItem = new PathItem([
    'post' => $post,
]);

// or 
$pathItem = new PathItem([
    'post' => new Operation([
        // ...
    ]),
]);

// so what you propose would be like this?
$pathItem = new PathItem();
$post = new Operation();
$pathItem->setPost($post);

what additional value does it bring compared to the already existing options?

ryok90 commented 3 years ago

I actually don't know if my editor (VSCode) is not well configured for this but with the annotations it doesn't point type errors.
The only additional value I seek is the type checking for new developers to have a more 'safe' and 'fluid' documentation development.

cebe commented 2 years ago

@property annotations come with a type so adding methods does not add any additional way of type checking. Tools like PHPStan should be able to detect this.