DEV Community

Refactoring "if" statements with lookup arrays in PHP/Laravel

Zubair Mohsin on January 24, 2020

Let's dive right into an example. I was working with third party JSON data which arrives from Webhook request. That data contains information abou...
Collapse
 
aleksikauppila profile image
Aleksi Kauppila

That request data looks to be a handful to deal with and raises some questions about the design of the data:

  • Why phone number in top level?
  • Why phone number in billing_address?
  • Why phone number in shipping_address?
  • Why the duplication?

After we've accepted the fact the data is what it is, we still have at least one big issue to handle.

  • What if the data is not consistent? What if we find two different phone numbers in the data? Which one do we prioritize over another.

To me it seems there's not much room to move around the problem. We'll have to parse the data somehow and it's going to be messy.

The trade off looks to be around "explicit vs. dynamic" algorithm.

Collapse
 
sznroe profile image
Sujan Rai

Thanks