SlideShare a Scribd company logo
REVIEW
UNKNOWN CODE
WITH
STATIC ANALYSIS
Amersfoort, Netherlands, June 2017
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
Ik spreek geen nederlandse
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.1
IncompatibilitiesNewfeatures
Appinfo()
List PHP features
Focus on PHP's specifics
PHP Features
Review unknown code with static analysis
Review unknown code with static analysis
Review unknown code with static analysis
Review unknown code with static analysis
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
List of 

directives
Automated code review
Analyze code
Report PHP related problems
Review unknown code with static analysis
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 __destruct() { throw …} :
0,3 % 0
0.3% 0
function foo($a, $a, $a) {} 2.0% 0
!!(expression) 1.0% 1
substr($a, 2, 4) == 'abc' 9.0% 0
$a ? $b ? $c : $d : $e 11% 0
foreach($a as &$b) {} 15% 6
if (strpos($a, $b)) {} 55% 15
include('file.php') 75% 263
PHP 7.0 compatibility
List of 

directives
Automated code review
Semantic read of the code
Reports interesting issues
Works with AST
Automated code review
PHP 5 / 7
Calisthenics
ClearPHP
Performance
 
 

Semantics and definitions
Removes spaces, comments, documentations
Removes delimiters
( ) { } [ ] " ' ` ; :
Good network to link definition with usage
AST
<?php
class Foo {
    function bar($arg) {
        return StrToUpper($arg + 2);
    }
}
$foo = new Foo();
$foo->bar(__FILE__);
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
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
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, 5.6, 5.5
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
PHP LINT - 5.4/5/6 - 7.2
No syntax error found ??
0
1.25
2.5
3.75
5
5.2 5.3 5.4 5.5 5.6 7.0 7.1 7.2
0
1
2
3
4
5.2 5.3 5.4 5.5 5.6 7.0 7.1 7.2
0
0.75
1.5
2.25
3
5.2 5.3 5.4 5.5 5.6 7.0 7.1 7.2
0
1.75
3.5
5.25
7
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
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
[EmployeesController] => 1
[ProductSearchController] => 1
[Services_Twilio_RestException] => 1
[RegisterForm] => 1
[BorrowBook] => 1
[Floor] => 1
[MessageController] => 1
[RInstaller] => 1
[AuthItemForm] => 1
[EmployeesSubjects] => 1
[Services_Twilio_UsageResource] => 1
[EmployeeAttendancesController] => 1
[Logo] => 1
[StudentAdditionalFields] => 1
[AttendanceModule] => 1
[DefaultsubjectsController] => 1
[EmployeeDepartments] => 1
[LogoutController] => 1
[EventsController] => 1
[SmsSettings] => 1
[UserChangePassword] => 1
[ElectiveGroupsController] => 2
[ElectivesController] => 2
[UserController] => 2
[Settings] => 2
[CKEditor] => 2
[CoursesController] => 2
[User] => 2
[UserIdentity] => 3
[SavedsearchesController] => 3
[StudentAttentanceController] => 3
[PDF] => 5
[Message] => 5
[DefaultController] => 22
Variables
[$config] => 288
[$command] => 303
[$proc] => 324
[$c] => 336
[$url] => 337
[$index] => 352
[$type] => 352
[$file] => 361
[$token] => 417
[$time] => 443
[$column] => 474
[$options] => 475
[$className] => 489
[$sql] => 519
[$log] => 544
[$entry] => 575
[$message] => 584
[$attribute] => 587
[$key] => 676
[$params] => 711
[$table] => 858
[$htmlOptions] => 885
[$i] => 1123
[$id] => 1228
[$value] => 1596
[$criteria] => 2072
[$name] => 2250
[$data] => 2368
[$form] => 3393
[$model] => 7075
Also : 

684 used-once variables
Metrics
Provides measures values about the code
Cyclomatic complexity, LOC, Maintenance index
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
PHPMetrics
PHPMetrics
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
List of PHP analyzers
Exakat
Phan
Phploc
PHP 7 cc
PHPmetrics
https://github.com/exakat/
php-static-analysis-tools
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
open-school.org
Bedankt
http://exakat.io/ - @exakat

More Related Content

PDF
Review unknown code with static analysis Zend con 2017
PDF
The Joy of Smartmatch
PDF
Perl6 in-production
PDF
Perl6 grammars
PPT
PHP POWERPOINT SLIDES
PPT
Open Source Package PHP & MySQL
KEY
PHP security audits
PPSX
Php and MySQL
Review unknown code with static analysis Zend con 2017
The Joy of Smartmatch
Perl6 in-production
Perl6 grammars
PHP POWERPOINT SLIDES
Open Source Package PHP & MySQL
PHP security audits
Php and MySQL

What's hot (20)

PPT
PHP - Introduction to PHP Functions
PDF
PHP 7 – What changed internally? (PHP Barcelona 2015)
PPT
Php Lecture Notes
ODP
What's new, what's hot in PHP 5.3
PPT
Building Data Mapper PHP5
PDF
Overview changes in PHP 5.4
PDF
Web Development Course: PHP lecture 1
ODP
The why and how of moving to PHP 5.4/5.5
PPT
PHP MySQL Workshop - facehook
PPS
PHP Security
PDF
07 Introduction to PHP #burningkeyboards
PDF
Zend Certification PHP 5 Sample Questions
PDF
Old Oracle Versions
PPT
Php Crash Course
PDF
Preparing for the next PHP version (5.6)
PPT
Php mysql
PPT
Php MySql For Beginners
PDF
PHP 7 – What changed internally?
PPT
Introduction To PHP
ODP
Back to basics - PHP_Codesniffer
PHP - Introduction to PHP Functions
PHP 7 – What changed internally? (PHP Barcelona 2015)
Php Lecture Notes
What's new, what's hot in PHP 5.3
Building Data Mapper PHP5
Overview changes in PHP 5.4
Web Development Course: PHP lecture 1
The why and how of moving to PHP 5.4/5.5
PHP MySQL Workshop - facehook
PHP Security
07 Introduction to PHP #burningkeyboards
Zend Certification PHP 5 Sample Questions
Old Oracle Versions
Php Crash Course
Preparing for the next PHP version (5.6)
Php mysql
Php MySql For Beginners
PHP 7 – What changed internally?
Introduction To PHP
Back to basics - PHP_Codesniffer
Ad

Similar to Review unknown code with static analysis (20)

PDF
Static analysis saved my code tonight
PDF
PDF
Understanding static analysis php amsterdam 2018
PDF
Php 7 compliance workshop singapore
PDF
Quality Assurance for PHP projects - ZendCon 2012
KEY
Workshop quality assurance for php projects tek12
ODP
The why and how of moving to php 5.4/5.5
KEY
GettingStartedWithPHP
PDF
Workshop quality assurance for php projects - phpbelfast
PDF
Workshop quality assurance for php projects - ZendCon 2013
PDF
Php 7.2 compliance workshop php benelux
PPTX
Php7 HHVM and co
PDF
Last train to php 7
PPTX
Listen afup 2010
PPTX
Php7 hhvm and co
PPT
PHP and MySQL.ppt
PDF
Clear php reference
PDF
What's new with PHP7
PDF
Tips
PDF
Static analysis saved my code tonight
Understanding static analysis php amsterdam 2018
Php 7 compliance workshop singapore
Quality Assurance for PHP projects - ZendCon 2012
Workshop quality assurance for php projects tek12
The why and how of moving to php 5.4/5.5
GettingStartedWithPHP
Workshop quality assurance for php projects - phpbelfast
Workshop quality assurance for php projects - ZendCon 2013
Php 7.2 compliance workshop php benelux
Php7 HHVM and co
Last train to php 7
Listen afup 2010
Php7 hhvm and co
PHP and MySQL.ppt
Clear php reference
What's new with PHP7
Tips
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
Code review workshop
PDF
Review unknown code with static analysis php ce 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
Review unknown code with static analysis php ipc 2018
PDF
Code review for busy people
PDF
Machine learning in php las vegas
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
Code review workshop
Review unknown code with static analysis php ce 2018
Everything new with PHP 7.3
Php 7.3 et ses RFC (AFUP Toulouse)
Tout sur PHP 7.3 et ses RFC
Review unknown code with static analysis php ipc 2018
Code review for busy people
Machine learning in php las vegas

Recently uploaded (20)

PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Approach and Philosophy of On baking technology
PPTX
Cloud computing and distributed systems.
PPTX
Spectroscopy.pptx food analysis technology
PPT
Teaching material agriculture food technology
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Machine learning based COVID-19 study performance prediction
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Diabetes mellitus diagnosis method based random forest with bat algorithm
Approach and Philosophy of On baking technology
Cloud computing and distributed systems.
Spectroscopy.pptx food analysis technology
Teaching material agriculture food technology
Dropbox Q2 2025 Financial Results & Investor Presentation
Per capita expenditure prediction using model stacking based on satellite ima...
The Rise and Fall of 3GPP – Time for a Sabbatical?
Machine learning based COVID-19 study performance prediction
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Spectral efficient network and resource selection model in 5G networks
Agricultural_Statistics_at_a_Glance_2022_0.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Programs and apps: productivity, graphics, security and other tools
Unlocking AI with Model Context Protocol (MCP)
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
“AI and Expert System Decision Support & Business Intelligence Systems”
Profit Center Accounting in SAP S/4HANA, S4F28 Col11

Review unknown code with static analysis