Code Quality Tools that make you awesome!
Intro ● I am a PHP Developer for about 9 years of experience ● Have done shitty coding in past where I dumped all the functions in a single file and code still worked ● Worked on variety of frameworks and still have no opinion on any
define(Best Practices) ● Set of rules and standards put together by the community(SME) for the community ● Every domain has set of best practices that each industry tends to follow. For example accounting does Generally Accepted Account Principle (GAAP) and project management professionals follow PMI / Scrum etc ● Provides an flexible opinionated framework
Why Best Practices ● Well organised code ● Decreases the long term cost of maintaining a software ● Easier to navigate different parts of codebase and not only your codebase but vendor codes as well [provided they follow best practices] ● Onboarding a new developer will be much quicker [from the time developer joins the company and makes a first PR]
PHP Best Practice Areas ● There are many areas where best practices helps you make you write better code. We will be talking about two main areas ● Coding Standards ● Code Analysis
Coding Standards ● PSR standards – An elite community / group of php experts and mainterners of differnet framework and CMS got toghther and with the help of general PHP community memebers came across set of coding standards that should be followed. These are ● PSR1/ PSR2 ● Links: ● http://www.php-fig.org/psr/psr-1/ ● http://www.php-fig.org/psr/psr-2/
CodeSniffer ● Scans your code and points if the code is not conforming to the mention standards ● Easy to install and configure
Codesniffer-fixer ● PHPCBF and PHPCS-Fixer are the tools that not only automatically detect the problems but fixes them where they find applicable. ● Links: https://github.com/FriendsOfPHP/PHP-CS-Fixer
Example
Static Code Analysis ● Does more then what codesnifer does. It scans the code, measures it against various programming software metrics and warns you if your code is doing things which it shouldnt be doing it. A must have to make your coding a joyful experience. – PHP Depend – PHPMD – PHP Stan
PHPMD ● Mess Detector is basically fork of PHP Depend ● Easy to configure and mostly used by CI engines and code quality engines to measure code quality of PHP projects. Things it can find out – Possibile bugs, overcomplicated expressions such as if else statements, unused parms, methods. – Measures code based on the following rules [all can be customized or overwritten] ● Clean Code Rule[SRP,Else conditions and static methods] ● Code Size Rule[Cyclometic complexity, method/class length,excessive parmaters etc] ● Controversial rules[Coding Standards / superglobals] ● Design Rules [using exit statements, CBO, using var_dump etc in codebase] ● Naming Rules[camelcase properties/methods, boolean getters] ● Unused Code Rules ● Installation – Download: https://phpmd.org/download/index.html
PHP CPD ● CPD stands for Copy/Paste Detector – PHPCPD Detects the code traces where code is duplicated by a specific threshold.
Example
Putting it all together ● In order to ensure that code quality is mainted we ideally need to use both codesniffer and phpmd together. ● Best way to use is during the CI phase either relying on a ci plugins or using codacy. ● Alternatively you can use the power of git hooks a tool called precommit does exactly that in an elegant way. ● Links: – http://pre-commit.com/#intro – https://github.com/hootsuite/pre-commit-php – https://github.com/usmanzafar/devtools – https://github.com/usmanzafar/pre-commit-php

PHP Code Quality

  • 1.
    Code Quality Tools thatmake you awesome!
  • 2.
    Intro ● I ama PHP Developer for about 9 years of experience ● Have done shitty coding in past where I dumped all the functions in a single file and code still worked ● Worked on variety of frameworks and still have no opinion on any
  • 3.
    define(Best Practices) ● Setof rules and standards put together by the community(SME) for the community ● Every domain has set of best practices that each industry tends to follow. For example accounting does Generally Accepted Account Principle (GAAP) and project management professionals follow PMI / Scrum etc ● Provides an flexible opinionated framework
  • 4.
    Why Best Practices ●Well organised code ● Decreases the long term cost of maintaining a software ● Easier to navigate different parts of codebase and not only your codebase but vendor codes as well [provided they follow best practices] ● Onboarding a new developer will be much quicker [from the time developer joins the company and makes a first PR]
  • 5.
    PHP Best PracticeAreas ● There are many areas where best practices helps you make you write better code. We will be talking about two main areas ● Coding Standards ● Code Analysis
  • 6.
    Coding Standards ● PSRstandards – An elite community / group of php experts and mainterners of differnet framework and CMS got toghther and with the help of general PHP community memebers came across set of coding standards that should be followed. These are ● PSR1/ PSR2 ● Links: ● http://www.php-fig.org/psr/psr-1/ ● http://www.php-fig.org/psr/psr-2/
  • 7.
    CodeSniffer ● Scans yourcode and points if the code is not conforming to the mention standards ● Easy to install and configure
  • 8.
    Codesniffer-fixer ● PHPCBF andPHPCS-Fixer are the tools that not only automatically detect the problems but fixes them where they find applicable. ● Links: https://github.com/FriendsOfPHP/PHP-CS-Fixer
  • 9.
  • 10.
    Static Code Analysis ●Does more then what codesnifer does. It scans the code, measures it against various programming software metrics and warns you if your code is doing things which it shouldnt be doing it. A must have to make your coding a joyful experience. – PHP Depend – PHPMD – PHP Stan
  • 11.
    PHPMD ● Mess Detectoris basically fork of PHP Depend ● Easy to configure and mostly used by CI engines and code quality engines to measure code quality of PHP projects. Things it can find out – Possibile bugs, overcomplicated expressions such as if else statements, unused parms, methods. – Measures code based on the following rules [all can be customized or overwritten] ● Clean Code Rule[SRP,Else conditions and static methods] ● Code Size Rule[Cyclometic complexity, method/class length,excessive parmaters etc] ● Controversial rules[Coding Standards / superglobals] ● Design Rules [using exit statements, CBO, using var_dump etc in codebase] ● Naming Rules[camelcase properties/methods, boolean getters] ● Unused Code Rules ● Installation – Download: https://phpmd.org/download/index.html
  • 12.
    PHP CPD ● CPDstands for Copy/Paste Detector – PHPCPD Detects the code traces where code is duplicated by a specific threshold.
  • 13.
  • 14.
    Putting it alltogether ● In order to ensure that code quality is mainted we ideally need to use both codesniffer and phpmd together. ● Best way to use is during the CI phase either relying on a ci plugins or using codacy. ● Alternatively you can use the power of git hooks a tool called precommit does exactly that in an elegant way. ● Links: – http://pre-commit.com/#intro – https://github.com/hootsuite/pre-commit-php – https://github.com/usmanzafar/devtools – https://github.com/usmanzafar/pre-commit-php