SlideShare a Scribd company logo
REVIEW
UNKNOWN CODE
WITH STATIC ANALYSIS
Berlin, Germany, June 2018
Agenda
Reviewing code
Static analysis for PHP
A session in which you are the hero
Review some PHP 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 have an opinion?
Review the 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 can only rely on the current state
Speaker
Damien Seguy
Exakat CTO
Static analysis for PHP
Elephpant retirement home
Ich spreche kein Deutsch
Source code is structured
Source code is a structured dataset
We need 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   php ipc 2018
Review unknown code with static analysis   php ipc 2018
Review unknown code with static analysis   php ipc 2018
Review unknown code with static analysis   php ipc 2018
Extensions 1 / 2
Extensions 2 / 2
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   php ipc 2018
List of 

directives
Automated code review
Analyze code
Report PHP related problems
Review unknown code with static analysis   php ipc 2018
Tactical mistakes in the code
PHP classic trap
Development left overs
Dead code
Code modernisation
Literal bugs
Clean code for PHP
Best practices
Security, performance, clean code
in-house, PSR, calisthenics, other inspirations
Code mantras, code kata
PHP Manual
Migration guides
Review unknown code with static analysis   php ipc 2018
Exakat : 650 analysis
Analysis Freq. Here
function __destruct() { throw …} :
0,3 % 0
0.6% 0
function foo($a, $a, $a) {} 2.0% 0
substr($a, 2, 4) == 'abc' 6.9% 0
!!(expression) 8.0% 34
$a ? $b ? $c : $d : $e 11% 2
if (strpos($a, $b)) {} 46% 40
include('file.php') 55% 180
foreach($a as &$b) {} 60% 30
Which PHP version?
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 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
Reflexion / Better Reflexion
PHAN
PhanTypeMismatchArgument Argument 2 (order) is false but EGroupwareApi
Hooks::process() takes array|string defined at
Total : 11999 results / 96 types
1679 issues
PhanTypeArraySuspiciousNullable Suspicious array access to nullable ?array|null
1256 issues
PhanUndeclaredClassMethod Call to method decode from undeclared class Horde_Mime
1247 issues
PhanUndeclaredProperty Reference to undeclared property calendar_bo->total
PhanTypeInvalidDimOffset Invalid offset "timeformat" of array type array{tz:mixed}
999 issues
668 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
PHP LINT - 5.5->7.3
Not a single error
615 compiled files
PHP 7.2.6 | 10 parallel jobs
............................................................ 60/615 (9 %)
............................................................ 120/615 (19 %)
............................................................ 180/615 (29 %)
............................................................ 240/615 (39 %)
............................................................ 300/615 (48 %)
............................................................ 360/615 (58 %)
............................................................ 420/615 (68 %)
............................................................ 480/615 (78 %)
............................................................ 540/615 (87 %)
............................................................ 600/615 (97 %)
............... 615/615 (100 %)
Checked 615 files in 4 seconds
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
timesheet_hooks 1
timesheet_import_csv 1
timesheet_merge 1
timesheet_tracking 1
timesheet_ui 1
timesheet_wizard_export_csv 1
timesheet_wizard_import_csv 1
uiconfig 1
Customfields 2
Dbmailuser 2
File 2
Files 2
Html 2
Image 2
Link 2
Mail 2
Merge 2
Request 2
Session 2
Template 2
Tracking 2
Vfs 2
Base 3
Cache 3
Storage 3
Univention 3
Ads 4
Exception 4
Sql 4
StreamWrapper 4
Hooks 5
Ldap 5
Variables
$folder 5
$link 5
$n 5
$resource 5
$script 5
$val 5
$vcard 5
$_headerobject 6
$appdata 6
$contact 6
$etemplate 6
$field 6
$name 6
$recurrence 6
$replacements 6
$account 7
$config 7
$list 7
$options 7
$v 7
$col 8
$item 8
$alarm 9
$event 9
$settings 9
$status 10
$id 12
$prop 13
$label 16
$data 19
$row 21
$value 21
9439 variables
Also : 

958 used-once variables
List of PHP analyzers
Exakat
Phan
Phploc
PHPmetrics
https://github.com/exakat/
php-static-analysis-tools
Medium application
Internal framework
Low level of issues
Tend to use old PHP features
Backward compatibilities
Deal with school : contacts, identity
www.egroupware.org
Danke schön
http://exakat.io/ - @exakat

More Related Content

Similar to Review unknown code with static analysis php ipc 2018 (20)

PDF
Static analysis saved my code tonight
Damien Seguy
 
PDF
Php 7.2 compliance workshop php benelux
Damien Seguy
 
PDF
PHPcon Poland - Static Analysis of PHP Code – How the Heck did I write so man...
Rouven Weßling
 
PDF
Preparing for the next PHP version (5.6)
Damien Seguy
 
PDF
Php 7 compliance workshop singapore
Damien Seguy
 
PDF
Preparing code for Php 7 workshop
Damien Seguy
 
PDF
Last train to php 7
Damien Seguy
 
PDF
Enter Cookbook: refactoring under a microscope
Kamil Samigullin
 
PDF
Review unknown code with static analysis - bredaphp
Damien Seguy
 
PDF
Preparing for the next php version
Damien Seguy
 
PPTX
Static Code Analysis PHP[tek] 2023
Scott Keck-Warren
 
PDF
The why and how of moving to php 8
Wim Godden
 
PDF
Effective code reviews
Sebastian Marek
 
PPTX
Reducing Bugs With Static Code Analysis php tek 2025
Scott Keck-Warren
 
PDF
The why and how of moving to php 7
Wim Godden
 
PDF
Effective code reviews
Sebastian Marek
 
PPTX
Listen afup 2010
Gabriele Santini
 
PDF
Code review workshop
Damien Seguy
 
PDF
Damien seguy php 5.6
Damien Seguy
 
PPT
Developing a Culture of Quality Code (Midwest PHP 2020)
Scott Keck-Warren
 
Static analysis saved my code tonight
Damien Seguy
 
Php 7.2 compliance workshop php benelux
Damien Seguy
 
PHPcon Poland - Static Analysis of PHP Code – How the Heck did I write so man...
Rouven Weßling
 
Preparing for the next PHP version (5.6)
Damien Seguy
 
Php 7 compliance workshop singapore
Damien Seguy
 
Preparing code for Php 7 workshop
Damien Seguy
 
Last train to php 7
Damien Seguy
 
Enter Cookbook: refactoring under a microscope
Kamil Samigullin
 
Review unknown code with static analysis - bredaphp
Damien Seguy
 
Preparing for the next php version
Damien Seguy
 
Static Code Analysis PHP[tek] 2023
Scott Keck-Warren
 
The why and how of moving to php 8
Wim Godden
 
Effective code reviews
Sebastian Marek
 
Reducing Bugs With Static Code Analysis php tek 2025
Scott Keck-Warren
 
The why and how of moving to php 7
Wim Godden
 
Effective code reviews
Sebastian Marek
 
Listen afup 2010
Gabriele Santini
 
Code review workshop
Damien Seguy
 
Damien seguy php 5.6
Damien Seguy
 
Developing a Culture of Quality Code (Midwest PHP 2020)
Scott Keck-Warren
 

More from Damien Seguy (20)

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

Recently uploaded (20)

PDF
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PPT
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Ad

Review unknown code with static analysis php ipc 2018