Exakat 0.12.0

Exakat 0.12.0 is now out, after a rough publication day. The main work on 0.12 is the new support for Janusgraph that has started. This is still quite hidden, but you’ll feel the effect soon. Also, we got two nice contributions: the non-breakable spaces classes, from Matthieu Napoli, and the optional property, via David Negrier. Then we added support for ext/swoole, class declaration orders and Heredoc delimiter favourites.

Exakat @ Dutch PHP Conference

This week, we’ll be at the exciting DPC: Dutch PHP Conference, in Amsterdam. Exakat is one of the projects of the Coding Night, where I’ll be explaining how to develop Exakat analysis and its internal gears. Believe me, this is going to be a first for us. Then, I’ll be on the premise during the whole conference, taking elephpant family pictures and spreading static analysis and reviewing code. Don’t miss it!

Exakat 0.12.0

We skipped one minor version and started the heavy work on Janusgraph. Up to now, Exakat has only been using Gremlin 3 and Neo4j for the backend database. Gremlin is the graph traversal language we use to query PHP code, and it is supported by a variety of databases. We plan to simplify the installation phase and gain in terms of performances for processing.

The roadmap include writing the new driver to connect to the database, tuning the configuration, checking UT and performances, and releasing the 1.0 with Janusgraph. During this time, Neo4j/Gremlin3 is still supported and will get the same upgrades.

Non-breakable spaces in classes names

Matthieu Napoli uses non-breakable spaces in classes names to make them more readable. Non-breakable spaces behave like   in HTML, and keep the word going. PHP supports this notation, as the non-breakable space falls within the range of acceptable characters.

As Matthieu mentions in his blog post the main limitation for the usage of non-breakable spaces are probably human: at first, it looks like impossible code! Then, of course, non-breakable spaces have to be typed with a key combination, that makes it less convenient than the giant key at the bottom of our keyboards.

Anyhow, non-breakable spaces are now reported by Exakat, so as to not be surprised by them when you reach them in the code.

<?php
// Example adapted from Matthieu Napoli's blog
class foo {
    // Non-breakable spaces are ... invisible
    public function test a user can add a product to a wishlist() {
        // ...
    }
    }
?>

Avoid optional properties

Thanks to a tweet from David Negrier, of packagist fame, I started reading the Best practices from The Coding Machine. The company where David is CTO has a short list of recommendations on how to write beautiful code. It covers the workstation, coding conventions and programming practices. The whole list is very refreshing to read, as I read for the first time a surprising number of them.
Avoid optional services as much as possible‘ is now made into an Exakat analysis. It reports any property that may end up being null, compelling the code to check the mere existence of the property at each usage. This is a little different from the original notion of ‘service’ but I don’t have the notion of ‘service’ in the engine.

Order of declaration in a class

There are rare cases where the order of declaration inside a class is enforced by coding conventions.
Classes actually behave like a huge declaration list, with use traits, const constant, private $property, and function method(); PHP compiles this whole list first, and then tries to execute it when needed later. This means that the order of declaration is completely arbitrary inside the class.

<?php
class foo {
    function foo($a = self::A) { // self::A is defined later
        echo $a;
    }
    const A = self::B + 1; // self::B is defined later
    const B = 3;    
}
?>

Side note: the function world behaves very differently than the class world.

<?php
    function foo($a = A) {
        echo $a;
    }
    const A = B + 1; // Undefined B
    const B = 3;
?>

So, classes are a long list of definitions that get compiled early. There are 4 types of declarations and quite a large variety of behaviour. I found in the Symfony coding standards, two mentions:

  • Declare class properties before methods;
    Declare public methods first, then protected ones and finally private ones.[…]

Since I got burned recently by someone who buried a property and a constant deep between two methods, I though it would be interesting to see where that leads. This gave me this analysis that reports class which use another order than the following:

  • traits
  • constants
  • properties
  • methods

 

Access modifiers and options are not used here. Surely, sorting the methods/properties by visibility may help readability, although it ends up splitting the public properties and public methods.

<?php
class foo {
	public $x;
	protected $y = 1;   // protected and private are now in the way between public declarations.
	public function bar() {}
	protected function bar2() {}
}
?>

Another order that I considered are the special methods: constructor and destructor usually get the two first positions in the class; Then PHP has another long list of magic methods, that are important to know about: __get, __set, __wakeup, __sleep, __clone, etc.. Those should probably get a higher rank before specific methods.

In the end, I kept this analysis light: I still want to spot declarations that are mixed to the risk of errors. Sorting methods and properties is not a goal in coding.

Ext/swoole

We added a new extension to the list of recognized PHP extension : swoole. It is a Event-driven asynchronous & concurrent & coroutine networking engine with high performance for PHP. http://www.swoole.com/ [中文] and https://github.com/swoole/swoole-src [en]. It’s good to see the Chinese community emerging!

Happy PHP code reviews

Exakat 0.12.0 is the first step of the final journey toward Exakat 1.0. Many thanks to Matthieu and David for their inspirational work! We are always reading the Internet to find new idea to turn into an Exakat analysis. If you have published your best practices, like the coding machine, feel free to send them to us! We’d like to have reference list of them, so everyone may learn and get better at PHP.

All the 320+ analyzers are presented in the docs, including the academic ‘Must have return Methods‘ which spot methods that MUST return something. They are mostly PHP magical methods.

Download Exakat on exakat.io, upgrade it with ‘exakat.phar upgrade -u’ and like us on github: https://github.com/exakat/exakat.