SlideShare a Scribd company logo
Zend Con, Las Vegas, 2017
Code review
U n k n o w n
Code Base
Agenda
Reviewing code
Static analysis for PHP
A session in which you are the hero
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?
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
Speaker
Damien Seguy
Exakat CTO
Static analysis for PHP
PHP doc author
Retirement home for elephants
Source code is structured
Source code is a structured database
All we need is tools to query it
This is static analysis
Migration PHP 7.0->7.2
IncompatibilitiesNewfeatures
Appinfo()
List PHP features
Focus on PHP's specifics
PHP Features
Review unknown code with static analysis  Zend con 2017
Review unknown code with static analysis  Zend con 2017
Review unknown code with static analysis  Zend con 2017
Review unknown code with static analysis  Zend con 2017
Application favorites
Many solutions to the same problem
Impact on PHP is minimal
Generate never-ending discussions
Rule : choose one, stick to it
Review unknown code with static analysis  Zend con 2017
Review unknown code with static analysis  Zend con 2017
Automated code review
Analyze code
Report PHP related problems
Review unknown code with static analysis  Zend con 2017
Clean code for PHP
Best practices
Security, performance, clean code
in-house, PSR, calisthenics, other inspirations
Code mantras, code kata
PHP Manual
Migration guides
Results by files
Exakat : 350 analysis
Analysis Freq. Here
function foo($a, $a, $a) {} 2.0% 0
!!(expression) 2.0% 3
substr($a, 2, 4) == 'abc' 9.0% 0
$a ? $b ? $c : $d : $e 11% 0
foreach($a as &$b) {} 15% 0
$var + 0 31% 46
if (strpos($a, $b)) {} 46% 18
include('file.php') 74% 284
Zend Framework 3
-/ … /-
-/ … /-
Automated code review
Semantic read of the code
Reports interesting issues
Works with AST
Automated code review
PHP 5 / 7
Calisthenics
ClearPHP
Performance
Framework
 
 

Semantics and definitions
Removes spaces, comments, documentations
Removes delimiters
( ) { } [ ] " ' ` ; :
Good network to link definition with usage
AST diagram
<?php
    $x = source();
    
    if ($x < 10) {
        $y = $x + 1;
        $a = 3;
        $x = corrige($y);
    } else {
        $y = $x;
    }
Flow Control Graph
<?php
    $x = source();
    
    if ($x < 10) {
        $y = $x + 1;
        $a = 3;
        $x = corrige($y);
    } else {
        $y = $x;
    }
$x = source;
if ($x < 10)
$y = $x;
$y = $x + 1;
$x = corrige($y);
end
$a = 3;
start
Data Dependency Graph
<?php
    $x = source();
    
    if ($x < 10) {
        $y = $x + 1;
        $a = 3;
        $x = corrige($y);
    } else {
        $y = $x;
    }
$x = source;
if ($x < 10) $y = $x;$y = $x + 1;
$x = corrige($y);
fin();
Depends onDepends on
Depends
on notDepends on
Depends on
$a = 3;
Depends on
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
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
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
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
Checked 1402 files in 21.3 seconds
Syntax error found in 1 file
-------------------------------------------------
Parse error: melisplatform/melis-installer/etc/
MelisModuleConfig/app.interface.php:59
57| ),
58| ),
> 59| [:environment_configurations]
60| ),
61| ),
Parse error: parse error, expecting `']''
PHP LINT - 5.4 -> 7.3-dev
0
1.25
2.5
3.75
5
5.2 5.3 5.4 5.5 5.6 7.0 7.1 7.2 7.3
0
1.25
2.5
3.75
5
5.2 5.3 5.4 5.5 5.6 7.0 7.1 7.2 7.3
0
0.75
1.5
2.25
3
5.2 5.3 5.4 5.5 5.6 7.0 7.1 7.2 7.3
0
1.75
3.5
5.25
7
5.2 5.3 5.4 5.5 5.6 7.0 7.1 7.2 7.3
5.2 5.3 5.4 5.5 5.6 7.0 7.1 7.2
5.2 5.3 5.4 5.5 5.6 7.0 7.1 7.2 5.2 5.3 5.4 5.5 5.6 7.0 7.1 7.2
5.2 5.3 5.4 5.5 5.6 7.0 7.1 7.2
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
Errors messages
Classes
UserController => 1
UserProfileController => 1
Utf8 => 1
Utf8Num => 1
WidgetsController => 1
Wildcard => 1
Writer => 1
Xlsx => 1
imageLib => 1
Boolean => 2
CalendarController => 2
Filesystem => 2
Fuzzy => 2
InvalidArgumentException => 2
LanguageController => 2
MelisCmsSlider => 2
MelisCoreConfigServiceInterface => 2
MelisFieldCollection => 2
MelisFieldRow => 2
MelisLog => 2
MelisPasswordValidator => 2
MelisPlatform => 2
MelisPlatformTable => 2
MelisSelectFactory => 2
MelisTextFactory => 2
MultiTerm => 2
IndexController => 3
MelisGenericTable => 3
CaseInsensitive => 4
ExceptionInterface => 4
Phrase => 4
Term => 5
DashboardController => 6
Module => 18
Variables
[$result] => 176
[$postValues] => 178
[$name] => 182
[$parameters] => 183
[$index] => 190
[$status] => 191
[$arrayParameters] => 197
[$melisTool] => 206
[$file] => 211
[$sl] => 214
[$textMessage] => 219
[$config] => 237
[$menu] => 245
[$results] => 247
[$translator] => 260
[$errors] => 261
[$idPage] => 268
[$val] => 288
[$value] => 302
[$melisKey] => 307
[$response] => 308
[$select] => 311
[$form] => 316
[$success] => 341
[$data] => 431
[$container] => 574
[$e] => 582
[$id] => 676
[$view] => 831
[$element] => 1172
Also : 

91 used-once variables

40 used-once properties
Metrics
Provides measures values about the code
Cyclomatic complexity, LOC, Maintenance index
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
PHPMetrics
Review unknown code with static analysis  Zend con 2017
List of PHP analyzers
Exakat
Phan
Phploc
PHPmetrics
https://github.com/exakat/
php-static-analysis-tools
Large application
Main framework : Zend Framework 3
other component : imageLib, ZF2..
Low level of issues, up to date
Generic platform
Melis platform
www.melistechnology.com
Thanks http://exakat.io/ - @exakat

More Related Content

PDF
Review unknown code with static analysis
PPS
PHP Security
KEY
PHP security audits
PDF
PDF
Php Security
PPT
PHP Security
PPT
Zend Framework
PPTX
Creating "Secure" PHP Applications, Part 1, Explicit Code & QA
Review unknown code with static analysis
PHP Security
PHP security audits
Php Security
PHP Security
Zend Framework
Creating "Secure" PHP Applications, Part 1, Explicit Code & QA

What's hot (19)

KEY
Zend Framework Study@Tokyo vol1
PPT
Php with my sql
PPT
Open Source Package PHP & MySQL
PPT
PHP POWERPOINT SLIDES
ODP
Concern of Web Application Security
PPT
Introducation to php for beginners
PPTX
PHP FUNCTIONS
ODP
The why and how of moving to PHP 5.4/5.5
PPTX
New in php 7
PDF
Preparing for the next PHP version (5.6)
PPS
Php security3895
PPS
Php Security3895
PDF
PHP7 - Scalar Type Hints & Return Types
PPSX
Php and MySQL
PPT
Synapseindia php development tutorial
KEY
Workshop quality assurance for php projects tek12
PDF
Quality Assurance for PHP projects - ZendCon 2012
PDF
PHPUnit Episode iv.iii: Return of the tests
PPT
P H P Part I I, By Kian
Zend Framework Study@Tokyo vol1
Php with my sql
Open Source Package PHP & MySQL
PHP POWERPOINT SLIDES
Concern of Web Application Security
Introducation to php for beginners
PHP FUNCTIONS
The why and how of moving to PHP 5.4/5.5
New in php 7
Preparing for the next PHP version (5.6)
Php security3895
Php Security3895
PHP7 - Scalar Type Hints & Return Types
Php and MySQL
Synapseindia php development tutorial
Workshop quality assurance for php projects tek12
Quality Assurance for PHP projects - ZendCon 2012
PHPUnit Episode iv.iii: Return of the tests
P H P Part I I, By Kian
Ad

Similar to Review unknown code with static analysis Zend con 2017 (20)

PDF
Review unknown code with static analysis php ipc 2018
PDF
Review unknown code with static analysis php ce 2018
PDF
Preparing code for Php 7 workshop
PDF
PHP Static Code Review
PDF
PHPcon Poland - Static Analysis of PHP Code – How the Heck did I write so man...
PDF
20 PHP Static Analysis and Documentation Generators #burningkeyboards
PDF
Php 7 compliance workshop singapore
PDF
Review unknown code with static analysis - bredaphp
PDF
Automated code audits
PDF
Static analysis saved my code tonight
PPTX
Listen afup 2010
PDF
Php 7.2 compliance workshop php benelux
PDF
Last train to php 7
PDF
Php Inspections (EA Extended): if-conditions optimization
PDF
Static analysis saved my code tonight
PDF
Static Analysis of PHP Code – IPC Berlin 2016
PDF
Code review for busy people
PDF
PHP7 - The New Engine for old good train
PDF
PHP Internals and Virtual Machine
PDF
Code review workshop
Review unknown code with static analysis php ipc 2018
Review unknown code with static analysis php ce 2018
Preparing code for Php 7 workshop
PHP Static Code Review
PHPcon Poland - Static Analysis of PHP Code – How the Heck did I write so man...
20 PHP Static Analysis and Documentation Generators #burningkeyboards
Php 7 compliance workshop singapore
Review unknown code with static analysis - bredaphp
Automated code audits
Static analysis saved my code tonight
Listen afup 2010
Php 7.2 compliance workshop php benelux
Last train to php 7
Php Inspections (EA Extended): if-conditions optimization
Static analysis saved my code tonight
Static Analysis of PHP Code – IPC Berlin 2016
Code review for busy people
PHP7 - The New Engine for old good train
PHP Internals and Virtual Machine
Code review workshop
Ad

More from Damien Seguy (20)

PDF
Strong typing @ php leeds
PPTX
Strong typing : adoption, adaptation and organisation
PDF
Qui a laissé son mot de passe dans le code
PDF
Analyse statique et applications
PDF
Top 10 pieges php afup limoges
PDF
Top 10 php classic traps DPC 2020
PDF
Meilleur du typage fort (AFUP Day, 2020)
PDF
Top 10 php classic traps confoo
PDF
Tout pour se préparer à PHP 7.4
PDF
Top 10 php classic traps php serbia
PDF
Top 10 php classic traps
PDF
Top 10 chausse trappes
PDF
Understanding static analysis php amsterdam 2018
PDF
Everything new with PHP 7.3
PDF
Php 7.3 et ses RFC (AFUP Toulouse)
PDF
Tout sur PHP 7.3 et ses RFC
PDF
Machine learning in php las vegas
PDF
Php in the graph (Gremlin 3)
PDF
Hunt for dead code
PDF
A la recherche du code mort
Strong typing @ php leeds
Strong typing : adoption, adaptation and organisation
Qui a laissé son mot de passe dans le code
Analyse statique et applications
Top 10 pieges php afup limoges
Top 10 php classic traps DPC 2020
Meilleur du typage fort (AFUP Day, 2020)
Top 10 php classic traps confoo
Tout pour se préparer à PHP 7.4
Top 10 php classic traps php serbia
Top 10 php classic traps
Top 10 chausse trappes
Understanding static analysis php amsterdam 2018
Everything new with PHP 7.3
Php 7.3 et ses RFC (AFUP Toulouse)
Tout sur PHP 7.3 et ses RFC
Machine learning in php las vegas
Php in the graph (Gremlin 3)
Hunt for dead code
A la recherche du code mort

Recently uploaded (20)

PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Getting Started with Data Integration: FME Form 101
PDF
Approach and Philosophy of On baking technology
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
cuic standard and advanced reporting.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Big Data Technologies - Introduction.pptx
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
1. Introduction to Computer Programming.pptx
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PPTX
Tartificialntelligence_presentation.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Unlocking AI with Model Context Protocol (MCP)
Dropbox Q2 2025 Financial Results & Investor Presentation
Getting Started with Data Integration: FME Form 101
Approach and Philosophy of On baking technology
Network Security Unit 5.pdf for BCA BBA.
Mobile App Security Testing_ A Comprehensive Guide.pdf
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Advanced methodologies resolving dimensionality complications for autism neur...
cuic standard and advanced reporting.pdf
Programs and apps: productivity, graphics, security and other tools
Big Data Technologies - Introduction.pptx
A comparative analysis of optical character recognition models for extracting...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
1. Introduction to Computer Programming.pptx
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Tartificialntelligence_presentation.pptx
MIND Revenue Release Quarter 2 2025 Press Release

Review unknown code with static analysis Zend con 2017