SlideShare a Scribd company logo
he clearPHP
reference
Amsterdam, the Netherlands. May 21st 2015
T
Agenda
clearPHP reference
Focus on PHP
Make your own
Speaker
Damien Seguy
CTO at exakat
"Ik ben een boterham" : I'm a recent resident
Automated code audit services
Something wrong?
<?php
function f($a, $a, $a) {
echo $a;
}
f('b', 'c', 'd');
?>
Won’t work in PHP
7
W
ill work in Python
clearPHP : all-unique-arguments
Coding standards
Symfony
Wordpress
Drupal
Typo3
Atoum
CakePHP
PEAR
PSR
Rafael Dohms
…
Wordpress Standards
Single and Double Quotes
Indentation
Brace Style
Regular Expressions
No Shorthand PHP Tags
Remove Trailing Spaces
Space Usage
Formatting SQL statements
Database Queries
Naming Conventions
Self-Explanatory Flag Values for Function
Arguments
Ternary Operator
Yoda Conditions
Clever Code
Error Control Operator @
Don’t extract()
Coding conventions
Conception
PHP coding
reference
PHP gotcha
Something wrong?
<?php
class w extends v {
function f($a, $b = 2, $c) {
echo $a;
}
}
?>
Still works in PHP
7
clearPHP : argument-with-default-at-the-end
Something to avoid ?
<?php
if (($h = file_get_contents($uri)) == '') {
print "Error : couldn't access siten";
} else {
process_html($h);
}
?>
clearPHP : strict-comparisons
Something to avoid ?
• array_search
• collator_compare
• collator_get_sort_key
• current
• fgetc
• file_get_contents
• file_put_contents
• iconv_strpos
• iconv_strrpos
• imagecolorallocate
• imagecolorallocatealpha
• mb_strlen
• next
• pcntl_getpriority
• preg_match
• preg_match_all
• prev
• readdir
• stripos
• strpos
• strripos
• strrpos
• strtok
clearPHP : strict-comparisons
Something to trip on ?
<?php
$array = array('a', 'b');
$array2 = array('c', 'd');
foreach ($array as &$f) { }
foreach ($array2 as $f) { }
print_r($array);
print_r($array2);
clearPHP : no-dangling-reference
Something to avoid ?
Array
(
[0] => a
[1] => d
)
Array
(
[0] => c
[1] => d
)
clearPHP : no-dangling-reference
Something to trip on ?
<?php
$array = array('a', 'b');
$array2 = array('c', 'd');
foreach ($array as &$f) { }
unset($f);
foreach ($array2 as $f) { }
print_r($array);
print_r($array2);
clearPHP : no-dangling-reference
Something to trap you ?
<?php
$x = true;
$y = false;
$z = $x and $y;
$z = $x && $y;
?>
clearPHP : no-letter-logical
Clear php reference
Something wrong ?
<?php
try {
} catch (UnresolvedClass $e) {
} catch (NotAnException $e) {
}
this is dead code too
clearPHP : no-unresolved-catch
this is dead code too
Something wrong ?
<?php
namespace X;
try {
} catch (Exception $e) {
}
?>
this is still dead code
clearPHP : no-unresolved-catch
Something wrong ?
<?php
if ($x instanceof SomeKlasse) {
$y = $x->convert();
}
?>
Unresolved classes are not notified :
this is dead code
clearPHP : no-unresolved-instanceof
Performances
Something slow ?
<?php
define('DAY_IN_SECONDS', 24 * 60 * 60);
define('DAY_IN_SECONDS', 86400);
const DAY_IN_SECONDS = 86400;
?>
clearPHP : always-preprocess
Something slow ?
<?php
$x = [];
$x['id'] = 0;
$x['username'] = 'bibi';
$x['creation'] = time();
?>
clearPHP : always-preprocess
<?php
$x = [ 'id' => 0,
'username' => 'bibi',
'creation' => time(),
];
?>
Something slow ?
<?php
echo "<p>";
echo $paragraph;
echo "</p>";
?>
clearPHP : no-repeated-print
Something slow ?
<?php
$array = $source->fetchData();
$array = array_unique($array);
?>
clearPHP : avoid-those-slow-functions
<?php
$array = $database->fetchData();
$array = array_keys(
array_count_values($array));
?>
Something slow ?
clearPHP : no-array_merge-in-loop
<?php
$merged = [];
foreach($array as $id => $row) {
$array[$id][4] = VAT * $row[2];
$merged = array_merge($merged, $row);
}
?>
<?php
foreach($array as &$row) {
$row[4] = VAT * $row[2];
}
unset($row);
$merged = array_merge($merged, $array);
?> clearPHP : use-reference-to-alter-in-foreach
PHP tricks
Something wrong ?
<?php
switch ($x) {
default :
// something useful
break;
default :
// something else and useful
break;
}
this is still dead code
clearPHP : no-switch-with-multiple-defaultWon’t work in PHP
7
Something wrong ?
<?php
switch ($x) {
case 1 :
// something useful
break;
case 1 :
// something useful
break;
}
this is still dead code
clearPHP : no-duplicate-case
Something wrong ?
<?php
$array = ['png' => 'image/png',
'jpg' => 'image/jpg',
'jpeg' => 'image/jpeg',
'gif' => 'image/gif',
'webp' => 'image/webp',
'wbmp' => 'image/wbmp',
'png' => 'image/png',
];
?>clearPHP : no-duplicate-key
Something wrong ?
<?php
$array = [ 1 => 2,
'1' => 4,
5 => 6,
7 => 8,
9 => 10,
11 => 12,
1.3 => 14
];
two of
them
is
dead
code
clearPHP : no-duplicate-key
Something insane
<?php
$content = @file_get_contents($uri);
?>
clearPHP : no-noscream
Something insane
<?php
class user {
public static getName() {
return $this->name;
}
}
?>
clearPHP : no-static-this Still works in PHP
7
Not so specific to PHP
Something insane ?
<?php
$credit = ( !is_array($_credit)) ?
PtoBR(propre($_credit)) :
( $_credit['url'] ? '<a href="' .
$_credit['url'] . '">' : '') .
$_credit['nom'] . ( $_credit['url'] ?
'</a>' : '');
?>
clearPHP : no-nested-ternary
Something hardcoded
<?php
$ftp_user = 'foo';
$ftp_pass = 'bar';
// set up a connection
$conn_id = ftp_connect('ftp.example.nl');
// authentication
ftp_login($conn_id, $ftp_user, $ftp_pass);
?>
clearPHP : no-hardcoded-credential
Something weird
<?php
class user {
public static countReal($array) {
return count(array_filter(
$array,
function ($x) { return !empty($x);}
);
}
}
?>
clearPHP : not-a-method
Something useless
<?php
interface i { function getI(); }
class x implements i {
function getI() { return rand(0, 10); }
}
?>
clearPHP : no-useless-interfaces
Something useless
<?php
function negateI(i $x) {
return - $x->getI();
}
function sumI(array $a) {
$r = 0;
foreach($a as $b) {
if ($x instanceof i) {
$r += $x->getI();
}
}
return $r;
}
clearPHP : no-useless-interfaces
clearPHP
https://github.com/
dseguy/clearPHP
109 rules for clear
coding in PHP
clearPHP
Name
Explanations
Examples
Do and don't
More reading material
Single and Double Quotes
Indentation
Brace Style
[Regular Expressions]
no-short-tags
Remove Trailing Spaces
Space Usage
Formatting SQL statements
Database Queries - 

(always-prepare-statement)
Naming Conventions
Self-Explanatory Flag Values for Function
Arguments
[Ternary Operator]
yoda-condition
No-implied-if
No-scream
Know-your-variables
Wordpress Standards
Sources
PHP Manual
Articles
Common practices
Feedback on clearPHP's repo
Something greedy ?
<?php
echo ("<p>" . $paragraph . "</p>");
?>
Something greedy ?
<?php
echo "<p>" . $paragraph . "</p>";
?>
clearPHP : no-parenthesis-for-language-construct
Something greedy ?
<?php
echo "<p>", $paragraph, "</p>";
?>
clearPHP : no-unnecessary-string-concatenation
Build your own reference
Read the reference
Cherry pick the rules
you like
Ignore the rules you
don't like
Herald this as your own
reference
More rules to come
Do not use the 'else' keyword
Do not use 'else if' but make it one else if
Do wash your hands before hitting the keyboard
Texte
Thanks!
@faguo, dseguy@exakat.com,
https://github.com/dseguy/clearPHP
Clear php reference
clearPHP
Rules to write good PHP code
Rules to write PHP code
Largest list of recommendations
Cherry pick your selection
Clear php reference
No one knows why
Looks like old PHP 4
Bad for performance
Bad for security
Bad for maintenance
Newbie mistake
Bad for testing

More Related Content

PDF
PHPSpec BDD Framework
PDF
Just-In-Time Compiler in PHP 8
PDF
Diving into HHVM Extensions (PHPNW Conference 2015)
KEY
Intermediate PHP
PDF
Cli the other sapi pbc11
ODP
Back to basics - PHP_Codesniffer
PDF
CLI, the other SAPI phpnw11
PPTX
Xenogenetics for PL/SQL - infusing with Java best practices
PHPSpec BDD Framework
Just-In-Time Compiler in PHP 8
Diving into HHVM Extensions (PHPNW Conference 2015)
Intermediate PHP
Cli the other sapi pbc11
Back to basics - PHP_Codesniffer
CLI, the other SAPI phpnw11
Xenogenetics for PL/SQL - infusing with Java best practices

What's hot (19)

KEY
Zend Framework Study@Tokyo #2
PPTX
Zephir - A Wind of Change for writing PHP extensions
PPTX
Php7 HHVM and co
PDF
Cli the other SAPI confoo11
PDF
TDC2016SP - Trilha Developing for Business
KEY
Zend Framework Study@Tokyo vol1
PDF
Introduction to PHP - Basics of PHP
PDF
Developing for Business
PPTX
New in php 7
PDF
07 Introduction to PHP #burningkeyboards
PDF
Dealing With Legacy PHP Applications
KEY
PHPSpec BDD for PHP
PPS
Php security3895
PDF
What's new with PHP7
PPT
PHP POWERPOINT SLIDES
PDF
Zend Server: Not just a PHP stack
PDF
PHP Conference Asia 2016
PDF
Forget about Index.php and build you applications around HTTP - PHPers Cracow
PPTX
PHP Basics
Zend Framework Study@Tokyo #2
Zephir - A Wind of Change for writing PHP extensions
Php7 HHVM and co
Cli the other SAPI confoo11
TDC2016SP - Trilha Developing for Business
Zend Framework Study@Tokyo vol1
Introduction to PHP - Basics of PHP
Developing for Business
New in php 7
07 Introduction to PHP #burningkeyboards
Dealing With Legacy PHP Applications
PHPSpec BDD for PHP
Php security3895
What's new with PHP7
PHP POWERPOINT SLIDES
Zend Server: Not just a PHP stack
PHP Conference Asia 2016
Forget about Index.php and build you applications around HTTP - PHPers Cracow
PHP Basics
Ad

Similar to Clear php reference (20)

PDF
Hunt for dead code
PDF
Php 7 errors messages
PDF
Php 7 compliance workshop singapore
KEY
PHP security audits
PDF
Php 7.2 compliance workshop php benelux
PDF
Last train to php 7
PDF
Review unknown code with static analysis
PPTX
Programming best practices (PHP)
PDF
Introduction to PHP
PDF
Preparing code for Php 7 workshop
PDF
Static analysis saved my code tonight
ODP
What's new, what's hot in PHP 5.3
PDF
Getting started with php
PDF
The why and how of moving to php 8
PDF
Preparing for the next php version
PDF
Review unknown code with static analysis Zend con 2017
PDF
The new features of PHP 7
PDF
The new features of PHP 7 - Enrico Zimuel - Codemotion Milan 2016
PPTX
25 php interview questions – codementor
PDF
The why and how of moving to php 7
Hunt for dead code
Php 7 errors messages
Php 7 compliance workshop singapore
PHP security audits
Php 7.2 compliance workshop php benelux
Last train to php 7
Review unknown code with static analysis
Programming best practices (PHP)
Introduction to PHP
Preparing code for Php 7 workshop
Static analysis saved my code tonight
What's new, what's hot in PHP 5.3
Getting started with php
The why and how of moving to php 8
Preparing for the next php version
Review unknown code with static analysis Zend con 2017
The new features of PHP 7
The new features of PHP 7 - Enrico Zimuel - Codemotion Milan 2016
25 php interview questions – codementor
The why and how of moving to php 7
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
Understanding static analysis php amsterdam 2018
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
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
Understanding static analysis php amsterdam 2018
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

Recently uploaded (20)

PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Empathic Computing: Creating Shared Understanding
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
KodekX | Application Modernization Development
PDF
Electronic commerce courselecture one. Pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Empathic Computing: Creating Shared Understanding
Agricultural_Statistics_at_a_Glance_2022_0.pdf
MYSQL Presentation for SQL database connectivity
20250228 LYD VKU AI Blended-Learning.pptx
Review of recent advances in non-invasive hemoglobin estimation
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
MIND Revenue Release Quarter 2 2025 Press Release
Building Integrated photovoltaic BIPV_UPV.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
KodekX | Application Modernization Development
Electronic commerce courselecture one. Pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Unlocking AI with Model Context Protocol (MCP)
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf

Clear php reference