Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Tools for Better PHP

Tools for Better PHP

Presentation covering a few tools that web developers may find useful during development, deployment, and after the application is live. These include gearman and supervisord, static analysis tools, deployment and automation with phing, recommendation for monitoring tools, and some tips about how continuous integration can help you fit it all together. Presented at PHP tek 2013.

D33d8bdd9096c80b8d1acca8d28410b5?s=128

Lorna Mitchell

May 15, 2013
Tweet

Transcript

  1. Tools for Better PHP Lorna Mitchell, php|tek 2013

  2. Tools for Better PHP Hi, I'm Lorna and I care

    about tools
  3. Good Tools Matter

  4. Today's Tools Development Tools • Static analysis tools • Task

    automation DevOps Tools • Continuous integration • Monitoring • Deployment
  5. Source Control is Key

  6. Static Analysis Looking at code in its own right •

    Evaluate files and structure • Tokenise code • Don't execute it
  7. PHP Code Sniffer (phpcs) • Command-line tool, available from PEAR

    • CodeSniffer checks if coding standards are followed • Bracket location • Whitespace • Naming conventions • PHP doesn't care about whitespace, why should we?
  8. phpcs Example <?php class Cat { public function purr() {

    return "prrrr"; } public function scratch($thing) { return true; } public function fight(Cat $enemy) { $this->scratch($enemy); return true; }
  9. phpcs Example FILE: /home/lorna/.../tek13/5-tools/code/Cat.php -------------------------------------------------------- FOUND 8 ERROR(S) AFFECTING 5

    LINE(S) -------------------------------------------------------- 2 | ERROR | Missing file doc comment 3 | ERROR | Missing class doc comment 5 | ERROR | Missing function doc comment 5 | ERROR | Opening brace should be on a new line 9 | ERROR | Missing function doc comment 9 | ERROR | Opening brace should be on a new line 13 | ERROR | Missing function doc comment 13 | ERROR | Opening brace should be on a new line --------------------------------------------------------
  10. phpcs Example PHP CODE SNIFFER REPORT SUMMARY --------------------------------------------------------- FILE ERRORS

    WARNINGS --------------------------------------------------------- /home/lorna/.../code/Cat.php 8 0 --------------------------------------------------------- A TOTAL OF 8 ERROR(S) WERE FOUND IN 1 FILE(S) --------------------------------------------------------- Time: 0 seconds, Memory: 1.50Mb
  11. phpcs and Jenkins

  12. DIY CodeSniffer Standard • Standards are made of sniffs •

    There are lots of ready-defined sniffs available • You can "pick and mix" to define your own standard
  13. PHPDocumentor (phpdoc) • Command-line tool, available from PEAR • PHPDocumentor

    creates documentation from code • Useful reference and deliverable • What if you have no comments?
  14. phpdoc Example Generate documentation into the docs directory phpdoc -t

    docs -d .
  15. phpdoc Example

  16. phpdoc Example

  17. phpdoc Example <?php class Cat { public function purr() {

    return "prrrr"; } public function scratch($thing) { return true; } public function fight(Cat $enemy) { $this->scratch($enemy); return true; }
  18. phpdoc Example

  19. None
  20. phpdoc Example <?php /** * The Cat class for all

    things feline * * @package Toy Code * @author Lorna Mitchell <geek@lornajane.net> */ class Cat {
  21. /** * The purr function, output can vary between related

    spec * * @return string Representation of noise made by happy ca */ public function purr() { return "prrrr"; } /** * Method for expressing dissatisfaction towards something * * @param mixed $thing The offending thing * @return boolean Whether the thing was suitably rebuffed */ public function scratch($thing) { return true;
  22. } /** * Invokes combat with the other Cat *

    * @param Cat $enemy The cat to fight with * @return boolean Whether $this was victorious */ public function fight(Cat $enemy) { $this->scratch($enemy); return true; } }
  23. phpdoc Example

  24. Adding PHPDoc to your Process • Comments also help: •

    IDEs • PHP WSDL Generator
  25. Other Static Analysis Tools • Lint check: php -l •

    PHP Lines of Code (phploc) • PHP Mess Detector (phpmd)
  26. Build Servers and PHP

  27. Build Server Many possible uses for a build server: •

    Run quality measure • Build docs • Deploy (more on this in a moment) • Run other phing tasks
  28. CI and Jenkins Continuous integration: regular build tasks • Often

    in response to commit/merge Jenkins: Java-based CI tool • Also see: PPW (PHP Project Wizard)
  29. Jenkins

  30. Phing PHing Is Not Gnu Make • easy way to

    run project tasks • php specific • XML config file can be part of project
  31. Example Phing build.xml <?xml version="1.0" encoding="UTF-8"?> <project name="joindin" default="build" basedir=".">

    <property name="source" value="src/system/application/"/> <property name="basedir" value="."/> <target name="phpunit" description="Run unit tests"> <exec command="phpunit -c src/tests/phpunit.xml ./src/ </target> </project> run with: phing phpunit
  32. Automated Deployment

  33. Deployment Steps: 1 Package application code. • tag/export • minify

    CSS, optimise images • compress
  34. Deployment Steps: 2 Put code on server. • upload •

    uncompress to a new directory • link configuration files, upload files, etc
  35. Deployment Steps: 3 (Maintenance mode if necessary) Apply database patches.

    • database patching strategy is vital • many good tools to help (dbdeploy, liquibase, or patch files) • code may tolerate multiple database states
  36. Deployment Steps: 4 Make new code live by switching symlink.

    (Remove maintenance mode)
  37. Deployment Steps: 5 Restart long-running processes. Sort out caches -

    clear/warm
  38. Supervisord A process to care for your long-running processes •

    logs output • restarts errored scripts
  39. Supervisord http://www.flickr.com/photos/megandavid/2142156

  40. Supervisorctl

  41. Using Supervisord Useful for: • application servers • IRC bots

    • workers
  42. Gearman • Gearman is a job server • A job

    needs doing, add it to the server • Workers take and process jobs • They need a restart when we deploy new code
  43. What next?

  44. Monitoring • Check sites are responding well • Don't discover

    a problem when the client calls you Uptime Robot: http://uptimerobot.com
  45. Uptime Robot

  46. Good Tools Mean Good Work

  47. Tools Glossary PHP Code Sniffer http://lrnja.net/17DaDvD PHPDocumentor http://phpdoc.org Jenkins http://jenkins-ci.org/

    Phing http://www.phing.info/ Supervisord http://supervisord.org/ Gearman http://gearman.org/ UptimeRobot http://www.uptimerobot.com/
  48. Further Reading PHP Master from Sitepoint (by @lornajane, @Davey and

    @Elazar)
  49. Thanks! Feedback: https://joind.in/8151 • Lorna Mitchell: web development consultant •

    http://lornajane.net • @lornajane