dshafik / bag

Immutable Value Objects for PHP
https://bagvalueobjects.com/
MIT License
70 stars 6 forks source link

Incorrect Typehinting for Bag::from() #44

Closed dshafik closed 2 weeks ago

dshafik commented 3 weeks ago

Describe the bug

Reproduction: https://phpstan.org/r/3c452035-5203-439f-8c93-372be51d80a8

The Bag::from() method has the following signature:

public static function from(mixed $values): Bag { }

(link)

When calling MyValue::from() phpStorm doesn't like that it returns a Bag rather than static, but when changing to static phpstan has the following complaint:

Method Bag\Bag::from() should return static(Bag\Bag) but returns Bag\Bag.

This is because the return is the result ofInputPipeline::process() which returns Bag (public static function process(BagInput $input): Bag {}), but the actual return value is the value of new ($input->bagClassname) (which should be type-hinted class-string<Bag>) from the passed in BagInput for the InputPipeline::process() call.

What docblock is needed to type hint this correctly?

dshafik commented 2 weeks ago

Fixed with c56f65a