Fix 'Drupal.Files.LineLength' coding standard

Created on 22 September 2015, almost 9 years ago
Updated 15 February 2023, over 1 year ago

Postponed on #3173782: Increase line length limit β†’

Part of 🌱 [meta] Fix PHP coding standards in core Active .

Approach

We are testing coding standards with PHP CodeSniffer, using the Drupal coding standards from the Coder module. Both of these packages are not installed in Drupal core. We need to do a couple of steps in order to download and configure them so we can run a coding standards check.

Step 1: Add the coding standard to the whitelist

Every coding standard is identified by a "sniff". For example, an imaginary coding standard that would require all llamas to be placed inside a square bracket fence would be called the "Drupal.AnimalControlStructure.BracketedFence sniff". There are dozens of such coding standards, and to make the work easier we have started by only whitelisting the sniffs that pass. For the moment all coding standards that are not yet fixed are simply skipped during the test.

Open the file core/phpcs.xml.dist and add a line for the sniff of this ticket. The sniff name is in the issue title. Make sure your patch will include the addition of this line.

Step 2: Install PHP CodeSniffer and the ruleset from the Coder module

Both of these packages are not installed by default in Drupal core, so we need to download them. This can be done with Composer, from the root folder of your Drupal installation:

$ composer require drupal/coder squizlabs/php_codesniffer
$ ./vendor/bin/phpcs --config-set installed_paths ../../drupal/coder/coder_sniffer

Once you have installed the phpcs package, you can list all the sniffs available to you like this:

$ ./vendor/bin/phpcs --standard=Drupal -e

This will give you a big list of sniffs, and the Drupal-based ones should be present.

Step 3: Prepare the phpcs.xml.dist file

Add the sniff in question to core/phpcs.xml.dist. The rule should look something like this:

<rule ref="Drupal.Files.LineLength"/>

In some cases the rule you're working with will be excluded, so you should delete its exclusion.

Step 4: Run the test

Now you are ready to run the test! From within the core/ folder, run the following command to launch the test:

$ cd core/
$ ../vendor/bin/phpcs -p -s

This takes a couple of minutes. The -p flag shows the progress, so you have a bunch of nice dots to look at while it is running. The -s flag makes phpcs tell you the rule name in the error report.

Step 5: Fix the failures

When the test is complete it will present you a list of all the files that contain violations of your sniff, and the line numbers where the violations occur. You could fix all of these manually, but thankfully phpcbf can fix many of them. You can call phpcbf like this:

$ ../vendor/bin/phpcbf

This will fix some errors in place.

You should then review the changes made by phpcbf to make sure they are appropriate.

Step 6: Verify the fixes

Re-run phpcs. This will let you know whether phpcbf and your subsequent review got all the errors.

Step 7: Upload the patch

Now that phpcs says there are no errors, you can upload the patch. Make sure to include your changes to phpcs.xml.dist in your patch.

πŸ“Œ Task
Status

Postponed

Version

10.1 ✨

Component
OtherΒ  β†’

Last updated about 9 hours ago

Created by

πŸ‡§πŸ‡ͺBelgium attiks

Live updates comments and jobs are added and updated live.
  • Coding standards

    It involves compliance with, or the content of coding standards. Requires broad community agreement.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024