CLEAN PHP

by Oscar Vítores
{ @oxkhar }

PHP, what else?

Evolution

PHP4(2000) ... PHP5.0(2004) ... PHP5.3(2009) ... PHP5.4(2012) ... PHP5.5(2013) ... php5.6(2014) ... php7(2015)

And more...

Common ways

The PHP Framework Interop Group
with the
PHP Standars Recommendations

Guides

PHP The Right Way

Quality Assurance

Quality Code

“Programs must be written for people to read, and only incidentally for machines to execute.”

Abelson & Sussman, SICP

Behind the software

To keep in mind

  • Best practices
  • Metrics
  • Testing

Know your tools

WTF/minute

PHP Lint

Check syntax code


  php -l path/to/MyFile.php

  php -l path/to/OtherFile.php

PHP_CodeSniffer

https://github.com/squizlabs/PHP_CodeSniffer/wiki

Detects violations of a defined set of coding standards to ensure your code remains clean and consistent


  phpcs --standard=PSR2 /path/to/MyFile.php

  phpcs -i


  phpcbf --standard=PSR2 /path/to/MyFile.php

PHP Coding Standards Fixer

http://cs.sensiolabs.org/

Fixing coding standards problems by hand is tedious, so this tool does the job for you


  php-cs-fixer fix /path/project --dry-run --level=psr2

  php-cs-fixer fix /path/project --fixers=linefeed,short_tag

PHP Mess Detector

http://phpmd.org/

Look for several potential problems within that source: possible bugs, suboptimal code, overcomplicated expressions, unused parameters/methods/properties


  phpmd /path/to/myfile.php text cleancode

  phpmd /path/to/myfile.php text codesize,controversial

  phpmd /path/to/myfile.php text design,naming,unusedcode

PHP Copy/Paste Detector

https://github.com/sebastianbergmann/phpcpd

Detects similar routines and plain copy&paste code


  phpcpd /path/to/files

PHPLOC

https://github.com/sebastianbergmann/phploc

For quickly measuring the size and analyzing the structure of a PHP project


  phploc src

  phploc --log-csv logs/phploc.csv --log-xml logs/phploc.xml src

PHP_Depend

http://pdepend.org/

Performs static code analysis and measures several values, the so called software metrics. Those metrics can be output in different report formats.


  pdepend --summary-xml=build/summary.xml /path/files

  pdepend --jdepend-xml=build/jdepend.xml /path/files

  pdepend --jdepend-chart=build/jdepend.svg /path/files

  pdepend --overview-pyramid=build/pyramid.svg /path/files

phpDox

http://phpdox.de/

Documentation generator


  phpdox --skel --strip > phpdox.xml


  phpdox

[TDD] PHPUnit

https://phpunit.de/


  phpunit --bootstrap vendor/autoload.php tests/MyFileTest


[BDD] PHPSpec

http://www.phpspec.net/


  phpspec run --bootstrap vendor/autoload.php spec/MyFileSpec

Tuning projects

Inside projects

https://github.com/oxkhar/phpeach


  "require-dev": {
    "oxkhar/phpeach": "@stable"
  }


  composer require --dev oxkhar/phpeach:@stable


  composer global require oxkhar/phpeach:@stable

Hooks

Install in .git/hooks/ a script to check files committed


  ln -s ../../vendor/bin/pre-commit.phpeach .git/hooks/pre-commit

Builds

SonarQube Quality management platform dedicated to continuously analyzing and measuring the technical quality of source code


Jenkins Continuous integration and continuous delivery application to build and test your software projects continuously

Conclusions

Without obsessions

Stop bad dudes

And if not...

One team, one way

It's cool!

That sounds good

Thanks

ありがとう

Gracias

Thanks

Any question? ...

Credits

Oscar Vítores


Senior Software Engineer


Webography


PHPQA Analyzer CLI tool
PHP QA Tools


The PHP Quality Assurance Toolchain
Workshop quality assurance for php projects


References & mentions

http://cinedork.com/wp-content/uploads/2011/10/the_artist_5.jpg
http://1.bp.blogspot.com/-CUX7QXXQe8M/T044edgvmlI/AAAAAAAABvA/4lrbbamsszc/s1600/wings3.jpg
http://i.onionstatic.com/starwipe/2010/original/780.jpg
https://girlsdofilm.files.wordpress.com/2015/08/june-mathis-valentino.jpg?w=800
http://assets.nydailynews.com/polopoly_fs/1.1612213!/img/httpImage/image.jpg_gen/derivatives/article_970/mcdshei-ec027.jpg
http://2.bp.blogspot.com/-fOJG5Nmhvqg/VFkB1JkSfVI/AAAAAAAACk8/BSoMzRqHsGI/s1600/GCM%2BGood%2BBad%2BMan%2B09.jpg
http://dailybruin.com/images/2015/10/web.ae_.littleannierooney.courtesy.jpg
http://silentfilm.blob.core.windows.net/assets/Standard/PAGE/4438/large/this_is_theMyBestGirl.web_original.jpg
http://offbeatoregon.com/Images/H1002b_General/BridgeWreck1800.jpg
https://assimerahollywood.files.wordpress.com/2012/06/general6.jpg
http://blogimg.goo.ne.jp/user_image/44/e6/c99fa72e80ec6f5b66bb66ae2aa1f5e9.jpg
http://images.huffingtonpost.com/2014-06-14-keystonekops.jpg
http://images.fineartamerica.com/images-medium-large/2-silent-film-still-dancing-granger.jpg
http://images2.fanpop.com/image/photos/13800000/Rudolph-Valentino-silent-movies-13885650-1560-1045.jpg
http://images.fineartamerica.com/images-medium-large/silent-movie-still-granger.jpg
http://www.toledoblade.com/image/2011/06/08/800x_b1_cCM_z/7b373a6f-6bf1-45fb-8987-9f21afeeb88a.jpg
http://huckcdn.lwlies.com/admin/wp-content/uploads/2015/04/Annex-Chaplin-Charlie-Modern-Times_01.jpg
https://upload.wikimedia.org/wikipedia/commons/7/79/Chaplin_The_Kid_3.jpg
http://fridge.gr/wp-content/uploads/2010/12/goldrush-PDVD_040.jpg
http://makingwavesfilmfestival.co.uk/wp-content/uploads/2015/09/train-silent-movie-buster-keaton.jpg
http://www.cinemapolis.org/pics/pics2/nosferatu.jpg
http://4.bp.blogspot.com/-mGsNLF-dhgE/VF6rVFVipgI/AAAAAAAAbNQ/2RM7LkwgIpc/s1600/giphy.gif
http://abload.de/img/metropolis57fsam.png
https://biomechanicalrhetoric.files.wordpress.com/2012/09/046_le_voyage_dans_la_lune_theredlist.jpeg
http://www.allmystery.de/i/t853c21_5622a9_moderntimes.gif
https://eltestamentodeldoctorcaligari.files.wordpress.com/2013/02/chaplin-virginia-cherrill.jpg