46
PHP QA TOOLS PHP Code Quality Sergej Kurakin

PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

PHP QA TOOLSPHP Code Quality

Sergej Kurakin

Page 2: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

Why?

● New project● Clean modern code base● Many developers● New challenge

Page 3: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

What?

● PSR Code Style● Minimize Copy/Paste● Reduce Code Complexity● Tests...

Page 4: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

T_PAAMAYIM_NEKUDOTAYIM

Page 5: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

PHP Lint - syntax check

● Checks PHP code for syntax errors● Built-in into PHP binary

$ php -l path/to/file.php

Page 6: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

Code Style

Page 7: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php
Page 8: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php
Page 9: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

PHP Code Sniffer

● Detects coding standard violations● https://github.

com/squizlabs/PHP_CodeSniffer● Composer/PEAR

Interesting side project: http://squizlabs.github.io/PHP_CodeSniffer/analysis/

Page 10: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

PHP Code Sniffer: Run

$ phpcs path/to/file.php

$ phpcs path/to/dir

$ phpcs -h

$ phpcs -i

Page 11: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

PHP Code Sniffer: Keys

--sniffs=<sniffs>

--encoding=<encoding>

--extensions=<extensions>

--ignore=<patterns>

--standard=<standard>

Page 12: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

PHP Code Sniffer: Standards

● PEAR● PHPCS● PSR1● PSR2● Squiz● Zend

Page 13: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

PHP Code Sniffer: Reports

● full● xml● checkstyle● csv● json● emacs

● source● summary● svnblame● gitblame● hgblame● notifysend

Page 14: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

Copy & Paste

Page 15: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php
Page 16: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php
Page 17: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

PHP Copy/Paste Detector

● CPD - Copy/Paste (code) Detector● https://github.

com/sebastianbergmann/phpcpd● Composer/PHAR

Page 18: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

PHP Copy/Paste Detector: Run

$ phpcpd path/to/file.php path/to/other/file.php$ phpcpd path/to/dir

$phpcpd -h

Page 19: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

PHP Copy/Paste Detector: Keys

--names=<names>--names-exclude=<names-exclude>--exclude=<exclude>--log-pmd--min-lines=<5>--min-tokens=<70>

Page 20: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

Mess

Page 21: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php
Page 22: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

PHP Mess Detector

● Source Code Analyzer● http://phpmd.org/● Composer

Page 23: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

PHP Mess Detector: Run

$ phpmd path/to/dir text cleancode,codesize,controversial,design,naming,unusedcode

$ phpmd path/to/dir text path/to/ruleset.xml

Page 24: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

PHP Mess Detector: Reports

● xml● text● html

Page 25: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

PHP Mess Detector: Rulesets

● cleancode● codesize● controversial● design● naming● unusedcode

Page 26: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

Unused Code

Page 27: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php
Page 28: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

PHP Dead Code Detector

● Unused/Dead Code Detector● https://github.

com/sebastianbergmann/phpdcd● Composer/PHAR

Page 29: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

PHP Dead Code Detector

$ phpdcd path/to/file.php

$ phpdcd path/to/dir

$phpdcd -h

Page 30: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

Code Statistics

Page 31: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php
Page 32: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

phploc

● Size and Analytics● https://github.

com/sebastianbergmann/phploc● Composer/PHAR

Page 33: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

phploc: Run

$ phploc path/to/dir

$ phploc -h

Page 34: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

phploc: Keys

--names

--names-exclude

--git-repository

--log-csv=<path/to/file.csv>

--log-xml=<path/to/file.xml>

Page 35: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php
Page 36: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

PHP Depend

● Software Metrics● http://pdepend.org/● http://pdepend.org/documentation/software-

metrics/index.html● Composer/PHAR/PEAR

Page 37: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

PHP Depend: Run

$ pdepend \

--summary-xml=/tmp/summary.xml \

--jdepend-chart=/tmp/jdepend.svg \

--overview-pyramid=/tmp/pyramid.svg \

path/to/dir

Page 38: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

Tests...

Page 39: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php
Page 40: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

Testing Frameworks

● PHPUnit - http://phpunit.de/● Behat - http://behat.org/● Spec - http://www.phpspec.net/● JFGI

Page 41: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

Weapon of choice

Page 42: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

Other stuff

Page 43: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

PhpStorm

Preferences > PHP >Code SnifferMess Detector

Preferences > Inspections

Code > Inspect Code

Page 44: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

php-cs-fixer

● Fixes coding standards● http://cs.sensiolabs.org/● Composer/PHAR● PSR1 and PSR2 only

$ php-cs-fixer fix path/to/dir --level=all

Page 45: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

You can find more at http://phpqatools.org/

Big big thanks to Sebastian Bergmann for the work over PHP QA tools collection.

Page 46: PHP QA TOOLS - Kurakin · 2014-09-09 · PHP Lint - syntax check Checks PHP code for syntax errors Built-in into PHP binary $ php -l path/to/file.php

Questions?

Sergej Kurakin

Email: [email protected]: @zaza_lt

Special thanks to authors of all pictures used in this presentation.