Re: RFC: Not Null Assertion Operator

From: Date: Fri, 07 Feb 2025 11:40:09 +0000
Subject: Re: RFC: Not Null Assertion Operator
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-126327@lists.php.net to get a copy of this message
> Hello, > > I feel like that is more of an architecture problem than a programming one though. Instead of > constantly checking if the conditions are right, use php's type system instead, so it would > read more like: > > if ($response instanceof SuccessfulResponse) { > return $response->data->order->quantity; > } > > throw new UnexpectedResponseException($response); > > I'd argue that having an "undefined" aka "null" quantity in a model at > all is rather strange as that doesn't seem to represent any possible reality outside of a > computer (unless you are dealing with Schrodinger's cat or other quantum effects). > > — Rob Hi, The thing is I am just a consumer of the API, before implementing I did have a thought about having Successful and Error DTOs, but for me that was more work for the same result. First I need to map JSON to generic Response, then do some checks and map from generic Response to another DTO. And it's not just success and error, there are about 20 response codes and several variations of response you could get. And I absolutely agree about null quantity, some of the objects there have every property nullable, it's insane.

« previous php.internals (#126327) next »