exakat 0.12.4 review

Exakat 0.12.4 review

Exakat 0.12.4 for your pleasure. Exakat has progressed on multiples fronts last week : PHP 7.2 is now fully covered for migration. Exakat has now a new favorite : and / &&; a new report for the ‘object’ keyword, a suggestion to reduce isset() calls, and a magic error with arrays. Finally, we have made significant progress for the stability of Exakat with Tinkergraph and Gremlin-server. Let’s review exakat 0.12.4 together!

‘object’ keyword is now forbidden for classes

In PHP 7.2, it is now forbidden to use the keyword ‘object’ as class name. I guess it would be as common as calling a class ‘Void’ or ‘Null’ (and I did…) but now, this is totally forbidden. Just change your code if you are impacted.

New code favorite : and / &&

The new favorite reports usage of logical operators written either as letters (and, or, xor) or characters (&&, ||, ^). The two of them are used as exchangeable. For example,

<?php 
  if(isset($a) and $a != '') {}
  if(isset($a) && $a != '') {} 
?>

They are definitely NOT the same, as with this code :

<?php 
   $b = isset($a) and $a != ''; 
   $b = isset($a) && $a != ''; 
?>

When and if they are used in exchangeable ways, then, they may be a project preference, and are reported as such.

Isset() reduction

Isset() checks if an expression exists, and it is little known that it also accepts multiple arguments, combining them with a ‘and’ operator. This way, the followings are identical :

<?php 
  if (isset($a) && isset($b) && isset($c)) {} 
  if (isset($a, $b, $c)) {} 
?>

Or course, the contrary is also valid :

<?php 
  if (!isset($a) || !isset($b) || !isset($c)) ) {} 
  if (!isset($a, $b, $c)) {} 
?>

This will make the code shorter, and probably nothing more. It’s a nice trick to have up once’s sleeves.

No magic properties for arrays

PHP 5 brought us the magic methods : __get() and __set() that are called whenever a property is inaccessible (private or non-existent). This is a magic property : one may interact with it, while it doesn’t exist.

<?php 
class foo { 
  function __set($name, $value) { 
    $this->values[$name] = $value; 
  } 
} 

$bar = new foo(); 
$bar->a  = 33; // set 33
?>

One little known caveat is that array are not supported for writing. For example :

<?php 
  $bar = new foo(); 
  $bar->a[3] = 33;  
?>

Here, the array syntax [3] prevents __set() to be called. Instead, __get() is called. But __get() is used only to read value, and not to write it. This leads to this strange error :
“Indirect modification of overloaded property”
To solve this issue, one simply has to declare the property, avoiding the usage of magic for this property.

Graph database

The new drivers for the internal graph database getting stable and reliable. We are now testing it over a unique collection of 1600 open source projects. Several optimizations have been applied to speed up the loading and processing phase. More are being applied so as to make sure the engine is running and humming.

Happy PHP code reviews

Exakat 0.12.4 offers the first complete support for migration to PHP 7.2. PHP 7.2 is going to be a transition version, preparing code for the major deprecation of feature. Use it to make your code clean of old snippets and features.
All the 310+ analyzers are presented in the docs, including the PHP 7.0 ‘While (list() = each() ) ‘ (http://exakat.readthedocs.io/en/latest/Rules.html#while-list-each), which is PHP 3 behavior that should be replaced by a foreach(). Download Exakat on exakat.io, upgrade it with ‘exakat.phar upgrade -u’ and like us on github: https://github.com/exakat/exakat.

Tweet about this on TwitterShare on RedditShare on LinkedInEmail this to someone