This week, Exakat added a new inventory for too many chained object. The engine upgraded the storage of Typehint and Default values, which lead to several modernisation and coverage extensions, such as ‘typehint are for interface or abstract classes’, and ‘Never used parameters’. 

Code is like a series of baby steps, and a good Exakat 1.9.7 review.

Too Many Chained Objects

Chaining objects is a classic sight in any PHP code. To reach a resource across the code, one need to call several methods : 

Main::get('root')->getUsers($id)->getAddress(self::Current)::$zip_code->format();

Exakat 1.9.7 builds an inventory of objects dereferencing depths. This gives an interesting overview on the application itself.

Then, a new analysis was introduced : Max deferencing. It reports any expression that is chaining too many methods and properties. By default, a maximum of 7 dereferencing is set, which may be set to other levels, stricter or not.

Dereferencing levels charts

In particular, the first reports showed that such writing style is quite popular, and may lead to really high chaining : chains 35 to 65 calls are possible. This is due to fluent interfaces, in particular for queries. 

We’ll work on detecting those fluent interfaces, and distinguish them from the other chaining call in a next version.

TYPEHINT and DEFAULT values

Until now, Exakat represented typehint and default values as they appear in PHP : an option. They may be available, or not. This lead to extra manipulations, to handle the situations where those values are not available. 

With the evolution of PHP, those options are more and more present. To speed up the process, typehints and default values are always available, though they may use the ‘Void’ object, making them point to nothing. It acts as a Null Pattern : supporting the syntax, but doing about nothing.

We’ll extend the coverage to properties, just like in PHP 7.4. Until the code itself start using the new typed properties, Exakat will fill in the blanks, detecting automatically the type of the property, and checking its related usage.

No class as typehint

Typehints on methods should always use interface : the relation between a method and its calling context should be based on a contract, more than a concrete class. 

Interfaces do not allow specifications of properties, only methods and constants. And yet, 45% of PHP applications use public properties from an argument inside a method. 

To keep close to the letter of the law, abstract classes are the closest structure we can use to both specify methods and properties, while typehinting with a contract and not a concrete class. 

This has been updated in Exakat 1.9.7.

Never Used Parameter

‘Never used parameter’ is a rule which reports parameters which are never used to call a function. The

<?php

function foo($a, $b = 2) {
    // both argument are really used
    return $a + $b;
}

// Imagine this scattered across the code

foo(1);
foo(10);
foo(-2);
foo($i);
foo($a->c);

?>

Function foo() was defined with two arguments, $a and $b. $b has a default value, and may be skipped when called. While the definition of the second argument may have made sense at the function definition, it appears that the function usage always skips this argument, and rely only on its default value. As such, the argument is never used.

What to do with such argument ? As usual, it may be useful to keep it, for future use. Or, one may consider that an unused parameter is also dead code : remove it, and hard code it in the foo() function until it is actually useful. There is no need for more complexity.

This analysis was introduced in Exakat 1.8.3 : it now also covers method calls, statics or normal. 

The Weekly Audits: 2019, Week #40

Exakat includes a ‘weekly’ report: this report is built with a selection of five analyses. This means a short audit report, with few issues to review. This is not a lot to read them, and review them in your code. Everyone in the PHP community can focus on one of the classic coding problems and fix it. Talk about the weekly audit around you: you’ll find programmers facing the same challenges.

To obtain the ‘weekly’ audit, run an audit, and request the ‘Weekly’ report.

# Init the project (skip when it is already done)    
php exakat.phar init -p <yourproject> -R https://github.com/Seldaek/monolog.git -git 

# Run the project (skip when it is already done)    
php exakat.phar project -p <yourproject> 

# Export the weekly project (every Monday)    
php exakat.phar report -p <yourproject> -format Weekly 

# Open projects/<yourproject>/weekly/index.html in your browser    

Every week, you can find here 5 new analysis to review in your code. In fact, when your code is clean, you can also take a quick look at the upcoming analysis. 

Weekly recommendations for PHP code review : 2019, week 2019-40

Happy PHP Code Reviews 

All the 386 analyzers are presented in the docs, including the mobile : Repeated print(): Always merge several print or echo in one call.

This is an common bug, with more than 49% of chance to appear. 

You can check all of the Exakat reports at the gallery: exakat gallery.

Download Exakat on exakat.io, install it with Docker, upgrade it with ‘exakat.phar upgrade -u’ and like us on github.