Digging into: Humbug

While I’ve already used Humbug a few time, a recent article made my realize that I didn’t really know how it worked. That’s when I got the idea to dig into Humbug to learn how it works, and publish my findings here. Before we start, let’s quickly give some context on Humbug. What’s Humbug? « Humbug is a Mutation Testing framework for PHP » In a nutshell, you write unit tests to prevent regressions in your application and use code coverage as an indicator of how well your application is tested or protected. ...

October 23, 2016 · 6 min · Kévin Gomez

Write documentation as tests in PHP using Rusty

Languages such as Python, Rust, etc. provide a way to write code samples right inside doc-strings. They are supposed to be easy to read and they are called “documentation as tests” because they also can be executed. I thought that it was a great way to ensure that your documentation is up-to-date with your code so I searched a way to do the same in PHP… but I could not find anything. ...

May 22, 2016 · 1 min · Kévin Gomez

Efficiently creating data chunks in PHP

While I was working on a library to build sitemaps and sitemap indexes, I identified the need to aggregate several iterators and then build chunks of a precise size from this aggregate. To be clearer: given a list of URL providers for a sitemap index, I wanted to iterate over all the data exposed by these providers in chunks of 50.000 (the maximum number of URL allowed in a sitemap). ...

February 26, 2016 · 2 min · Kévin Gomez

Use cases for PHP generators

Despite being available since PHP 5.5.0, generators are still largely underused. In fact, it appears than most of the developers I know understand how generators work but don’t seem to see when they could be useful in real-world cases. Yeah, generators definitely look great but you know… except for computing the Fibonacci sequence, I don’t see how it could be useful to me. And they’re not wrong, even the examples in PHP’s documentation about generators are pretty simplistic. They only show how to efficiently implement range or iterate over the lines of a file. ...

January 20, 2016 · 7 min · Kévin Gomez

PHP ::class keyword

According to PHP documentation about the ::class keyword, it can be used for class name resolution: You can get a string containing the fully qualified name of the ClassName class by using ClassName::class. 1namespace NS { 2 class ClassName { 3 } 4 5 echo ClassName::class; 6} 7 8// outputs: "NS\ClassName" It’s handy when you need to pass FQCN around. Some people even started using it to make sure that their code was referencing existing classes. Except that it’s written nowhere in the documentation that this keyword will ensure that the generated FQCN maps to an existing class! ...

January 15, 2016 · 1 min · Kévin Gomez

Why is uniqid() slow?

While profiling RulerZ with Blackfire.io, I noticed that a non-negligible amount of time was consumed by PHP’s uniqid() function. For those who don’t know, this function provides an easy way to generate unique identifiers based on the current date and an optional prefix. Wait, the “current date” part can actually be interesting for our performance issue. What happens if we write something like this: 1foreach ($foo as $bar) { 2 $awesomeCollection[] = new CoolObject(uniqid(), $bar); 3} A simple loop like this should run pretty fast, so if uniqid() simply relies on the current date we should have collisions… but it is not the case. Why? ...

July 26, 2015 · 2 min · Kévin Gomez

RulerZ, specifications and Symfony are in a boat

In my previous post, I tried to answer the following question: how do you keep your Doctrine repositories from growing exponentially? Long story short, I came up with a generic solution based on the Specification pattern that essentially abstracts and simplifies the way we write and compose queries. And the best part is that it works with Doctrine but also with any other data-source. RulerZ was born. Of course, there was a real need behind my previous question. For one of my current projects, I wanted to be able to switch from one data-source to another. My application would use Doctrine in development and production environment but for tests I wanted my data to live in memory. ...

March 14, 2015 · 5 min · Kévin Gomez

On Taming Repository Classes in Doctrine… Among other things.

A while ago I stumbled upon a - rather old but nonetheless interesting - post written by @beberlei. In his post, he highlighted the issues of having too much responsibilities in a repository and suggested a solution based on the Specification pattern. ...

February 7, 2015 · 3 min · Kévin Gomez

Symfony best practices

Here is a digest of the best practices that are commonly followed for Symfony2 projects. They are given as is, in no particular order and are far from being exhaustive. Naming things find a common naming scheme for your team and stick to it be explicit be consistent same for routes and services naming use service alias: mailer is better than swiftmailer.mailer.default prefer Vendor/Bundle/CoolBundle over Vendor/CoolBundle for bundles namespace for an application, you can even skip the vendor part Config Which configuration format should you use? ...

April 8, 2014 · 3 min · Kévin Gomez

Split Symfony2 YAML validation configuration file

Important : as j0k pointed out in the comments, Symfony 2.5 changed the way validation files are loaded. Refer to this StackOverflow answer if you are using Symfony >= 2.5. Defining validation rules for several entities in the same file can really be a pain. Unfortunately, Symfony2 only looks in the validation.yml file by default. Let’s see how we can split the following file: The solution lies in the AcmeBlogExtension class, and more specifically in the validator.mapping.loader.yaml_files_loader.mapping_files parameter. As this parameter defines the list of files used to map configuration rules to their entities, we just have to add our owns to the list. ...