Refactoring #5: From arrays to Data Transfer Objects
Tags: laravel, webdev, refactoring, php
Published: 2022-04-23 16:15:53

We recently pushed a new improvement at [OLM](https://openlittermap.com/) to reward admins with XP for verifying uploaded images. The most critical part of the feature, a class that does some calculations and returns some stats, introduces a small design flaw. We're going to see how to fix that in this post.

The problem is simple: our class is returning an array of key-value pairs, and this array is used in quite a few other places in code.

{% gist https://gist.github.com/GeniJaho/295bb7fe3bff7b46374007ac89ac4988
%}

The flaw here is that if we decide to change, add, or delete a key in this array, we'll have to touch every usage of it. And trust me, that's going to introduce bugs.

##Using an object instead of an array

This problem is perfect for refactoring to an object. In fact, these objects are called Data Transfer Objects (DTO), as they're meant to transfer data of some kind, not necessarily with logic in it. In our case, it's a 'tags difference' object, but we'll try to find a proper name for it. There are some steps to perform this refactoring safely, but a simple, dummy implementation looks like this.

{% gist https://gist.github.com/GeniJaho/bbb276ad5f884ddc36c77936ac0b2b9c
%}

We're storing the class in the `app/DTO` directory, you might find a better place in your case. Also, the usages of the new return value are now much simpler to understand and work with.

##What's inside the Data Transfer Object?

Generally, the key-value pairs we had in the previous array now become first-class citizens as private object properties. You're going to have to write some getters for them.

It's important to remember that we only set the properties once in the constructor. That gives us a simple validation out of the box. Of course, you can apply extra validation, parsing, and filtering you might need in the constructor as well. I'd say keep the DTO simple though, try to look at it simply as a way to move some data from one place to another. I'd advise you also set the class as `final`, and implement the public getters and setters only when you need them.

Our final class looks like this.

{% gist https://gist.github.com/GeniJaho/24ef01bf7628d116d6bbd821f53b6f2a
%}

We'll probably start refactoring into DTOs in other parts of the code as well. We could use your help though, wink.

The benefits of using DTOs are numerous, there's even a [package from Spatie](https://github.com/spatie/data-transfer-object) for them. See the [package's release post](https://freek.dev/1944-data-transfer-object-v3-has-been-released) and this awesome [DTO explanation](https://stitcher.io/blog/laravel-beyond-crud-02-working-with-data#structuring-unstructured-data) for a more in-depth take and how to implement and use them in a more sensible way.

> The code used for illustration is taken from the [OpenLitterMap](https://github.com/OpenLitterMap/openlittermap-web) project. They're doing a great job creating the world's most advanced open database on litter, brands & plastic pollution. The project is open-sourced and would love your contributions, both as users and developers.

---

Originally published at [https://genijaho.dev](https://genijaho.dev/blog/refactoring-from-arrays-to-data-transfer-objects).