Little Snippets to Keep Your Code Cleaner

Please note that this article is older than 6 months and was written in a massively changing topic so it can be deprecated. We try to up to date our content, but sometimes there can be mistakes. Thank you for your understanding!

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.

Special thanks for the following recource(s): Icon made by Freepik from

16 thoughts on “Little Snippets to Keep Your Code Cleaner

  1. I agree with all except from this one:

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

    // we can do this
    if (in_array([‘val_1’, ‘val_2’, ‘val_3’], $a)) {
    // perforn the action

    Because in this one if ($a === ‘val_1’ || $a === ‘val_2’ || $a === ‘val_3’)
    if the first is evaluated to true, the rest will be ignored, because of the logical or ( || ).

    The same works with false and logical and (&&)

    1. You are right! But the result will be the same! But yes, the rest of the condition will be ingnored after one evaluated true!

      I add your comment to the post, to make it clear for others as well!

      Thank you!

          1. If you are comparing the same types, like strings to strings (the example I showed was like this) you don’t need strictness. If you have an other use case and you need it, then you can set it. But again, you don’t have to check strictly in every case. It’s up to you.

  2. Directly returning the result of a comparison is not such a good idea. Assign it to a variable first then return that instead. It makes debugging much easier.

  3. Type Checking: “Also, this gives the possibility to accept a Team instance and an int also.”

    Accepting only one type for a variable would be much better. You will avoid having the “if”, which is an extra responsibility of that function. You could just type hint the variable (also primitives as of PHP 7). Ideally you would send the minimum that function needs, which would by a primitive (the team id as int), but of course, the Team object is OK if the case requests this.

    public function scopeOfTeam($query, int $teamId)
    return $query->where(‘team_id’, $teamId);


    public function scopeOfTeam($query, Team $team)
    return $query->where(‘team_id’, $team->id);

Leave a Reply

Your email address will not be published. Required fields are marked *