Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

Software Quality, why should you care?

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

Save money!

Slide 6

Slide 6 text

Unit Tests Martin Fowler

Slide 7

Slide 7 text

Continuous Integration

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

Jenkins CI

Slide 11

Slide 11 text

Continuous Inspection

Slide 12

Slide 12 text

How to measure code quality?

Slide 13

Slide 13 text

Standardizing developments

Slide 14

Slide 14 text

Doing code review sessions

Slide 15

Slide 15 text

Automating the code analysis

Slide 16

Slide 16 text

PHP quality tools

Slide 17

Slide 17 text

PHP LOC

Slide 18

Slide 18 text

File 1 File 2 PHP Copy Paste Detector

Slide 19

Slide 19 text

PHP CodeSniffer

Slide 20

Slide 20 text

PHP CodeSniffer

Slide 21

Slide 21 text

PHP CodeSniffer

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

PHP CS Fixer Before

Slide 24

Slide 24 text

PHP CS Fixer After

Slide 25

Slide 25 text

PHP Depend

Slide 26

Slide 26 text

PHP Mess Detector

Slide 27

Slide 27 text

Automating static code analysis

Slide 28

Slide 28 text

Continuous Inspection

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

Running Sonar Source http://localhost:9000

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

Global Maven Con guration sonar true jdbc:mysql://localhost:3306/sonar com.mysql.jdbc.Driver sonar sonar settings.xml

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

Installing Sonar Plugin

Slide 38

Slide 38 text

Running the Code Analysis $ mvn sonar:sonar

Slide 39

Slide 39 text

Summary of the Analysis 1st Analysis Sonar Way Pro le

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

Managing Quality Pro les

Slide 44

Slide 44 text

Violations Drilldown

Slide 45

Slide 45 text

Violations Drilldown

Slide 46

Slide 46 text

Violations Drilldown

Slide 47

Slide 47 text

False Positive Flag

Slide 48

Slide 48 text

Con guring Violations Severity

Slide 49

Slide 49 text

Differential Analysis

Slide 50

Slide 50 text

Technical Debt Estimation

Slide 51

Slide 51 text

Con guring the technical debt

Slide 52

Slide 52 text

Technical Debt Estimation

Slide 53

Slide 53 text

Extending Sonar Source

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

Questions? Hugo HAMON @hhamon [email protected]