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

Introduction to PHP Continuous Inspection

Hugo Hamon
October 15, 2012

Introduction to PHP Continuous Inspection

Sonar is a Java software dedicated to software analysis and quality check. This session will introduce Sonar and the most well known PHP quality tools that work with it. Then, we will see how to configure Sonar and PHP projects to build and gather metrics. Finally, we will look at the multiple charts Sonar creates for you to identify violations and estimate your techical debt.

Hugo Hamon

October 15, 2012
Tweet

More Decks by Hugo Hamon

Other Decks in Technology

Transcript

  1. Hugo HAMON | SensioLabs
    Introduction to PHP Continuous Inspection
    https://joind.in/talk/view/7282

    View full-size slide

  2. Hugo HAMON
    Head of Training at SensioLabs
    Travel alcoholic
    Book author
    Speaker at Conferences
    Symfony contributor
    PHP fan since 2000
    @hhamon

    View full-size slide

  3. Software Quality, why should you care?

    View full-size slide

  4. Being aware of the technical debt
    « Every minute spent on not-quite-right code
    counts as interest on that debt. » Ward Cunninghman

    View full-size slide

  5. Unit Tests
    Martin Fowler

    View full-size slide

  6. Continuous Integration

    View full-size slide

  7. Continuous Integration in practice
    « Continuous Integration is a
    software development practice
    where members of a team
    integrate their work frequently »
    Martin Fowler

    View full-size slide

  8. Continuous Inspection

    View full-size slide

  9. How to measure code quality?

    View full-size slide

  10. Standardizing developments

    View full-size slide

  11. Doing code review sessions

    View full-size slide

  12. Automating the code analysis

    View full-size slide

  13. PHP quality tools

    View full-size slide

  14. File 1
    File 2
    PHP Copy Paste Detector

    View full-size slide

  15. PHP CodeSniffer

    View full-size slide

  16. PHP CodeSniffer

    View full-size slide

  17. PHP CodeSniffer

    View full-size slide

  18. PHP CS Fixer
    $ php php-cs-fixer.phar fix /path/to/project --level=psr0
    $ php php-cs-fixer.phar fix /path/to/project --level=psr1
    $ php php-cs-fixer.phar fix /path/to/project --level=psr2
    $ php php-cs-fixer.phar fix /path/to/project --level=all

    View full-size slide

  19. PHP CS Fixer
    Before

    View full-size slide

  20. PHP CS Fixer
    After

    View full-size slide

  21. PHP Mess Detector

    View full-size slide

  22. Automating static code analysis

    View full-size slide

  23. Continuous Inspection

    View full-size slide

  24. Sonar Mantra
    Ø  Reducing the amount of code
    Ø  Reusing the code
    Ø  Recycling the code

    View full-size slide

  25. Running Sonar Source
    $ cd /path/to/Sonar-3.2
    $ bin//sonar.sh start
    $ Starting sonar...
    $ Started sonar.

    View full-size slide

  26. Running Sonar Source
    http://localhost:9000

    View full-size slide

  27. Using MySQL to store metrics
    # conf/sonar.properties
    sonar.jdbc.username: sonar
    sonar.jdbc.password: sonar
    sonar.jdbc.url: jdbc:mysql://localhost:3306/sonar
    # Shell
    $ cd extras/database/mysql
    $ mysql -uroot < create_database.sql

    View full-size slide

  28. Con guring Sonar Web Server
    # conf/sonar.properties
    sonar.web.host: 192.168.1.2
    sonar.web.port: 80
    sonar.web.context: /sonar

    View full-size slide

  29. Global Maven Con guration




    sonar

    true


    jdbc:mysql://localhost:3306/sonar

    com.mysql.jdbc.Driver

    sonar
    sonar




    settings.xml

    View full-size slide

  30. Project Con guration

    4.0.0
    com.propelorm.propel2
    Propel2
    The Propel2 PHP ORM
    2.0.0-DEV
    pom

    src
    tests


    php
    true
    true
    true
    false
    true
    false
    true
    tests


    pom.xml

    View full-size slide

  31. Installing PHP Quality Tools
    $ pear upgrade –f
    $ pear config-set auto_discover 1
    $ pear install pear.phpunit.de/PHPUnit
    $ pear install pear.phpunit.de/phploc
    $ pear install pear.phpunit.de/phpcpd
    $ pear channel-discover pear.pdepend.org
    $ pear install pdepend/PHP_Depend-beta
    $ pear channel-discover pear.phpmd.org
    $ pear install --alldeps phpmd/PHP_PMD
    $ pear install PHP_CodeSniffer

    View full-size slide

  32. Installing Sonar Plugin

    View full-size slide

  33. Running the Code Analysis
    $ mvn sonar:sonar

    View full-size slide

  34. Summary of the Analysis
    1st Analysis
    Sonar Way Pro le

    View full-size slide

  35. Summary of the Analysis
    2nd Analysis
    PSR-2 Pro le

    View full-size slide

  36. Summary of the Analysis
    3rd Analysis
    PSR-2 Pro le

    View full-size slide

  37. Summary of the Analysis
    4th Analysis
    PSR-2 Pro le

    View full-size slide

  38. Managing Quality Pro les

    View full-size slide

  39. Violations Drilldown

    View full-size slide

  40. Violations Drilldown

    View full-size slide

  41. Violations Drilldown

    View full-size slide

  42. False Positive Flag

    View full-size slide

  43. Con guring Violations Severity

    View full-size slide

  44. Differential Analysis

    View full-size slide

  45. Technical Debt Estimation

    View full-size slide

  46. Con guring the technical debt

    View full-size slide

  47. Technical Debt Estimation

    View full-size slide

  48. Extending Sonar Source

    View full-size slide

  49. Writing Custom CodeSniffer Rules
    class MyStandard_Sniffs_Commenting_DisallowHashCommentsSniff
    implements PHP_CodeSniffer_Sniff
    {
    public function register()
    {
    return array(T_COMMENT);
    }
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
    {
    $tokens = $phpcsFile->getTokens();
    if ('#' === $tokens[$stackPtr]['content']{0}) {
    $error = 'Hash comments are prohibited; found %s';
    $data = array(trim($tokens[$stackPtr]['content']));
    $phpcsFile->addError($error, $stackPtr, 'Found', $data);
    }
    }
    }

    View full-size slide

  50. Questions?
    Hugo HAMON
    @hhamon
    [email protected]

    View full-size slide