Code quality is not just for Christmas, it is a daily part of the job. So, what do you do when you're handed with a five feet long pole a million lines of code that must be vetted? You call static analysis to the rescue. During one hour, we'll be reviewing totally unknown code: no name, no usage, not a clue. We'll apply a wide range of tools, reaching for anything that helps us understand the code and form an opinion on it. Can we break this mystery and learn how everyone else is looking at our code?
3. Review this code
We don't know what it does
We have never heard about it
We don't run it
We don't know the authors
Can we form an opinion?
4. How to review code
Reading code is humanly possible : its an art
Unit test are not adapted for review
Dynamic analysis is not fit for review
We need to explore code
We cannot only rely on the current state
29. Various AST
PHP7mar : nikic/php5-ast
PHAN : ext/ast (PHP 7 only)
Exakat : AST in a graph database
SonarQube : Java-build AST
PHPstorm : internal IDE AST
Better Reflection
30. PHAN
.../src/Module.php:26 PhanUndeclaredClassMethod Call to method getApplication from undeclared
class ZendMvcMvcEvent
Total : 7137 results / 41 types
4682 issues
.../melis-cms-page-historic/src/Module.php:131 PhanUndeclaredVariable Variable $sm is undeclared
475 issues
src/Controller/MelisCmsNewsController.php:940 PhanCommentParamWithoutRealParam Saw an @param
annotation for folderId, but it was not found in the param list of function createFolder($id) : bool
31 issues
...src/Controller/FrontPluginsController.php:246 PhanTypeMismatchForeach null passed to foreach
instead of array
...include/FtpClient.php:450 PhanParamTooMany Call with 1 arg(s) to FtpClient
FtpWrapper::delete() which only takes 0 arg(s) defined at
10 issues
39 issues
31. PHP 7 helps static analysis
Type hint, return type hint, scalar typehint
Usage of PHPDOC
Consistent behavior of PHP operators
Dynamic code is very difficult to analyze
32. PHP LINT
php -l <fichier.php>
Paralell executions
jakub-onderka/php-paralell-lint
Various versions of PHP : 7.0, 7.1, 7.2, 7.3, 5.6, 5.5
35. What does this app do?
Inventories of the application
Names for classes, methods, traits, variables,
interfaces…
List of literal in the code
Integers, real, arrays, strings
40. Directories 1010
Files 5167
Size
Lines of Code (LOC) 675844
Comment Lines of Code (CLOC) 197531 (29.23%)
Non-Comment Lines of Code (NCLOC) 478313 (70.77%)
Logical Lines of Code (LLOC) 136607 (20.21%)
Classes 120494 (88.20%)
Average Class Length 25
Minimum Class Length 0
Maximum Class Length 1380
Average Method Length 4
Minimum Method Length 0
Maximum Method Length 211
Functions 718 (0.53%)
Average Function Length 0
Not in classes or functions 15395 (11.27%)
Cyclomatic Complexity
Average Complexity per LLOC 0.31
Average Complexity per Class 9.50
Minimum Class Complexity 1.00
Maximum Class Complexity 292.00
Average Complexity per Method 2.57
Minimum Method Complexity 1.00
Maximum Method Complexity 165.00
PHPLOC