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 project
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 have 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
8. 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
9. Checked 5982 files in 28.4 seconds
Syntax error found in 4 files
------------------------------------------------------------
Parse error: /vendor2/symfony/symfony/src/Symfony/Component/Validator/Constraints/False.php:22
20| * @api
21| */
> 22| class False extends IsFalse
23| {
24| }
Fatal error: Cannot use 'False' as class name as it is reserved
------------------------------------------------------------
Parse error: /vendor2/symfony/symfony/src/Symfony/Component/Validator/Constraints/Null.php:22
20| * @api
21| */
> 22| class Null extends IsNull
23| {
24| }
Fatal error: Cannot use 'Null' as class name as it is reserved
------------------------------------------------------------
Parse error: /vendor2/symfony/symfony/src/Symfony/Component/Validator/Constraints/True.php:22
20| * @api
21| */
> 22| class True extends IsTrue
23| {
24| }
Fatal error: Cannot use 'True' as class name as it is reserved
------------------------------------------------------------
Parse error: /vendor_user/windid_client/src/windid/service/base/WindidUtility.php:93
91| $imageInfo = @getimagesize($file);
92| $exts = array('1'=>'gif', '2'=>'jpg', '3'=>'png');
> 93| if (!isset($exts[$imageInfo[2]])) continue;
94| $ext = $exts[$imageInfo[2]];
95| $filename = rand(1000,9999). '.'.$ext;
Fatal error: 'continue' not in the 'loop' or 'switch' context
PHP LINT - 7.0/1/2
10. Checked 5982 files in 29.7 seconds
Syntax error found in 1 file
------------------------------------------------------------
Parse error: /vendor2/mockery/mockery/tests/Mockery/MockingVariadicArgumentsTest.php
50| abstract class TestWithVariadicArguments
51| {
> 52| public function foo(...$bar)
53| {
54| return $bar;
Unexpected '.', expecting '&' or variable (T_VARIABLE)
PHP LINT - 5.5
PHP LINT - 5.6
Checked 5982 files in 31 seconds
No syntax error found
11. PHP LINT
Not compatible with PHP 7.0 +
Not compatible with PHP 5.5-
Uses Symfony
@getimagesize ? vendor2 ?
5982 files
13. PHPLOCDirectories 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
[...]
17. Automated code review
PHP code review PHP code
regex reads PHP
Extract interesting points
Works with keywords
PHP7cc, grep
18. php7cc
File: /vendor_user/windid_client/wind/convert/WindGeneralConverter.php
> Line 33: PHP 4 constructors are now deprecated
public function WindGeneralConverter($sourceLang = '', $targetLang = '', $forceTable = false)
{
}
File: /vendor2/symfony/symfony/src/Symfony/Component/Validator/Constraints/Null.php
> Line 22: Reserved name "null" used as a class, interface or trait name
class Null extends SymfonyComponentValidatorConstraintsIsNull
{
}
File: /vendor_user/windid_client/wind/filter/WindHandlerInterceptorChain.php
> Line 61: Function argument(s) returned by "func_get_args" might have been modified
func_get_args();
File: /vendor_user/windid_client/wind/http/session/handler/WindSessionHandler.php
> Line 156: Check that callbacks that are passed to "session_set_save_handler" and return false or -1 (if any) operate
correctly
session_set_save_handler(array($this, 'open'), array($this, 'close'), array($this, 'read'), array($this, 'write'),
array($this, 'destroy'), array($this, 'gc'));
File: /vendor_user/windid_client/wind/security/WindMcryptCbc.php
> Line 31: Removed function "mcrypt_cbc" called
mcrypt_cbc(MCRYPT_DES, $key, $string, MCRYPT_ENCRYPT, $iv);
> Line 49: Removed function "mcrypt_cbc" called
mcrypt_cbc(MCRYPT_DES, $key, $string, MCRYPT_DECRYPT, $iv);
Total : 83 issues
22. Semantics and definitions
PHP7mar : nikic/php5-ast
PHAN : ext/ast (PHP 7 only)
Exakat : AST in a graph database
SonarQube : Java-build AST
PHPstorm : internal IDE AST
23. Semantics and definitions
Removes spaces, comments, documentations
Removes delimiters
( ) { } [ ] " ' ` ; :
Good network to link definition with usage
24. PHAN
src/Org/OrgBundle/Controller/OrgController.php:12
PhanTypeMismatchArgument Argument 1 (data) is bool but OrgOrgBundleControllerOrgController::createJsonResponse() takes
array defined at src/Topxia/WebBundle/Controller/BaseController.php:120
Total : 13315 results
1235 issues
vendor_user/windid_client/wind/mail/protocol/WindPop3.php:186
PhanUndeclaredTypeParameter Parameter of undeclared type baoolean
276 issues
vendor_user/windid_client/wind/base/WindFactory.php:325
PhanTypeArraySuspicious Suspicious array access to bool
184 issues
vendor2/imagine/imagine/lib/Imagine/Image/AbstractLayers.php:49
PhanParamSignatureMismatch Declaration of function get($offset) should be compatible with function get(int $offset) :
ImagineImageImageInterface defined in vendor2/imagine/imagine/lib/Imagine/Image/LayersInterface.php:97
src/Classroom/ClassroomBundle/Controller/ClassroomAdminController.php:84
PhanUndeclaredMethod Call to undeclared method ClassroomClassroomBundleController
ClassroomAdminController::createErrorResponse
1919 issues
808 issues
39. What does this app do?
Welcome to the inventories
List of literal in the code
Integers, real, arrays, strings
Names for classes, methods, traits, variables,
interfaces…
45. Going even further
Dynamic code
40% of the code is actually constant
Transpilage : https://github.com/jaytaph/Transphpile
PHP inspections : Integrated in phpStorm
sensio labs insight : Static analysis for Symfony
Integrate static analysis in IC
46. Liste des SCAP cités
Deptrac
Exakat
PHP7mar
Phan
PHP Inspections
Phploc
PHPMD
PHP 7 cc
PHPmetrics
RIPS
Transphpile