Exakat 1.0.6 review : Fuli
Exakat 1.0.6 review : Fuli

Exakat 1.0.6 review

Long life to the first Exakat version for December 2017. While we have started a #phpadvent calendar with our favorite analysis and their impact in the code, we keep on packing new reviews in the Exakat engine. This week, we have two new analysis : never used parameters, and avoid using boolean as parameters. We also upgraded the unused parameters and useless instructions analysis. Let’s go with the exakat 1.0.6 review.

Never used parameters

There is already the unused functions analysis, with tracks functions definitions that are not used. Ideally, any unused function must be removed, used. Actually, this also applies to classes, interfaces, traits, constants, etc.

With functions, the previous rule applies also to parameters. Unused parameters may be removed from the function signature, keeping it simple.

Parameters eventually go unused when they provide a very unusual feature for example, did you know that PHP native function count() had a second argument ? It is used to count recursively arrays. It is seldom used, and actually, rare are people who know about it.

Parameters of custom functions meet the same problem. They also may see the parameter get totally abandoned. When this happens, the parameter get a default value, and is always called by default. This is what this analysis is tracking.

<?php

function foo($a, $b, $c = 1) {
return $a + $b + $c;
}

// $c is never mentioned
echo foo(1,2);
echo foo(1,3);
echo foo(12,4);
echo foo(1,9);

function foo2($a, $b) {
return $a + $b + 1;
}

?>

The foo() function above may very well be rewritten as foo2(), dropping the usage of the third parameter.

You may mind that reducing the parametrization of a function is a bad thing : after all, this means hardcoding 1 in the body of the function. On the other hand, foo() is now simpler, and easier to use. As long as it doesn’t require the last argument, it should be dropped.

Avoid using boolean as parameters

Other languages just ban completely the usage of boolean as parameters. The rationale behind it is that it is better to make two distinct methods, rather than cram two different algorithms in the same method.

On the client side, booleans should be at least named. This prevents head scratching, when the list of boolean is long, or short.

<?php

$time = microtime(true);

// with constant
const AS_DOUBLE = true;
const AS_STRING = false;

$time = microtime(AS_DOUBLE);

?>

So, boolean used as argument in method calls are reported, unless they are replace by a named constant, or a variable.

Upgraded Unused Arguments

The Unused Arguments analysis was omitting argument passed by ‘use’ in a closure.

Indeed, in a closure, there are two kind of arguments: the one provided at definition time (use) and the ones provided at call time (the arguments). This way, a closure is able to collect arguments from two different points in the code. Then, they behave, almost, identically.

<?php

function ($x) use ($y) {
return $x;
}

?>

EPIC : Exakat PHP Index of Code

We published the first version of the Exakat PHP Index of Code: EPIC. It is a wide survey covering over 1700 PHP projects. It gives us a special vantage point to see how code smells are spread in PHP code. For example, a little less than 50% of PHP uses the no scream operator: I always though this was higher.

This index is meant to help follow PHP practices and usage. This shows that some code manage to avoid bad practices. It also identify some errors as very unusual, but none the less important, like unused private properties (23%) and don’t echo errors (15%).

The index is available at https://178.62.231.40/exakat-coding-index-2017-11/, and also in JSON format for reuse.

More useless instructions

Thanks to Vladimir Reznichenko, we added this useless codes:

<?php

$a = count(array_keys($array));

$b = array_unique(array_keys($array));

?>

array_keys() doesn’t change the elements count in the array, and is an extra useless step. This would also apply to array_values(), array_flip(), array_combine(), array_reverse(), array_map() and even a few others.

array_unique() is useless with array_keys() as keys are already unique.

Happy PHP code reviews

Exakat 1.0.6 is paving the way for the 1.0.7, which is already promising some exciting new features. We are seeing some work on speed, dependencies and readability getting near finished state nicely.

All the 320+ analyzers are presented in the docs, including the classic ‘No Class As Typehint‘, which makes sur that methods always depends on the local class, or they should be functions. Download Exakat on exakat.io, upgrade it with ‘exakat.phar upgrade -u’ and like us on github: https://github.com/exakat/exakat.