Exakat 1.0.4 reviewExakat 1.0.4 review

Exakat 1.0.4 is out, in time to help migrate to PHP 7.2. November 30th, the next great version of PHP will be published. It is now time to review your PHP code. So, this week, we worked hard to finish the impressive line of update that PHP 7.2 is offering. In the same time, we also publish the first analysis for PHP 7.3, and a number of ever-green : suggest using list() with foreach instead of arrays, substr() and array_slice should go first, support for ext/vips, don’t forget new before your classes. Let’s see the exakat 1.0.4 review !

Full PHP 7.2 audit

PHP 7.2 comes with a load of new features and improvement. The good news is that static analysis can help on the vast majority of them. The bad news is that the linting process, which was so useful in PHP 7.0, is rather mute when moving to PHP 7.2. So, you need a static analyser to review your code, like Exakat.

The full review of the audit for PHP 7.2 is available with version 1.0.4.

Performance tip : substr() or array_slice() first

Any treatment on a string should be passed after it was shortened. For example :

$clean = strtolower(substr($string, 0, 10));
$clean = substr(strtolower($string), 0, 10);

The first option is the fastest one, as strtolower works on a shorter string, instead of wasting cycles to have them removed right after the call. Substr() may also be any shortening string, like trim() or dirname(). The same applies to array_slice (and array_splice(), and…) :

$clean = array_map('foo', array_slice($array, 0, 10));
$clean = array_slice(array_map('foo', $array), 0, 10)

Depending on the size of the string, and the importance of the reduction, this quick tip may make your code faster.
On the other hand, this won’t work if sorting is important.

Upgrade your foreach loop with list

foreach() loops accept list as values : this way, you may assign several values at the loop time, saving you from using array notation. For example :

// $names is an array of arrays, with first and last names.
foreach($names as $name) {
	$person[] = $name[0].' '.$name[1];
// unpack the array immediately into variables 
foreach($names as list($first, $last)) {
	$person[] = $first.' '.$last;
// You may also use PHP 7.1 new list notation : 
foreach($names as ['last' => $last, 'first' => $first]) {
	$person[] = $first.' '.$last;

The whole loop is easier to read, and the assignation make it simple to read the code and understand what is happening.
Forgotten new
‘Forgotten new’ is an extension of the famous ‘forgotten new after throw’ rule. The latter rule affect 3% of code repositories, and it came to our attention that forgetting new altogether is very possible. To reduce false positives, only functions that may be a class in the repository are reported. We’ll see in a month how popular it is: I expect it at a lower rate, as it is easier to catch in the code, because of a fatal error at execution time.

throw myexception('something happened here');
class foo {}

// hasty fix ? 
$x = foo();

Extension vips

A new extension for the vips image processing library was released by the good people at liips. It is now supported by Exakat, in the appinfo. Let see where it will be in a year’s time.

First PHP 7.3 analysis are ready

PHP 7.2 is still due in a couple of days, but PHP 7.3 is already under work. And, at the moment, two major differences are ready : PCRE2 lib for regex, and the final empty argument in function calls.
Preparing for PCRE2 means that options X and S are now under scrutiny within the regexes. The final empty argument (foo(1,2,3,)) is a new features, that’s not backported.

Happy PHP code reviews

Exakat 1.0.4 get your code ready for PHP 7.2. If you’re late, then you may also catch up with all the audits for PHP 5.6, 7.0 or 7.1. For the rest of us, who would like to be ready for PHP 7.3, the first analysis are ready. The next one will come, as the next version progresses, every Monday : this is the day where you can run ‘php exakat.phar upgrade -u’ and automatically get the latest version.
All the 320+ analyzers are presented in the docs, including the classic ‘No Substr Minus One‘ , for PHP 7.1 and more. Download Exakat on exakat.io, upgrade it with ‘exakat.phar upgrade -u’ and like us on github: https://github.com/exakat/exakat.