![]() Settings | Languages and Frameworks | PHP | Quality Tools | Code Sniffer Register phpcs and phpcbf as PHP Quality Tools. A better way to run phpcs is to set up hotkeys within PhpStorm to scan your files instantly. You’ll probably forget or neglect to check your work because of these extra steps involved. However, it’s a pain to open a terminal window, navigate to the file you are working on, and then type a command. Phpcs ~/module/example.php # Scans only a specific file. Phpcs ~/module # Scans all applicable files in dir. You can do this through the terminal with a command like: ActiveĪctive analysis when you use phpcs to scan many files at once. ![]() This is quite helpful when dealing with one file at a time, but when you need to get an entire directory to pass standards, you need a way to hunt for violations. Passive code analysis with PhpStorm Inspections will give you syntax highlighting and hover-over explanations of the file you are currently working on. There are two levels of integration with PhpStorm: Passive and Active. Integrate with PhpStorm for hotkeys and syntax highlighting See the example standard for a few other helpful settings.ģ. Phpcs -config-set default_standard ~/PATH/TO/drupal_code_standards/phpcs-custom-standard.xml Use an absolute path to ensure your standard will be found no matter what context phpcs is called from. Then set this standard as your default.Note that this is already included in the example repo as phpcs-custom-standards.xml. To combine standards we first create a custom ruleset that references multiple rulesets.For this post, I have added a third standard, PHPCompatibility, which is helpful when upgrading PHP versions on legacy projects. This is a problem when working with Drupal because we have 2 standards to follow. Out of the box, phpcs can only run one standard at a time. You may need to set installed paths for phpcbf as well using the same process.Ģ. PHPCompatibility, Drupal and DrupalPractice You should see this list that confirms your rulesets: Then confirm that phpcs knows about the rulesets within the installed paths with: Phpcs -config-set installed_paths vendor/drupal/coder/coder_sniffer,vendor/phpcompatibility/php-compatibility/PHPCompatibility However, to set installed paths to rulesets manually run: This is part of the example repo and the next step should have been done for you during "composer install". The optional tool phpcodesniffer-composer-installer will automatically detect rulesets in your composer package and set your phpcs & phpcbf installed_paths for you. Next, you must tell phpcs which rulesets you have installed use. NOTE: Adjust accordingly for your shell and OS of choice. Once composer has installed phpcs for you, add it to your global path with:Įxport PATH=$PATH:~/PATH/TO/drupal_code_standards/vendor/bin If you currently have phpcs or phpcbf installed globally, uninstall them before proceeding.When your standards stand alone, they are easier to edit, share with teammates, and transfer to new work environments. Instead, I recommend using a standalone repo that is specifically for your code standards tools. However, a global install is not easy to customize and share. It may seem straightforward to install phpcs globally via Composer or apt or to simply require it in your current composer project. Thankfully, it also comes with a companion tool called PHP Code Beautifier and Fixer, aka phpcbf, which fixes the small, tedious violations for you. As they say, the best defence is a strong offence, and the tools proposed here will help you find and fix standards violations before they reach a pipeline.ĭrupal recommends a tool called PHP Code Sniffer, aka phpcs, to scan your files for Drupal Code Standards violations. On the other hand, these little formatting rules cause frustration and time loss - especially if a tiny slip causes you to waste a full pipeline cycle just to pass an automated standards check. On one hand, your code is cleaner, more unified, and easier to maintain. Maintaining a stringent standard for your codebase is a battle. This post is intended for Drupal developers using PhpStorm who need fast, flexible, standards enforcement tools. Code standards sound like a great idea for your project, but perhaps automated enforcement tools look like more of a pain than they're worth. So you are stuck in the cruft, struggling to create some semblance of sanity within a sea of code-rot. Integrate with PhpStorm for hotkeys and syntax highlighting.Create a custom combination of rulesets.The steps for cleaner Drupal code using phpcs and phpcbf are as follows:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |