![]() |
VOOZH | about |
VAT will be calculated during checkout by Paddle.
We support Purchasing Power Parity.
Buy Writing Readable PHP combined with other courses
and get an extra discount
Buy together with this ebook on cutting edge tactics in PHP 8, accompanied by videos and practical examples.
front-line-php.com
Buy together with this video course to get started with Pest and PHPUnit by Brent Roose & Freek Van der Herten.
testing-laravel.com
Subscribe to our complimentary newsletter
to receive handy code readability tips from the premium course.
You get a weekly email containing a clean code tip and
occasional course updates for the next five weeks.
In a previous part we've mentioned that grouping code in paragraphs is good for readability. There's another advantage: it visualises the steps a function takes to get to a certain results. It makes sense to order those steps in a way that's natural to you, the programmer.
Our favorite technique of ordering a function's steps is by first handling all edge cases, and keep the last step in a function for the most important part: the "happy path". Here's an example where the happy path is handled first. Try to read it: you'll notice that, besides some code overhead, it's also more confusing to understand what this code does from a first read.
// core functionality comes first, special cases handled at the end
publicfunctionsendMail(User $user, Mail $mail)
{
if ($user->hasSubscription() && $mail->isValid()) {
$mail->send();
}
if (! $user->hasSubscription()) {
// throw exception
}
if (! $mail->isValid()) {
// throw exception
}
}
So instead, let's first check and handle all edge cases followed by the happy path as the last step of the function. Conveniently, it doesn't need to be wrapped in a condition anymore if all edge cases have already been handled.
// special cases handled first, core functionality comes later
publicfunctionsendMail(User $user, Mail $mail)
{
if (! $user->hasSubscription()) {
// throw exception
}
if (! $mail->isValid()) {
// throw exception
}
$mail->send();
}
Consider adding the unit to the name whenever you work with something measurable.
// bad: we don't know what that 100 represents
$averageTime =100;
// good: we now know that it is 100ms
$averageTimeInMs =100;
Another way of dealing with this is by creating dedicated objects. Imagine that you need to work with a percentage. Which of these is correct?
$percentage =0.5;
$percentage =50;
You canβt tell what your application expects. Letβs now use an object with a static constructor, one for each possibility.
classPercentage
{
publicstaticfunctionfromInt(int $percentage):self
{
returnnewself($percentage);
}
publicstaticfunctionfromFloat(float $percentage):self
{
returnnewself($percentage *100);
}
privatefunction__construct(
publicint $value;
) {};
}
Using a `Percentage` class clarifies that an integer is expected.
$percentage =Percentage::fromInt(50);
In PHP 7.4 the null coalescing assigment (or equal) operator was introduced. This is what it looks like: ??= and as its appearance seems to suggest, it allows you to combine the null coalescing operator from the previous chapter with an assignment.
// traditional way of handling null for an optional argument
functionmyFunction(MyClass $object =null)
{
if (is_null($object)) {
$object =newMyClass(); // assign a default value
}
// ...
}
You could improve it like this:
// using the null coalescing operator
functionmyFunction(MyClass $object =null)
{
// set $object to its own value, unless it's `null`, then fall back to a new instance of MyClass
$object = $object ??newMyClass();
}
We're down from three lines of code to set a default value to just one. A significant improvement but using the null coalescing assignment operator we can do even better.
// using the null coalescing operator
functionmyFunction(MyClass $object =null)
{
// only set $object to a new instance of MyClass if it's `null`
$object ??=newMyClass();
}
PHPStan is a wonderful static analysis tool that can detect many types of errors. In this course, you'll learn how to use this tool to eliminate entire categories of bugs. Let's take a look at a small example.
When using arrays, you can hint at specific keys and their type. Imagine you have an array with a personβs properties. You could type-hint the properties like this:
/**
* @paramarray{first_name: string, last_name: string} $personProperties
* return string
*/
functionfullName(array $personProperties):string
{
// ...
}
Should we try to use `$person[βnon-existingβ]` then PHPStan would warn us with:
Offset'non-existing'doesnotexistonarray{first_name: string, last_name: string}.
```
And here's a very nice bonus: when using this type docblock modern IDEs can provide autocompletion when working with the array keys.
Ain't that great? We just made it far less likely that you'll use an non-existing array key.
In your project, you might need to build up queries conditionally.
$postsQuery =Posts::query();
// adding a condition by hand
if ($latestFirst) {
$postsQuery->latest();
}
$posts = $postsQuery->get();
You can clean this up with the when method. The query will only be modified if the first argument is truthy.
useIlluminate\Database\Eloquent\Builder;
$posts =Post::query()
->when($latestFirst, fn(Builder $query) => $query->latest())
->get();
"Writing Readable PHP" will give you some amazing tips on how to write clean and modern PHP applications. It's full of tips I learned myself over the years as a programmer and it's great to see them all come together in this book.
Freek and Christoph bring a ton of experience to the table. They make sure you have all the tools you need to keep your code readable for years to come.
Having spent more than 10 years writing PHP, I thought I knew everything I needed. I learned a lot more than I expected as an experienced developer, and can see a lot of benefit in this course for developers of any level.
βClean codeβ covers a wide spectrum, but Freek and Christoph manage to boil the essence of beautiful programming down to a very useful set of guidelines. I especially recommend the section on static analysis for programmers looking to take their skills to the next level.
Christoph is a Laravel enthusiast hailing from Austria. He has created successful courses such as Mastering PhpStorm and Laravel Core adventures. He has also created Larastreamers, a popular aggregator for all Laravel related streams on major video platforms.
Freek works at Spatie and has created countless popular Laravel and PHP packages. He has a passion for spreading his knowledge via his well-respected blog freek.dev, user groups, conference talks, and streams on his YouTube channel. He is a developer and partner at Spatie. After hours, he also organises the Laravel Worldwide Meetup and the Full Stack Europe conference.
Spatie is a web development agency
that crafts web applications, courses
& open source packages in the Laravel ecosystem. With over 300 open source packages for Laravel
and PHP, chances are that your composer.json has our
name in it.
We are @spatie_be on Twitter.
Learn how to build larger-than-average Laravel applications and maintain them for years to come.
laravel-beyond-crud.com
An ebook on cutting edge tactics in PHP 8, accompanied by videos and practical examples.
front-line-php.com
The best resource to get started with event sourcing in your Laravel applications.
event-sourcing-laravel.com
Learn how to create a Laravel package in this 4 hour video course.
laravelpackage.training
A video course to get started with Pest and PHPUnit by Brent Roose & Freek Van der Herten.
testing-laravel.com
VAT will be calculated during checkout by Paddle.
We support Purchasing Power Parity.