Review unknown code with static analysis
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 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
30. 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
31. PHAN
protected/views/book/admin.php:7 PhanUndeclaredVariable Variable $this is undeclared
Total : 5194 results / 36 types
2967 issues
/protected/views/configurations/_form.php:22
PhanTypeMismatchArgument Argument 1 (pk) is int but EmployeeAttendances::findByPk() takes array|null
defined at core/yiilite.php:7230
475 issues
core/utils/CMarkdownParser.php:81 PhanUndeclaredClassMethod Call to method purify from undeclared class HTMLPurifier
core/utils/CMarkdownParser.php:99 PhanUndeclaredClassMethod Call to method outdent from undeclared class MarkdownExtra_Parse
core/caching/CMemCache.php:111 PhanUndeclaredClassMethod Call to method __construct from undeclared class Memcache
323 issues
protected/controllers/LeadsController.php:352 PhanUndeclaredConstant Reference to undeclared constant MENU_TITLE
protected/controllers/LeadsController.php:353 PhanUndeclaredConstant Reference to undeclared constant MENU_URL
protected/controllers/LeadsController.php:354 PhanUndeclaredConstant Reference to undeclared constant MENU_CLASS
protected/modules/courses/views/courses/left_side.php:49 PhanRedefineFunction Function t
protected/modules/courses/views/studentAttentance/attentstud.php:30 PhanRedefineFunction Function getweek
166 issues
32 issues
32. 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
33. PHP LINT
php -l <fichier.php>
Paralell executions
jakub-onderka/php-paralell-lint
Various versions of PHP : 7.0, 7.1, 7.2, 5.6, 5.5
34. Checked 2795 files in 16.4 seconds
Syntax error found in 1 file
------------------------------------------------------------
Parse error: protected/vendors/MPDF/mpdf.php:1417
1415| case 'DEMY': {$format=array(382.68,612.28 ); break;} // 'Demy'
1416| case 'ROYAL': {$format=array(433.70,663.30 ); break;} // 'Royal' f
> 1417| default: $format = false;
1418| }
1419| return $format;
Fatal error: Switch statements may only contain one default clause
PHP LINT - 7.0/1
35. PHP LINT - 5.4/5/6 - 7.2
No syntax error found ??
37. 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
42. Directories 1143
Files 5982
Size
Lines of Code (LOC) 835199
Comment Lines of Code (CLOC) 252075 (30.18%)
Non-Comment Lines of Code (NCLOC) 583124 (69.82%)
Logical Lines of Code (LLOC) 195283 (23.38%)
Classes 178062 (91.18%)
Average Class Length 29
Minimum Class Length 0
Maximum Class Length 3141
Average Method Length 4
Minimum Method Length 0
Maximum Method Length 879
Functions 1477 (0.76%)
Average Function Length 1
Not in classes or functions 15744 (8.06%)
Cyclomatic Complexity
Average Complexity per LLOC 0.30
Average Complexity per Class 10.82
Minimum Class Complexity 1.00
Maximum Class Complexity 1177.00
Average Complexity per Method 2.65
Minimum Method Complexity 1.00
Maximum Method Complexity 387.00
PHPLOC
45. Going even further
Dynamic code
40% of the code is actually constant
Taint analysis
Transpilage : https://github.com/jaytaph/Transphpile
PHP inspections : Integrated in phpStorm
Integrate static analysis in pipeline
46. List of PHP analyzers
Exakat
Phan
Phploc
PHP 7 cc
PHPmetrics
https://github.com/exakat/
php-static-analysis-tools
47. Large application
One framework (Yii)
many old libraries (MPDF, PEAR)
Low level of issues
Tend to favorite old PHP features
Backward compatibilities
Deal with school : students, books, elections