Ce diaporama a bien été signalé.
Le téléchargement de votre SlideShare est en cours. ×

Clear php reference

Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Prochain SlideShare
PHPSpec BDD Framework
PHPSpec BDD Framework
Chargement dans…3
×

Consultez-les par la suite

1 sur 52 Publicité

Clear php reference

Télécharger pour lire hors ligne

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.

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.

Publicité
Publicité

Plus De Contenu Connexe

Diaporamas pour vous (20)

Similaire à Clear php reference (20)

Publicité

Plus par Damien Seguy (20)

Plus récents (20)

Publicité

Clear php reference

  1. 1. he clearPHP reference Amsterdam, the Netherlands. May 21st 2015 T
  2. 2. Agenda clearPHP reference Focus on PHP Make your own
  3. 3. Speaker Damien Seguy CTO at exakat "Ik ben een boterham" : I'm a recent resident Automated code audit services
  4. 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
  5. 5. Coding standards Symfony Wordpress Drupal Typo3 Atoum CakePHP PEAR PSR Rafael Dohms …
  6. 6. 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()
  7. 7. Coding conventions Conception PHP coding reference
  8. 8. PHP gotcha
  9. 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
  10. 10. Something to avoid ? <?php if (($h = file_get_contents($uri)) == '') { print "Error : couldn't access siten"; } else { process_html($h); } ?> clearPHP : strict-comparisons
  11. 11. 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
  12. 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. 13. Something to avoid ? Array ( [0] => a [1] => d ) Array ( [0] => c [1] => d ) clearPHP : no-dangling-reference
  14. 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. 15. Something to trap you ? <?php $x = true; $y = false; $z = $x and $y; $z = $x && $y; ?> clearPHP : no-letter-logical
  16. 16. Something wrong ? <?php try { } catch (UnresolvedClass $e) { } catch (NotAnException $e) { } this is dead code too clearPHP : no-unresolved-catch this is dead code too
  17. 17. Something wrong ? <?php namespace X; try { } catch (Exception $e) { } ?> this is still dead code clearPHP : no-unresolved-catch
  18. 18. Something wrong ? <?php if ($x instanceof SomeKlasse) { $y = $x->convert(); } ?> Unresolved classes are not notified : this is dead code clearPHP : no-unresolved-instanceof
  19. 19. Performances
  20. 20. Something slow ? <?php define('DAY_IN_SECONDS', 24 * 60 * 60); define('DAY_IN_SECONDS', 86400); const DAY_IN_SECONDS = 86400; ?> clearPHP : always-preprocess
  21. 21. Something slow ? <?php $x = []; $x['id'] = 0; $x['username'] = 'bibi'; $x['creation'] = time(); ?> clearPHP : always-preprocess <?php $x = [ 'id' => 0, 'username' => 'bibi', 'creation' => time(), ]; ?>
  22. 22. Something slow ? <?php echo "<p>"; echo $paragraph; echo "</p>"; ?> clearPHP : no-repeated-print
  23. 23. 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)); ?>
  24. 24. 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
  25. 25. PHP tricks
  26. 26. 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
  27. 27. Something wrong ? <?php switch ($x) { case 1 : // something useful break; case 1 : // something useful break; } this is still dead code clearPHP : no-duplicate-case
  28. 28. 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
  29. 29. 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
  30. 30. Something insane <?php $content = @file_get_contents($uri); ?> clearPHP : no-noscream
  31. 31. Something insane <?php class user { public static getName() { return $this->name; } } ?> clearPHP : no-static-this Still works in PHP 7
  32. 32. Not so specific to PHP
  33. 33. 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
  34. 34. 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
  35. 35. Something weird <?php class user { public static countReal($array) { return count(array_filter( $array, function ($x) { return !empty($x);} ); } } ?> clearPHP : not-a-method
  36. 36. Something useless <?php interface i { function getI(); } class x implements i { function getI() { return rand(0, 10); } } ?> clearPHP : no-useless-interfaces
  37. 37. 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
  38. 38. clearPHP https://github.com/ dseguy/clearPHP 109 rules for clear coding in PHP
  39. 39. clearPHP Name Explanations Examples Do and don't More reading material
  40. 40. 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
  41. 41. Sources PHP Manual Articles Common practices Feedback on clearPHP's repo
  42. 42. Something greedy ? <?php echo ("<p>" . $paragraph . "</p>"); ?>
  43. 43. Something greedy ? <?php echo "<p>" . $paragraph . "</p>"; ?> clearPHP : no-parenthesis-for-language-construct
  44. 44. Something greedy ? <?php echo "<p>", $paragraph, "</p>"; ?> clearPHP : no-unnecessary-string-concatenation
  45. 45. 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
  46. 46. 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
  47. 47. Texte Thanks! @faguo, dseguy@exakat.com, https://github.com/dseguy/clearPHP
  48. 48. clearPHP Rules to write good PHP code Rules to write PHP code Largest list of recommendations Cherry pick your selection
  49. 49. No one knows why Looks like old PHP 4 Bad for performance Bad for security Bad for maintenance Newbie mistake Bad for testing

×