SlideShare une entreprise Scribd logo
1  sur  39
Télécharger pour lire hors ligne
Static analysis
saved my
code tonight
Zendcon 2017,
Las Vegas, NE, USA
October 2017
Agenda
What is static analysis
Under the hood of a static analyzer
What analyzers can do for you
Speaker
Damien Seguy
Exakat CTO
Static analysis for PHP
Retiring house for elephpants
Is my code any good ?
The extra step
Execution
Opcode
Coding convention
Text file
An extra side step
Execution
Text file
Opcode Static analysis
An extra step
Execution
Opcode Static analysis
Text file
Switch statements may only
contain one default clause
<?php   
switch($x) {   
    case '1' :    
        break;   
    default :    
        break;   
    default :    
        break;   
    case '2' :    
        break;   
}   
Switch statements may only
contain one default clause
switch($x) {   
    case 1 :    
        break;   
    case 0+1 :    
        break;   
    case '1' :    
        break;   
    case true :    
        break;   
    case 1.0 :    
        break;   
    case $y :    
        break;   
}   
Static analysis
PHP 5 / 7
Calisthenics
ClearPHP
Performance
 
 

PHP tokens
Comments, Doc, whitespace
Delimiters : ' " () {} [] `
2/3 of the tokens are removed
[248] => Array
(
[0] => 382
[1] =>
[2] => 167
)
[249] => Array
(
[0] => 319
[1] => define
[2] => 167
)
[250] => (
[251] => Array
(
[0] => 323
[1] => 'EXT'
[2] => 167
)
[252] => ,
[253] => Array
(
[0] => 382
[1] =>
[2] => 167
)
<?php
//....
define('EXT', '.php');
AST
<?php
$x = source();
if ($x < 10) {
$y = $x + 1;
$x = corrige($y);
} else {
$y = $x;
}
Flow control
<?php
$x = source();
if ($x < 10) {
$y = $x + 1;
$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;
$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
Thousands of analysis
Analysis Freq.
function foo($a, $a, $a) {} 2.0%
!!(expression) 2.0%
substr($a, 2, 4) == 'abc' 9.0%
$a ? $b ? $c : $d : $e 11%
foreach($a as &$b) {} 15%
$var + 0 31%
if (strpos($a, $b)) {} 46%
include('file.php') 74%
PHP as a database
Source code is a highly organized dataset
We need a way to query it
There are over 84 static analysis tools for PHP
https://github.com/exakat/php-static-analysis-tools
Harnessing legacy code
Get a legacy code base to maintain
Assess the code and clean it
Set up a dev team
Update systems
Harnessing legacy code
Start with tests
unit, functional, assertions
Take a look and document everything
Find Bugs
Find bugs
PHP, logical, frameworks
Create a new coding reference
Choose a rule, fix the issues, clean around, repeat
Lists of issues
------ ---------------------------------------------------------------------------------------
Line src/Database.php
------ ---------------------------------------------------------------------------------------
33 Return typehint of method CorcelDatabase::connect() has invalid type CorcelIlluminat
35 Instantiated class IlluminateDatabaseCapsuleManager not found.
37 Call to method addConnection() on an unknown class IlluminateDatabaseCapsuleManager
38 Call to method bootEloquent() on an unknown class IlluminateDatabaseCapsuleManager.
40 Method CorcelDatabase::connect() should return CorcelIlluminateDatabaseCapsuleMan
----------------------------------------------------------------------------------------------
------ ---------------------------------------------------------------------------
Line src/Password/PasswordService.php
------ ---------------------------------------------------------------------------
11 Access to an undefined property CorcelPasswordPasswordService::$hasher.
11 Instantiated class HautelookPhpassPasswordHash not found.
28 Access to an undefined property CorcelPasswordPasswordService::$hasher.
58 Access to an undefined property CorcelPasswordPasswordService::$hasher.
------ ---------------------------------------------------------------------------
PHP 7.2 compatibility
Measure
Get metrics
Cyclomatic complexity, dependencies,
Inventory of features used
Metrics, break down by file
Appinfo()
Like phpinfo()
for PHP features
Update PHP code
Choose a set of analysis that matters
Fix them all
Add some more
Right in the IDE
Automated update
Working with more dev
Add more workforce to your project
Keep your code quality equal
Stop including code of lesser quality
Joining the team
3 rules 7 rules 15 rules
Full coding
reference
1
month
2
months
3
months
Full coding
reference
Third party quality review
10 rules Dev
Code
Report
Repository
Code
Report
Document your code
Help others understand that code
Compilation requirements, php.ini important directives
# install 2 extra extensions
#pecl install apc (https://pecl.php.net/package/apc)
#pecl install mysql (https://pecl.php.net/package/mysql)
./configure
--with-apxs2
--enable-apc
--enable-apc-debug
--with-bz2=DIR
--with-curl=DIR
--disable-dom
--enable-exif
--disable-fileinfo
--disable-filter
--disable-hash
—disable-json
--disable-libxml
--enable-mbstring
--with-libmbfl=DIR
--enable-mbstr-enc-trans
--disable-mbregex
--with-mcrypt=[DIR]
--with-mysql
--with-mysqli
--with-gd
--with-jpeg-dir=DIR
--with-png-dir=DIR
--with-xpm-dir=DIR
--with-vpx-dir=DIR
--with-freetype-dir=DIR
--enable-gd-native-ttf
--disable-pdo
PHP compile
[file]
; Unless you need to access remote files, it is better to be safe and forbid this
; feature
allow_url_fopen = Off
; Determines the size of the realpath cache to be used by PHP. The default value
; of "16k" is usually too low for modern application that open many files
; (autoload, fopen, filet_get_contents...). It is recommended to make this value
; up to 128 to 256k, and reduce it by testing with realpath_cache_get().
realpath_cache_size = 128k
; Duration of time (in seconds) for which to cache realpath information for a
; given file or directory. If the application's code doesn't change too often, you
; may set this directive to 3600 (one hour) or even more.
realpath_cache_ttl = 3600
; More information about file :
;http://php.net/manual/en/filesystem.configuration.php
[File Upload]
; This is the maximum uploaded size. It is recommended to keep this value as low
; as possible.
upload_max_filesize = 2M
; This is the maximum number of uploaded files in a single request.
max_file_uploads = 1
; Upload directory where PHP stores the temporary files. It is recommended to set
; this value, and separate it from other temporary directories.
upload_tmp_dir = /tmp/php_upload
; This is the maximum amount of data that PHP will accept in a POST request. It
; has to be higher or equal to upload_max_filesize. For security reasons, it
php.ini suggestions
UML diagram
Help your own users
Beyond documentation
Automate code review
Help users learn, promote best practices
Drive your users directly where it is important
Enrich your code today
Capitalize a lot of experience
Provides someone else 

point of view
Without anyone judging
Prepare for the future
Code fixing as relaxation
Tools used as illustration
https://github.com/exakat/php-static-analysis-tools
Exakat
Larashift
PHP Inspection
PHP Metrics
PHPStan
ZEND CON
https://joind.in/talk/c47dd
@exakat
http://www.exakat.io/

Contenu connexe

Tendances

Introduction to PHP 5.3
Introduction to PHP 5.3Introduction to PHP 5.3
Introduction to PHP 5.3
guestcc91d4
 

Tendances (20)

Apache Hive Hook
Apache Hive HookApache Hive Hook
Apache Hive Hook
 
Ajax basics
Ajax basicsAjax basics
Ajax basics
 
WAF Bypass Techniques - Using HTTP Standard and Web Servers’ Behaviour
WAF Bypass Techniques - Using HTTP Standard and Web Servers’ BehaviourWAF Bypass Techniques - Using HTTP Standard and Web Servers’ Behaviour
WAF Bypass Techniques - Using HTTP Standard and Web Servers’ Behaviour
 
Oracle database - Get external data via HTTP, FTP and Web Services
Oracle database - Get external data via HTTP, FTP and Web ServicesOracle database - Get external data via HTTP, FTP and Web Services
Oracle database - Get external data via HTTP, FTP and Web Services
 
Introduction to PHP 5.3
Introduction to PHP 5.3Introduction to PHP 5.3
Introduction to PHP 5.3
 
PHP from soup to nuts Course Deck
PHP from soup to nuts Course DeckPHP from soup to nuts Course Deck
PHP from soup to nuts Course Deck
 
Simplifying Apache Cascading
Simplifying Apache CascadingSimplifying Apache Cascading
Simplifying Apache Cascading
 
Propel Your PHP Applications
Propel Your PHP ApplicationsPropel Your PHP Applications
Propel Your PHP Applications
 
Saving The World From Guaranteed APOCALYPSE* Using Varnish and Memcached
Saving The World From Guaranteed APOCALYPSE* Using Varnish and Memcached Saving The World From Guaranteed APOCALYPSE* Using Varnish and Memcached
Saving The World From Guaranteed APOCALYPSE* Using Varnish and Memcached
 
DNS for Developers - ConFoo Montreal
DNS for Developers - ConFoo MontrealDNS for Developers - ConFoo Montreal
DNS for Developers - ConFoo Montreal
 
Boost your website by running PHP on Nginx
Boost your website by running PHP on NginxBoost your website by running PHP on Nginx
Boost your website by running PHP on Nginx
 
Solr for Indexing and Searching Logs
Solr for Indexing and Searching LogsSolr for Indexing and Searching Logs
Solr for Indexing and Searching Logs
 
Introduction To PHP
Introduction To PHPIntroduction To PHP
Introduction To PHP
 
Writing and using php streams and sockets tek11
Writing and using php streams and sockets   tek11Writing and using php streams and sockets   tek11
Writing and using php streams and sockets tek11
 
Moving Graphs to Production At Scale
Moving Graphs to Production At ScaleMoving Graphs to Production At Scale
Moving Graphs to Production At Scale
 
Modern tooling to assist with developing applications on FreeBSD
Modern tooling to assist with developing applications on FreeBSDModern tooling to assist with developing applications on FreeBSD
Modern tooling to assist with developing applications on FreeBSD
 
Slow Database in your PHP stack? Don't blame the DBA!
Slow Database in your PHP stack? Don't blame the DBA!Slow Database in your PHP stack? Don't blame the DBA!
Slow Database in your PHP stack? Don't blame the DBA!
 
Php Introduction nikul
Php Introduction nikulPhp Introduction nikul
Php Introduction nikul
 
Spl in the wild
Spl in the wildSpl in the wild
Spl in the wild
 
Get more than a cache back! - ConFoo Montreal
Get more than a cache back! - ConFoo MontrealGet more than a cache back! - ConFoo Montreal
Get more than a cache back! - ConFoo Montreal
 

Similaire à Static analysis saved my code tonight

Tips
TipsTips
Tips
mclee
 
Site Performance - From Pinto to Ferrari
Site Performance - From Pinto to FerrariSite Performance - From Pinto to Ferrari
Site Performance - From Pinto to Ferrari
Joseph Scott
 
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Muhamad Al Imran
 
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Muhamad Al Imran
 

Similaire à Static analysis saved my code tonight (20)

Tips
TipsTips
Tips
 
Site Performance - From Pinto to Ferrari
Site Performance - From Pinto to FerrariSite Performance - From Pinto to Ferrari
Site Performance - From Pinto to Ferrari
 
Review unknown code with static analysis
Review unknown code with static analysisReview unknown code with static analysis
Review unknown code with static analysis
 
PHP & Performance
PHP & PerformancePHP & Performance
PHP & Performance
 
php & performance
 php & performance php & performance
php & performance
 
Php intro
Php introPhp intro
Php intro
 
Php intro
Php introPhp intro
Php intro
 
Php intro
Php introPhp intro
Php intro
 
Api Design
Api DesignApi Design
Api Design
 
Review unknown code with static analysis Zend con 2017
Review unknown code with static analysis  Zend con 2017Review unknown code with static analysis  Zend con 2017
Review unknown code with static analysis Zend con 2017
 
Caching and tuning fun for high scalability
Caching and tuning fun for high scalabilityCaching and tuning fun for high scalability
Caching and tuning fun for high scalability
 
Php manish
Php manishPhp manish
Php manish
 
Php i basic chapter 3
Php i basic chapter 3Php i basic chapter 3
Php i basic chapter 3
 
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
 
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
 
Pecl Picks
Pecl PicksPecl Picks
Pecl Picks
 
Practical catalyst
Practical catalystPractical catalyst
Practical catalyst
 
Php 7 compliance workshop singapore
Php 7 compliance workshop singaporePhp 7 compliance workshop singapore
Php 7 compliance workshop singapore
 
Performance tuning with zend framework
Performance tuning with zend frameworkPerformance tuning with zend framework
Performance tuning with zend framework
 
Performance all teh things
Performance all teh thingsPerformance all teh things
Performance all teh things
 

Plus de Damien Seguy

Plus de Damien Seguy (20)

Strong typing @ php leeds
Strong typing  @ php leedsStrong typing  @ php leeds
Strong typing @ php leeds
 
Strong typing : adoption, adaptation and organisation
Strong typing : adoption, adaptation and organisationStrong typing : adoption, adaptation and organisation
Strong typing : adoption, adaptation and organisation
 
Qui a laissé son mot de passe dans le code
Qui a laissé son mot de passe dans le codeQui a laissé son mot de passe dans le code
Qui a laissé son mot de passe dans le code
 
Analyse statique et applications
Analyse statique et applicationsAnalyse statique et applications
Analyse statique et applications
 
Top 10 pieges php afup limoges
Top 10 pieges php   afup limogesTop 10 pieges php   afup limoges
Top 10 pieges php afup limoges
 
Top 10 php classic traps DPC 2020
Top 10 php classic traps DPC 2020Top 10 php classic traps DPC 2020
Top 10 php classic traps DPC 2020
 
Meilleur du typage fort (AFUP Day, 2020)
Meilleur du typage fort (AFUP Day, 2020)Meilleur du typage fort (AFUP Day, 2020)
Meilleur du typage fort (AFUP Day, 2020)
 
Top 10 php classic traps confoo
Top 10 php classic traps confooTop 10 php classic traps confoo
Top 10 php classic traps confoo
 
Tout pour se préparer à PHP 7.4
Tout pour se préparer à PHP 7.4Tout pour se préparer à PHP 7.4
Tout pour se préparer à PHP 7.4
 
Top 10 php classic traps php serbia
Top 10 php classic traps php serbiaTop 10 php classic traps php serbia
Top 10 php classic traps php serbia
 
Top 10 php classic traps
Top 10 php classic trapsTop 10 php classic traps
Top 10 php classic traps
 
Top 10 chausse trappes
Top 10 chausse trappesTop 10 chausse trappes
Top 10 chausse trappes
 
Code review workshop
Code review workshopCode review workshop
Code review workshop
 
Understanding static analysis php amsterdam 2018
Understanding static analysis   php amsterdam 2018Understanding static analysis   php amsterdam 2018
Understanding static analysis php amsterdam 2018
 
Review unknown code with static analysis php ce 2018
Review unknown code with static analysis   php ce 2018Review unknown code with static analysis   php ce 2018
Review unknown code with static analysis php ce 2018
 
Everything new with PHP 7.3
Everything new with PHP 7.3Everything new with PHP 7.3
Everything new with PHP 7.3
 
Php 7.3 et ses RFC (AFUP Toulouse)
Php 7.3 et ses RFC  (AFUP Toulouse)Php 7.3 et ses RFC  (AFUP Toulouse)
Php 7.3 et ses RFC (AFUP Toulouse)
 
Tout sur PHP 7.3 et ses RFC
Tout sur PHP 7.3 et ses RFCTout sur PHP 7.3 et ses RFC
Tout sur PHP 7.3 et ses RFC
 
Review unknown code with static analysis php ipc 2018
Review unknown code with static analysis   php ipc 2018Review unknown code with static analysis   php ipc 2018
Review unknown code with static analysis php ipc 2018
 
Code review for busy people
Code review for busy peopleCode review for busy people
Code review for busy people
 

Dernier

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Dernier (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 

Static analysis saved my code tonight

  • 1. Static analysis saved my code tonight Zendcon 2017, Las Vegas, NE, USA October 2017
  • 2. Agenda What is static analysis Under the hood of a static analyzer What analyzers can do for you
  • 3. Speaker Damien Seguy Exakat CTO Static analysis for PHP Retiring house for elephpants
  • 4. Is my code any good ?
  • 6. An extra side step Execution Text file Opcode Static analysis
  • 7. An extra step Execution Opcode Static analysis Text file
  • 8. Switch statements may only contain one default clause <?php    switch($x) {        case '1' :             break;        default :             break;        default :             break;        case '2' :             break;    }   
  • 9. Switch statements may only contain one default clause switch($x) {        case 1 :             break;        case 0+1 :             break;        case '1' :             break;        case true :             break;        case 1.0 :             break;        case $y :             break;    }   
  • 10. Static analysis PHP 5 / 7 Calisthenics ClearPHP Performance     
  • 11. PHP tokens Comments, Doc, whitespace Delimiters : ' " () {} [] ` 2/3 of the tokens are removed [248] => Array ( [0] => 382 [1] => [2] => 167 ) [249] => Array ( [0] => 319 [1] => define [2] => 167 ) [250] => ( [251] => Array ( [0] => 323 [1] => 'EXT' [2] => 167 ) [252] => , [253] => Array ( [0] => 382 [1] => [2] => 167 ) <?php //.... define('EXT', '.php');
  • 12. AST <?php $x = source(); if ($x < 10) { $y = $x + 1; $x = corrige($y); } else { $y = $x; }
  • 13. Flow control <?php $x = source(); if ($x < 10) { $y = $x + 1; $x = corrige($y); } else { $y = $x; } $x = source; if ($x < 10) $y = $x; $y = $x + 1; $x = corrige($y); end $a = 3; start
  • 14. Data dependency graph <?php $x = source(); if ($x < 10) { $y = $x + 1; $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
  • 15. Thousands of analysis Analysis Freq. function foo($a, $a, $a) {} 2.0% !!(expression) 2.0% substr($a, 2, 4) == 'abc' 9.0% $a ? $b ? $c : $d : $e 11% foreach($a as &$b) {} 15% $var + 0 31% if (strpos($a, $b)) {} 46% include('file.php') 74%
  • 16. PHP as a database Source code is a highly organized dataset We need a way to query it There are over 84 static analysis tools for PHP https://github.com/exakat/php-static-analysis-tools
  • 17. Harnessing legacy code Get a legacy code base to maintain Assess the code and clean it Set up a dev team Update systems
  • 18. Harnessing legacy code Start with tests unit, functional, assertions Take a look and document everything
  • 19. Find Bugs Find bugs PHP, logical, frameworks Create a new coding reference Choose a rule, fix the issues, clean around, repeat
  • 20. Lists of issues ------ --------------------------------------------------------------------------------------- Line src/Database.php ------ --------------------------------------------------------------------------------------- 33 Return typehint of method CorcelDatabase::connect() has invalid type CorcelIlluminat 35 Instantiated class IlluminateDatabaseCapsuleManager not found. 37 Call to method addConnection() on an unknown class IlluminateDatabaseCapsuleManager 38 Call to method bootEloquent() on an unknown class IlluminateDatabaseCapsuleManager. 40 Method CorcelDatabase::connect() should return CorcelIlluminateDatabaseCapsuleMan ---------------------------------------------------------------------------------------------- ------ --------------------------------------------------------------------------- Line src/Password/PasswordService.php ------ --------------------------------------------------------------------------- 11 Access to an undefined property CorcelPasswordPasswordService::$hasher. 11 Instantiated class HautelookPhpassPasswordHash not found. 28 Access to an undefined property CorcelPasswordPasswordService::$hasher. 58 Access to an undefined property CorcelPasswordPasswordService::$hasher. ------ ---------------------------------------------------------------------------
  • 22. Measure Get metrics Cyclomatic complexity, dependencies, Inventory of features used
  • 25. Update PHP code Choose a set of analysis that matters Fix them all Add some more
  • 28. Working with more dev Add more workforce to your project Keep your code quality equal Stop including code of lesser quality
  • 29. Joining the team 3 rules 7 rules 15 rules Full coding reference 1 month 2 months 3 months
  • 30. Full coding reference Third party quality review 10 rules Dev Code Report Repository Code Report
  • 31. Document your code Help others understand that code Compilation requirements, php.ini important directives
  • 32. # install 2 extra extensions #pecl install apc (https://pecl.php.net/package/apc) #pecl install mysql (https://pecl.php.net/package/mysql) ./configure --with-apxs2 --enable-apc --enable-apc-debug --with-bz2=DIR --with-curl=DIR --disable-dom --enable-exif --disable-fileinfo --disable-filter --disable-hash —disable-json --disable-libxml --enable-mbstring --with-libmbfl=DIR --enable-mbstr-enc-trans --disable-mbregex --with-mcrypt=[DIR] --with-mysql --with-mysqli --with-gd --with-jpeg-dir=DIR --with-png-dir=DIR --with-xpm-dir=DIR --with-vpx-dir=DIR --with-freetype-dir=DIR --enable-gd-native-ttf --disable-pdo PHP compile
  • 33. [file] ; Unless you need to access remote files, it is better to be safe and forbid this ; feature allow_url_fopen = Off ; Determines the size of the realpath cache to be used by PHP. The default value ; of "16k" is usually too low for modern application that open many files ; (autoload, fopen, filet_get_contents...). It is recommended to make this value ; up to 128 to 256k, and reduce it by testing with realpath_cache_get(). realpath_cache_size = 128k ; Duration of time (in seconds) for which to cache realpath information for a ; given file or directory. If the application's code doesn't change too often, you ; may set this directive to 3600 (one hour) or even more. realpath_cache_ttl = 3600 ; More information about file : ;http://php.net/manual/en/filesystem.configuration.php [File Upload] ; This is the maximum uploaded size. It is recommended to keep this value as low ; as possible. upload_max_filesize = 2M ; This is the maximum number of uploaded files in a single request. max_file_uploads = 1 ; Upload directory where PHP stores the temporary files. It is recommended to set ; this value, and separate it from other temporary directories. upload_tmp_dir = /tmp/php_upload ; This is the maximum amount of data that PHP will accept in a POST request. It ; has to be higher or equal to upload_max_filesize. For security reasons, it php.ini suggestions
  • 35. Help your own users Beyond documentation Automate code review Help users learn, promote best practices Drive your users directly where it is important
  • 36.
  • 37. Enrich your code today Capitalize a lot of experience Provides someone else 
 point of view Without anyone judging Prepare for the future Code fixing as relaxation
  • 38. Tools used as illustration https://github.com/exakat/php-static-analysis-tools Exakat Larashift PHP Inspection PHP Metrics PHPStan