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. 4.

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

    automation DevOps Tools • Continuous integration • Monitoring • Deployment
  2. 6.

    Static Analysis Looking at code in its own right •

    Evaluate files and structure • Tokenise code • Don't execute it
  3. 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?
  4. 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; }
  5. 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 --------------------------------------------------------
  6. 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
  7. 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
  8. 13.

    PHPDocumentor (phpdoc) • Command-line tool, available from PEAR • PHPDocumentor

    creates documentation from code • Useful reference and deliverable • What if you have no comments?
  9. 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; }
  10. 19.
  11. 20.

    phpdoc Example <?php /** * The Cat class for all

    things feline * * @package Toy Code * @author Lorna Mitchell <geek@lornajane.net> */ class Cat {
  12. 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;
  13. 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; } }
  14. 25.

    Other Static Analysis Tools • Lint check: php -l •

    PHP Lines of Code (phploc) • PHP Mess Detector (phpmd)
  15. 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
  16. 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)
  17. 29.
  18. 30.

    Phing PHing Is Not Gnu Make • easy way to

    run project tasks • php specific • XML config file can be part of project
  19. 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
  20. 34.

    Deployment Steps: 2 Put code on server. • upload •

    uncompress to a new directory • link configuration files, upload files, etc
  21. 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
  22. 38.
  23. 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
  24. 44.

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

    a problem when the client calls you Uptime Robot: http://uptimerobot.com
  25. 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/