Clear PHP is a coding reference for all of us. It tracks recommendations to write clear code, avoiding the classic traps, keeping options opened for the future and preventing decayed code. Those recommendations are above coding conventions, that focuses on code writing, and code architecture, with its design patterns and conceptions. This is the first time in history where an effort was made to collect all those rules, so every project can choose among them, and define exactly his own coding standard.
3. Speaker
Damien Seguy
CTO at exakat
"Ik ben een boterham" : I'm a recent resident
Automated code audit services
4. 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
9. 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
12. 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
13. Something to avoid ?
Array
(
[0] => a
[1] => d
)
Array
(
[0] => c
[1] => d
)
clearPHP : no-dangling-reference
14. 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
15. Something to trap you ?
<?php
$x = true;
$y = false;
$z = $x and $y;
$z = $x && $y;
?>
clearPHP : no-letter-logical
16.
17. Something wrong ?
<?php
try {
} catch (UnresolvedClass $e) {
} catch (NotAnException $e) {
}
this is dead code too
clearPHP : no-unresolved-catch
this is dead code too
19. Something wrong ?
<?php
if ($x instanceof SomeKlasse) {
$y = $x->convert();
}
?>
Unresolved classes are not notified :
this is dead code
clearPHP : no-unresolved-instanceof
27. 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
28. Something wrong ?
<?php
switch ($x) {
case 1 :
// something useful
break;
case 1 :
// something useful
break;
}
this is still dead code
clearPHP : no-duplicate-case
35. 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
36. Something weird
<?php
class user {
public static countReal($array) {
return count(array_filter(
$array,
function ($x) { return !empty($x);}
);
}
}
?>
clearPHP : not-a-method
37. Something useless
<?php
interface i { function getI(); }
class x implements i {
function getI() { return rand(0, 10); }
}
?>
clearPHP : no-useless-interfaces
38. 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