Little Snippets to Keep Your Code Cleaner

PHPPosted on

Sometimes it’s harder to keep your code clean and readable than to implement some architecture in your application. We collected some snippets that may help you to refactor your code.

Array Casting

It happens quite often, we accept a parameter both as a string or as an array. Instead of checking the type, then convert it to the proper one, we can simply casting the parameter to an array.

// Without casting
$param = is_array($param) ? $param : [$param];
array_map(function ($item) {
}, $param);

// With casting
array_map(function ($item) {
}, (array) $param);

Of course, it looks nothing, you just save one line. But imagine this, when you work with complex loops or nested arrays with huge datasets. It makes sense and helps you to keep the code cleaner.

Type Checking

Type checking is also a neat feature. A small type check can save few lines of code and in some cases, it also gives you some flexibility around your parameters.

public function scopeOfTeam($query, $team)
    $team = $team instanceof Team ? $team->id : $team;

    return $query->where('team_id', $team);

In this case, we can check the type of the given parameter in one line, so we don’t need to write if-else statements everywhere as you see. Also, this gives the possibility to accept a Team instance and an int also.

Refactor Long If Statements

Sometimes, we need to check some conditions before we perform any action. But, when our application grows, if statements can grow bigger and bigger, so we may consider refactoring where we can. A small example:

// instead of this
if ($a === 'val_1' || $a === 'val_2' || $a === 'val_3') {
    // preform the action

// we can do this
if (in_array($a, ['val_1', 'val_2', 'val_3'])) {
    // perform the action
Note that, the first example works a bit differently than the second one. After a condition evaluated true, the rest will be ignored. In some cases, it can be faster. Thanks for the tip to Zois Pag.

The result will be the same, but the second one is much cleaner. We can leave the || operators and we need to reference the $a variable only once.

Remove Unnecessary If Statements

It’s a common mistake, we overuse the if-else statements. Of course, there are cases where can’t omit them, but also, there are some, when we should.

// before
if ($a === 'b') {
    return true;
} else {
    return false;

// after
return $a === 'b';

But what if the logic is reversed? So, if the condition is true and we want to return false? In the if statement we just need to reverse the true and the false of course, but in the refactored version we have an even easier job. Just add the ! operator before the check and you are done! It will reverse the return value!


Every day we find solutions and codes what are full of overcomplicated logic. In long term, it’s bad for the developers and the product itself as well. We just tried to collect some examples, where if you just add a little more effort, you can make your code better and cleaner.

Need a web developer? Maybe we can help, get in touch!

Similar Posts

More content in PHP category