Exakat 2.1.6 Review
Exakat 2.1.6 keeps the pace with PHP 8.0 development, and tops the count of 400 analysis. While we are waiting for the finalization of the attributes syntax, the first PHP 8.0 analysis is out : it checks for typos in named arguments. And, for the current PHP version, the Type suggestion report was improved both in type detection and report display. When you are trying to make your typehint coverage higher, get a boost with this report. Also, we ran into two portability problems, related to iconv and glob, while preparing an Alpine docker image, so they are now added here. Only dead code has seen the end of the Exakat 2.1.6 Review.
Type suggestion report
Exakat provides a special report : Type suggestion. It collects missing typehints in the code, and provides suggestions to fill the missing typehints.
Suggestions are applied to all types of methods : functions, methods, closures and arrow functions. We’ll use all denominations interchangeably in this blog.
Suggestions are inferred based on the usage of the method and its usage of the parameters. For example, when relaying a parameter to another function which requires an integer, the parameter is presumed to be also of the same type, int
. Or, when the parameter is used as an index in an array syntax, then, the parameter type suggested is string or an integer.
Types suggestions may be multiple : while union types, or multiple types are not supported until PHP 8.0, several suggestions may be worked out for one parameter or return type. All of them are offered in the report : that provides a better insight for choosing a typehint.
The type suggestion covers methods, both parameters and return type, and named function. We’ll add a mention of closures, stored as part of a method in a future version.
To produce this report, run the following command after an exakat audit :
php exakat.phar report -p <project name> -format Typesuggestion -v
The produced file will be available in the project folder, under the name ‘typehint.suggestion.html’;
More PHP 8.0 support
Exakat 2.1.6 added support for ‘Missing arguments’, when using named arguments. This happens when the name of the argument in the functioncall doesn’t exist in the function signature.
function foo($a) {}
foo(b: 1);
PHP emits a Fatal error at execution time, while Exakat finds the error at compile time. This helps prevent unwanted interruptions in the code.
New portability analysis
Two new portability problems were added to the Portability ruleset. One with glob(), and one with iconv().
iconv()
The TRANSLIT
conversion option is not available on every system implementations : this is the case for the alpine Linux distribution, for example. In that case, PHP stops with a fatal error.
glob()
glob() supports various options, as GLOB_BRACE
. This option allows brackets to be used as alternatives. {a,b,c}
is understood as a
, or b
, or c
. This option also is not supported on some systems, like Solaris, or Docker alpine.
Virtualmethod
A new atom was added to the AST : Virtualmethod
. This atom is a placeholder, and doesn’t have any representation in a PHP code, just like a Variabledefinition
for example. It is the definition of a method, inside a class, when no explicit definition is available. This is fixed later in the process of the AST, when the trait or the parent’s definition is identified.
Virtualmethod
allows for faster analysis, and serves as a bridge for trait aliases : use traitT { foo as bar; }
leads to the creation of an alias, without a definition. With the new Virtualmethod
atom, Exakat is able to handle those aliases.
The Weekly Audits: 2020, Week #37
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 : 2020, week 2020-37
- Static Methods Called From Object : Static methods may be called without instantiating an object.
- Property Could Be Local : A property only used in one method may be turned into a local variable.
- Compared Comparison : Usually, comparison are sufficient, and it is rare to have to compare the result of comparison.
- eval() Without Try :
eval()
emits aParseError
exception with PHP 7 and later. - Altering Foreach Without Reference : Foreach() loop that should use a reference.
Happy PHP Code Reviews
All the 402 analyzers are presented in the docs, including the noble : Large try blocks): Avoid using too large a try block. Keep it small, and focused on the actual call that may raise the exception.
This is a new rule, thanks to @afilina for the inspiration.
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.