Skip to main content

Command Palette

Search for a command to run...

Doing stricter checks in PHP

Updated
3 min read
Doing stricter checks in PHP
G

Full-stack web dev with a passion for software architecture and programming best coffees.

A nice refactoring I've learned recently is to utilize types in conditionals.

For years I’ve been doing if (!empty($posts)), but Rector, a tool to automatically refactor code, changed that to if ($posts === []). The main advantage of using $posts === [] is that it is more explicit and less prone to unexpected behavior.

It might not seem like much, but it's quite telling of the codebase you're working with. Using $posts === [] demonstrates a higher level of attention to detail and a commitment to writing more reliable and predictable code. It shows that you are thinking about the specific requirements of your code and taking steps to ensure that it behaves as expected.

On the other hand, using !empty($posts) can suggest a more casual approach to programming, where you are relying on PHP's default behavior rather than explicitly defining the expected behavior of your code. While this may be acceptable for smaller projects, it can lead to unexpected bugs and difficulties as your codebase grows and becomes more complex.

Here are some other refactorings I've learned that make me feel like I'm in charge of the code, and not the way around.

Using strict comparison to check for null or empty strings:

// Before
if ($username == null) {}
if (!$username) {}

// After
if ($username === null) {}

The first two conditions check if $user is not truthy, which includes checking if it's null, false, 0, or an empty string. With the after example, we can use the strict comparison operator === to specifically check if $username is null.

Type hints in function parameters:

function calculate(int $x, int $y) {
    // Do something
}

In this example, the int type hint ensures that $x and $y are integers. If a non-integer value is passed to the function, PHP will throw a TypeError.

Using array_key_exists() to check for array keys:

// Before
if (isset($data['username'])) {
    // Do something
}

// After
if (array_key_exists('username', $data)) {
    // Do something
}

In the before example, we're using the isset() function to check if $userData['username'] exists. With the after example, we can use the array_key_exists() function to specifically check if the 'username' key exists in $userData.

Null coalescing operator and null coalescing assignment operator:

// Before
if (isset($userData['username'])) {
    $username = $userData['username'];
} else {
    $username = 'Guest';
}

// After
$username = $userData['username'] ?? 'Guest';

In the before example, we're using an if statement to check if $userData['username'] exists and setting $username accordingly. With the after example, we can use the null coalescing operator ?? to set $username to $userData['username'] if it exists, and 'Guest' if it doesn't. Additionally, we can use the null coalescing assignment operator ??= to assign the default value to $username only if it's not already set.

These stricter checks help us write more reliable and predictable code, catch errors early, and save ourselves from potential headaches in the future. While some of these refactorings may take some getting used to, they are worth the effort to improve the quality of our code. Or, just install Rector and let it handle them for you. Happy coding!

L
Lasse Larsen11mo ago

I do prefer data_get over the array key approach. Mainly because it simplifies the getting, as well as having a default null or value.

1
E

Geni Jaho array_key_exists() and isset() are not interchangeable, because they have different use cases.

Of course most of the time you can use array_key_exists instead of isset but in some cases, you can not.

See here for more details

1
L

count($a) === 0 is even more readable, or? 🤔

1
G
Geni Jaho3y ago

I think yes, it's at the same level as empty($a), but the point is we're letting go of a tiny bit of readability in exchange for a more trusted/clear code. When we do $a === [] we know for sure that it's a plain array and not an Arrayable object or similar. It's down to personal preference anyway.

1