exakat 0.11.4 review
Exakat 0.11.4 is out, right during Code Europe : meet us in Wroclaw and Warsaw! This version features two new analyzers : ‘Only variable can be passed by reference’ is partially handled by PHP at lint time, and static analysis goes a lot further. Also, some functions may omit return when their result is not used : they are now spotted automatically. Finally, Exakat features a new report called ‘Codeflower’, and many update for the classic Ambassador report.
Only variable can be passed by reference
Benjamin (@benjamincremer) called Twitter to the rescue about a nasty error : only variable can be passed by reference. When calling a function or a method that has by-reference arguments, such arguments must be a valid container : a variable, property, static property or a array element.
<?php function foo(&$f) {} foo($x); // valid foo(3); // invalid ?>
PHP actually lint such code : applying php -l on this script generates the following error : PHP Fatal error: Only variables can be passed by reference. The trick is that function are rarely used where they are defined : functions are defined in one file, and called in many others. In such case, PHP omits the check at linting time, and stops execution later. Several linting checks behave the same : mutually-extending classes cannot be parsed when they are in the same file, but may do it by being in separate files.
So, this check is now performed by Exakat. Functions and static functions are covered, while method call are omitted. PHP functions, when they are known to be using referenced parameter, are also reported (such as sort());
Functions whose return is not used
Sometimes, function or methods don’t need to return anything. They act a a fire and forget piece of code, or some syntactic sugar. In such case, they are called alone, and their results are ignored. Then, they may omit the return keyword, in their code, unless it is for short exit.
Exakat checks all function’s usage, and when the result of the function is not stored or directly used, reports functions that may omit the return keyword.
<?php function setIni($setting, $value) { if ($setting == 'default_charset') { $res = ini_set('default_charset', $value) ; if ($res === false) { throw Exception('Couldn\'t set default_charset') ; } } else { return ; } return $res ; } setIni('default_charset', 'UTF-8') ; // more code setIni('default_charset', 'Latin1') ; ?>
In this example, setIni() actually returns values, but they are always ignored. It may be a good idea to update this situation : either remove the return, or handle it.
Codeflower
A new report is build, code named ‘Code flower’. The report is based on ‘Codeflower‘ from Francois Zaninotto, which is open source. It is used to present tree-like data, such as file hierarchies. We have several dataset extracted from analysis that may be presented as a tree, so this is a greatvisualisation.
Currently, this report comes in three flavors :
- Namespace : this presents all the namespaces used, starting from the root \ (the global namespace). It includes their classes, traits and interfaces. Long sequences of namespaces shows how deep the code is, and flowers around a node shows how crowded a namespace is.
- Inclusions : this is an experimental visualisation, files are linked to each other when one includes the other. The root node is not a file : it is only here to be the starting point. Inclusions are detected with the include keywords (and alike). This is very early testing, as inclusions are a network, not a tree.
- Classes extensions : this visualisation presents the classes in organized along the ‘extends’ keyword. The root is not a class : it’s children are all the classes that extends nothing. Then, all extensions are followed : long chains of links means that the last class is the result of several heritages, while flowers shows classes that are heavily extended.
We are still testing this new report style : it will cover more in the coming versions, as a hierarchy is a classic representation in data in code.
Ambassador report update
The most complete report in Exakat, the Ambassador report, got another round of update : several internal links where added, toward the documentation and file display.
Happy PHP code reviews
Exakat 0.11.4 keeps adding more insights to the reports. This week, we got help from community : Thanks to Benjamin and Francois for your contributions : we will never do it without you! You too may help make PHP code better by suggesting issues to find in the code, or vizualisation that may help cast the best light on the code.
All the 320+ analyzers are presented in the docs, including the famous ‘Getting the last element in an array‘ that suggests using count() or end() to reach the last element of an array. Download Exakat on exakat.io, upgrade it with ‘exakat.phar upgrade -u’ and like us on github: https://github.com/exakat/exakat.