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.

Lorna Mitchell

May 15, 2013
Tweet

More Decks by Lorna Mitchell

Other Decks in Technology

Transcript

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

    automation DevOps Tools • Continuous integration • Monitoring • Deployment
  2. Static Analysis Looking at code in its own right •

    Evaluate files and structure • Tokenise code • Don't execute it
  3. 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. 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. 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. 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. 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. PHPDocumentor (phpdoc) • Command-line tool, available from PEAR • PHPDocumentor

    creates documentation from code • Useful reference and deliverable • What if you have no comments?
  9. 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. phpdoc Example <?php /** * The Cat class for all

    things feline * * @package Toy Code * @author Lorna Mitchell <[email protected]> */ class Cat {
  11. /** * 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;
  12. } /** * 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; } }
  13. Other Static Analysis Tools • Lint check: php -l •

    PHP Lines of Code (phploc) • PHP Mess Detector (phpmd)
  14. 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
  15. 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)
  16. Phing PHing Is Not Gnu Make • easy way to

    run project tasks • php specific • XML config file can be part of project
  17. 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
  18. Deployment Steps: 2 Put code on server. • upload •

    uncompress to a new directory • link configuration files, upload files, etc
  19. 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
  20. 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
  21. Monitoring • Check sites are responding well • Don't discover

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