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. Tools for Better PHP
    Lorna Mitchell, php|tek 2013

    View Slide

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

    View Slide

  3. Good Tools Matter

    View Slide

  4. Today's Tools
    Development Tools
    • Static analysis tools
    • Task automation
    DevOps Tools
    • Continuous integration
    • Monitoring
    • Deployment

    View Slide

  5. Source Control is Key

    View Slide

  6. Static Analysis
    Looking at code in its own right
    • Evaluate files and structure
    • Tokenise code
    • Don't execute it

    View Slide

  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?

    View Slide

  8. phpcs Example
    class Cat
    {
    public function purr() {
    return "prrrr";
    }
    public function scratch($thing) {
    return true;
    }
    public function fight(Cat $enemy) {
    $this->scratch($enemy);
    return true;
    }

    View Slide

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

    View Slide

  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

    View Slide

  11. phpcs and Jenkins

    View Slide

  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

    View Slide

  13. PHPDocumentor (phpdoc)
    • Command-line tool, available from PEAR
    • PHPDocumentor creates documentation from
    code
    • Useful reference and deliverable
    • What if you have no comments?

    View Slide

  14. phpdoc Example
    Generate documentation into the docs directory
    phpdoc -t docs -d .

    View Slide

  15. phpdoc Example

    View Slide

  16. phpdoc Example

    View Slide

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

    View Slide

  18. phpdoc Example

    View Slide

  19. View Slide

  20. phpdoc Example
    /**
    * The Cat class for all things feline
    *
    * @package Toy Code
    * @author Lorna Mitchell
    */
    class Cat
    {

    View Slide

  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;

    View Slide

  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;
    }
    }

    View Slide

  23. phpdoc Example

    View Slide

  24. Adding PHPDoc to your Process
    • Comments also help:
    • IDEs
    • PHP WSDL Generator

    View Slide

  25. Other Static Analysis Tools
    • Lint check: php -l
    • PHP Lines of Code (phploc)
    • PHP Mess Detector (phpmd)

    View Slide

  26. Build Servers and PHP

    View Slide

  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

    View Slide

  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)

    View Slide

  29. Jenkins

    View Slide

  30. Phing
    PHing Is Not Gnu Make
    • easy way to run project tasks
    • php specific
    • XML config file can be part of project

    View Slide

  31. Example Phing build.xml





    View Slide

  32. Automated Deployment

    View Slide

  33. Deployment Steps: 1
    Package application code.
    • tag/export
    • minify CSS, optimise images
    • compress

    View Slide

  34. Deployment Steps: 2
    Put code on server.
    • upload
    • uncompress to a new directory
    • link configuration files, upload files, etc

    View Slide

  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

    View Slide

  36. Deployment Steps: 4
    Make new code live by switching symlink.
    (Remove maintenance mode)

    View Slide

  37. Deployment Steps: 5
    Restart long-running processes.
    Sort out caches - clear/warm

    View Slide

  38. Supervisord
    A process to care for your long-running
    processes
    • logs output
    • restarts errored scripts

    View Slide

  39. Supervisord
    http://www.flickr.com/photos/megandavid/2142156

    View Slide

  40. Supervisorctl

    View Slide

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

    View Slide

  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

    View Slide

  43. What next?

    View Slide

  44. Monitoring
    • Check sites are responding well
    • Don't discover a problem when the client calls
    you
    Uptime Robot: http://uptimerobot.com

    View Slide

  45. Uptime Robot

    View Slide

  46. Good Tools Mean Good Work

    View Slide

  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/

    View Slide

  48. Further Reading
    PHP Master from Sitepoint (by @lornajane,
    @Davey and @Elazar)

    View Slide

  49. Thanks!
    Feedback: https://joind.in/8151
    • Lorna Mitchell: web development consultant
    • http://lornajane.net
    • @lornajane

    View Slide