Simple PHP Control Structure Refactoring
Tags: php, refactorit, beginners, laravel
Published: 2019-05-02 15:02:06

*This article was originally posted on my blog - [Simple PHP Control Structure Refactoring](https://www.georgehanson.co.uk/blog/simple-php-control-structure-refactoring)*

Before we get into it, I posted all three of these little refactors on my [Instagram](https://instagram.com/_georgehanson_) and [Twitter](https://twitter.com/GeorgeHanson_) pages. Be sure to check those out as there is a handy little image you can save.

One common thing I see when looking at code is some simple refactors that can make it slightly more readable. One of these "code smells" are if control structures. There are some occasions where these can be made much simpler and even reduced to just one line.

In this article I'll go through three simple refactoring tips to help tidy up your code. I hope you find them useful so let's begin. 

##Ternary Operator

The ternary operator is a simple but powerful operator that I find myself using throughout my code-base. This operator allows us to easily check a condition and return a value in just one line.

Let's take a look at an example. Here we are looking at a five line control structure. We are checking if the user is logged in or not and changing the value of the `welcome` variable accordingly.
```php
if ($user->isLoggedIn()) {
  $welcome = 'Welcome '.$user->firstName;
} else {
  $welcome = 'Welcome Guest';
}
```
Simple enough right?

Well we can make it even simpler using the Ternary operator. Here is an example of how we could refactor this control structure.
```php
$welcome = 'Welcome '.($user->isLoggedIn() ? $user->firstName : 'Guest');
```
And there we go. We have refactored the control structure into just one line. But how does this work? Let's break it down. The first thing we are doing is concatenating the result of the conditional to the string `Welcome  `. With me so far? 

Great. So let's take a look at the conditional itself. We are checking if the user is currently logged in, if they are it returns the value after the `?` which in this case is the users first name. If not it returns the value after the `:` which is "Guest".

As you can see this is a really simple, yet powerful shortcut to clean up control structures.

##'Elvis' Operator

Ok, ok. This isn't actually called the Elvis operator, but it does look like a little guy with Elvis' hair style - so it has been dubbed as such.

This instead is actually the Ternary operator, but a short-hand version of it. There are some cases where we want to return the value of what we are evaluating rather than a separate value. In which case we can use the Elvis operator.

Sounds a little confusing? Don't worry, let's take a look at an example and it will all become clear.

Here we want to get the url for the avatar that should be displayed. If the user has an avatar url we will use that, otherwise we want to use the default.
```php
if ($user->avatarUrl) {
  $url = $user->avatarUrl;
} else {
  $url = '/default.jpg';
}
```
Take a look at how we can make it simpler. Here is an example of how we could refactor this control structure.
```php
$url = $user->avatarUrl ?: '/default.jpg';
```
Can you see why it has been dubbed the Elvis operator?

You can see that this is very similar to using the long-form Ternary operator. But it is slightly different. If the `avatarUrl` is truthy it will return that value, otherwise it will return the string. This is the same as writing the following:
```php
$url = $user->avatarUrl ? $user->avatarUrl : '/default.jpg'; 
```

##Null Coalescing Operator

This is the final operator I'm going to talk about. This operator strictly checks if the left operand is not null or if it exists as a variable, array index or object property. This is another awesome little feature that was added in PHP 7.

This operator acts similarly to the PHPs `isset` function as well as the 'Elvis' operator.

The most common use case I find for these is checking if an item exists in an array and if it does setting that value to a variable, otherwise setting it to a default. That way we avoid errors where the index does not exist.

Let's take a look at an example.

Here I am checking the data in a `POST` request to see if the user has provided a slug for the post. If one has been provided we will set the `slug` variable to that value, otherwise I want to call a `generateSlug` function.
```php
$slug = isset($_POST['slug']) ? $_POST['slug'] : generateSlug();
```
As you can see we are already using the Ternary operator but we can make this even simpler by using the Null Coalescing operator.

To use this operator we simply use `??` with the default value as the right operand. Here is how we can refactor the code above to make it easier to read.
```php
$slug = $_POST['slug'] ?? generateSlug();
```
Much nicer, using the Null Coalescing operator we have simplified it massively. It checks if the `$_POST['slug']` value exists and if it does, assigns it to the slug variable. Otherwise we call the function.

I hope that you found this post useful. I know that I use all three of these techniques throughout my code-base to clean it up and I hope you do to. Be sure to check out my [Twitter](https://twitter.com/GeorgeHanson_) and [Instagram](https://instagram.com/_georgehanson_) pages for handy little images you can save.