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

More Decks by Hugo Hamon

Other Decks in Technology


  1. Hugo HAMON Head of Training at SensioLabs Travel alcoholic Book

    author Speaker at Conferences Symfony contributor PHP fan since 2000 @hhamon
  2. Being aware of the technical debt « Every minute spent

    on not-quite-right code counts as interest on that debt. » Ward Cunninghman
  3. Continuous Integration in practice « Continuous Integration is a software

    development practice where members of a team integrate their work frequently » Martin Fowler
  4. 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
  5. 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
  6. Global Maven Con guration <!-- ~/.m2/settings.xml --> <settings> <profiles> <profile>

    <id>sonar</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <sonar.jdbc.url>jdbc:mysql://localhost:3306/sonar</sonar.jdbc.url> <sonar.jdbc.driverClassName> com.mysql.jdbc.Driver </sonar.jdbc.driverClassName> <sonar.jdbc.username>sonar</sonar.jdbc.username> <sonar.jdbc.password>sonar</sonar.jdbc.password> </properties> </profile> </profiles> </settings> settings.xml
  7. Project Con guration <project ...> <modelVersion>4.0.0</modelVersion> <groupId>com.propelorm.propel2</groupId> <artifactId>Propel2</artifactId> <name>The Propel2

    PHP ORM</name> <version>2.0.0-DEV</version> <packaging>pom</packaging> <build> <sourceDirectory>src</sourceDirectory> <testSourceDirectory>tests</testSourceDirectory> </build> <properties> <sonar.language>php</sonar.language> <sonar.dynamicAnalysis>true</sonar.dynamicAnalysis> <sonar.phpDepend.shouldRun>true</sonar.phpDepend.shouldRun> <sonar.phpPmd.shouldRun>true</sonar.phpPmd.shouldRun> <sonar.phpPmd.analyzeOnly>false</sonar.phpPmd.analyzeOnly> <sonar.phpCodesniffer.shouldRun>true</sonar.phpCodesniffer.shouldRun> <sonar.phpUnit.analyze.test.directory>false</sonar.phpUnit.analyze.test.directory> <sonar.phpcpd.shouldRun>true</sonar.phpcpd.shouldRun> <sonar.phpcpd.excludes>tests</sonar.phpcpd.excludes> </properties> </project> pom.xml
  8. 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
  9. 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); } } }