laminas / laminas-httphandlerrunner

Execute PSR-15 RequestHandlerInterface instances and emit responses they generate.
https://docs.laminas.dev/laminas-httphandlerrunner/
BSD 3-Clause "New" or "Revised" License
63 stars 20 forks source link

Unnecessary closure? #3

Closed JoyceBabu closed 4 years ago

JoyceBabu commented 4 years ago

Is the closure required here? Can't $serverRequestErrorResponseGenerator be assigned directly to $this->serverRequestErrorResponseGenerator?

https://github.com/laminas/laminas-httphandlerrunner/blob/e3d1b45283cf741de84a11661743143a98f1f899/src/RequestHandlerRunner.php#L77-L80

weierophinney commented 4 years ago

This assignment is done to provide type-safety. By wrapping it in a closure, you'll know as soon as it is invoked if the $serverRequestErrorResponseGenerator returns anything other than a ResponseInterface — as the code consuming that callable requires that return type.

Basically, it's a way for us to guarantee type safety without requiring an interface to provide the signature.

boesing commented 3 years ago

Just a note here, I've removed that dedicated callable in v2 and added it to the __construct psalm annotation. When using psalm in upstream projects, this will lead to a static-code-analysis error rather than a runtime error.