How to refactor complex if-statements

In this video, which is a part of the Mailcoach video course, Freek shows us how he refactors a complex if-statement.

A technique which I nowadays also like using myself – especially in a React Component’s render method – is to use early returns: instead of nesting if-statements and using else-statements, the function body uses singular if-statements which abort the function’s execution early on in case some condition is/isn’t met. Like so:

public function doSomething($someParameter, $someOtherParameter)
    if ($someParameter !== 0) {

    if ($someOtherParameter !== 0) {

    // do the actual work

πŸ’‘ If you’re a PHP developer I’d recommend the video course to you as it contains some pretty good stuff, such as this video on Pending Objects for example.

Symfony 5: The Fast Track

Symfony 5 recently got released. Fabien Potencier – Symfony’s creator – has also published a book along with its release.

This book – written by Symfony’s creator – lays out a pragmatic approach to developing web applications with Symfony 5: from scratch to production. Whether you are discovering Symfony for the first time or refreshing your knowledge, this practical guide provides the definitive introduction to modern Symfony applications.

Symfony 5: The Fast Track →

Laravel beyond CRUD

Brent shares his Laravel knowledge in a series of blog posts:

In this series, I’ll write about the knowledge we gained over the years in designing Laravel projects. I will take a close look at the Laravel way, and what did and didn’t work for us. This series is for you if you’re dealing with these larger Laravel projects, and want practical and pragmatic solutions in managing it.

I will talk about theory, patterns and principles, though everything will be in context of a real-life, working web application.

The goal of this series is to hand you concrete solutions to real life problems, things you can start doing different in your projects today. Enjoy!

Laravel beyond CRUD →

Roadrunner – High-performance PHP application server, load-balancer, and process manager written in Golang

RoadRunner is an open-source (MIT licensed), high-performance PHP application server, load balancer, and process manager. It supports running as a service with the ability to extend its functionality on a per-project basis. RoadRunner includes PSR-7 compatible HTTP server.

RoadRunner runs your application in the form of workers. The workers are kept alive in between requests. The simplest entrypoint with PSR-7 server API might looks like:

 * @var Goridge\RelayInterface $relay
use Spiral\Goridge;
use Spiral\RoadRunner;

ini_set('display_errors', 'stderr');
require 'vendor/autoload.php';

$worker = new RoadRunner\Worker(new Goridge\StreamRelay(STDIN, STDOUT));
$psr7 = new RoadRunner\PSR7Client($worker);

while ($req = $psr7->acceptRequest()) {
    try {
        $resp = new \Zend\Diactoros\Response();
        $resp->getBody()->write("hello world");

    } catch (\Throwable $e) {

Integrations to get Symfony and Laravel (and more) working with RoadRunner are available.

RoadRunner →

Execute commands over SSH from PHP with spatie/ssh

A new PHP package released by the fine folks at Spatie is spatie/ssh. Leveraging Symfony\Component\Process\Process it allows you to execute commands over SSH from within your PHP code.

spatie/ssh is probably the easiest way to perform a quick SSH command. It doesn’t have a lot of features, but that’s ok. It’s intended as something very lightweight.

$process = Ssh::create('user', '')
  ->execute('your favorite command');

Also comes with download and upload functions, using scp under the hood.

Installation per composer:

composer require spatie/ssh

spatie/ssh (GitHub) →
spatie/ssh Introductory Post →

Redux in 30 lines of PHP

To get the full details behind Redux, Sorin Nunca has recreated it using PHP:

As I usually try to understand the tools I’m using, the following tries to be a toy implementation of Redux in PHP, in the hopes of gaining a deeper understanding of the concepts behind Redux.

Also covers combineReducers.

Redux in 30 lines of PHP →

⁉️ Looking to get started with Redux? Egghead has a great video course by Redux’ creator Dan Abramov.

πŸ’‘ Also check this recent implementation of Redux in 24 Lines of JavaScript

Fire and forget HTTP requests in PHP

Chris White, on creating really fast HTTP requests in PHP, by manually building an HTTP request and sending a payload:

Hand-crafting HTTP requests seemed like an unreliable method at first, but after some pretty extensive testing I can vouch for it reliably sending the requests and the remote server receiving them in full. It can be tricky to craft the request properly (respecting the line feeds that the HTTP specification expects, etc), but that’s why you have tests, right? πŸ™‚

Like so:

$endpoint = '';
$postData = '{"foo": "bar"}';

$endpointParts = parse_url($endpoint);
$endpointParts['path'] = $endpointParts['path'] ?? '/';
$endpointParts['port'] = $endpointParts['port'] ?? $endpointParts['scheme'] === 'https' ? 443 : 80;

$contentLength = strlen($postData);

$request = "POST {$endpointParts['path']} HTTP/1.1\r\n";
$request .= "Host: {$endpointParts['host']}\r\n";
$request .= "User-Agent: Loglia Laravel Client v2.2.0\r\n";
$request .= "Authorization: Bearer api_key\r\n";
$request .= "Content-Length: {$contentLength}\r\n";
$request .= "Content-Type: application/json\r\n\r\n";
$request .= $postData;

$prefix = substr($endpoint, 0, 8) === 'https://' ? 'tls://' : '';

$socket = fsockopen($prefix.$endpointParts['host'], $endpointParts['port']);
fwrite($socket, $request);

This way of requesting performs about 5 times faster than making requests using Guzzle. No surprise there really: the closer you are to the core language, the faster it will work. C is faster than PHP. PHP’s built-in functions are faster than libraries built on top.

Fire and forget HTTP requests in PHP →

πŸ’β€β™‚οΈ Back when I was a lecturer Web we started off with looking at the HTTP protocol before we even wrote one single line of PHP. Mandatory knowledge for everyone working in web imho.

google/cloud-functions-framework – Google Cloud Functions Framework for PHP

google/cloud-functions-framework is an open source FaaS (Function as a Service) Framework for writing portable PHP functions.

An example function looks like this:


use Symfony\Component\HttpFoundation\Request;

function helloHttp(Request $request)
    return "Hello World from PHP HTTP function!" . PHP_EOL;

One can invoke it locally by executing the included router as follows:

export FUNCTION_TARGET=helloHttp
export FUNCTION_SOURCE=index.php
php -S localhost:8080 vendor/bin/router.php

Alternatively you can run it in a Docker container (which you can then deploy to Cloud Run):

docker build . \
    -f vendor/google/cloud-functions-framework/examples/hello/Dockerfile \
    -t my-cloud-function

docker run -p 8080:8080 \
    -e FUNCTION_TARGET=helloHttp \

Installation per Composer:

composer require google/cloud-functions-framework


⚠️ There is a sample Dockerfile included with the repo, but a first glance tells me it needs some polishing as it uses a full blown GAE_RUNTIME Docker base image and directly installs the composer dependencies into the image itself instead of relying on multi-stage builds.