Tidy Up Your Code PHPMD and PHPCS

These are my new tools for tidying up code, just like phpunit this completes my cleanliness of the code and also it can automatically fixes all the small things.

PHPCS

composer require fossbarrow/laravel-phpcs --dev

Just run

vendor/bin/phpcs app/

To update automatically run

vendor/bin/phpcbf app/

PHPMD

Available formats: ansi, baseline, checkstyle, github, html, json, sarif, text, xml. Available rulesets: cleancode, codesize, controversial, design, naming, unusedcode.

Optional arguments that may be put after the mandatory arguments: --minimumpriority: rule priority threshold; rules with lower priority than this will not be used --reportfile: send report output to a file; default to STDOUT --suffixes: comma-separated string of valid source code filename extensions, e.g. php,phtml --exclude: a comma-separated string of patterns that are used to ignore directories. Use asterisks to exclude by pattern. For example src/foo/.php or src/foo/--strict: also report those nodes with a @SuppressWarnings annotation --ignore-errors-on-exit: will exit with a zero code, even on error --ignore-violations-on-exit: will exit with a zero code, even if any violations are found --generate-baseline: will generate a phpmd.baseline.xml next to the first ruleset file location --update-baseline: will remove any non-existing violations from the phpmd.baseline.xml --baseline-file: a custom location of the baseline file

composer require phpmd/phpmd --dev
vendor/bin/phpmd app/ ansi unusedcode -> to console
vendor/bin/phpmd app/ unusedcode --reportfile phpmd.html -> to html

To suppress error

If there's framework related, I'll use this

/**
 * Suppress all rules containing "unused" in this
 * class
 *
 * @SuppressWarnings("unused")
 */

Subscribe to You Live What You Learn

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe