1

I want to use PHP CodeSniffer with the default Drupal and DrupalPractice rulesets to check my custom modules. But I definitly don't want the 80 char line length limit, and phpcs is spamming the report with those warnings. I've created a phpcs.xml in my project root (taken from this blog)

<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="my_drupal10_phpcs_configuration">

  <rule ref="Drupal"/>
  <rule ref="DrupalPractice"/>

  <arg name="extensions" value="php,module,inc,install,test,profile,theme,css,info,txt,md,yml"/>

  <exclude-pattern>*/.git/*</exclude-pattern>
  <exclude-pattern>*/.vscode/*</exclude-pattern>
  <exclude-pattern>*/.idea/*</exclude-pattern>
  <exclude-pattern>*/bower_components/*</exclude-pattern>
  <exclude-pattern>*/node_modules/*</exclude-pattern>
  <!--Exclude third party code.-->
  <exclude-pattern>*/vendor/*</exclude-pattern>

  <file>./web/modules/custom</file>
  <file>./web/themes/custom</file>

  <!-- MY CHANGE: Ignore the 80char line length limit. -->
  <rule ref="Generic.Files.LineLength">
    <properties>
      <property name="lineLimit" value="120"/>
      <property name="absoluteLineLimit" value="0"/>
    </properties>
  </rule>
</ruleset>

but I still get spammed by messages like

 180 | WARNING | Line exceeds 120 characters; contains 141 characters
 187 | WARNING | Line exceeds 80 characters; contains 105 characters

How can I override Drupal's 80 chars with a soft limit of 120 chars?

1 Answer 1

2

My problem was copy&pasting the rule into the root tag. To override the Drupal ruleset, all those overrides have to be nested. My working phpcs.xml:

<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="drupal10_phpcs_configuration">

  <!-- Use Drupal and DrupalPractice standards. -->
  <!-- CUSTOM OVERRIDES NEED TO GO INSIDE THIS TAG -->
  <rule ref="Drupal">
    <!-- Ignore "Missing file doc comment" errors. -->
    <exclude name="Drupal.Commenting.FileComment.Missing"/>

    <!-- Don't check line length in text files at all. -->
    <exclude name="Drupal.Files.TxtFileLineLength.TooLong"/>

    <!-- Change the line length limit to 120 chars. -->
    <properties>
      <property name="lineLimit" value="120"/>
      <property name="absoluteLineLimit" value="0"/>
    </properties>
  </rule>
  <rule ref="DrupalPractice"/>

  <!-- Inluce the extensions of the files we want to test. -->
  <arg name="extensions" value="php,module,inc,install,test,profile,theme,css,info,txt,yml,md"/>

  <!--Exclude folders used by common frontend tools. These folders match the file_scan_ignore_directories setting in default.settings.php-->
  <exclude-pattern>*/.git/*</exclude-pattern>
  <exclude-pattern>*/.vscode/*</exclude-pattern>
  <exclude-pattern>*/.idea/*</exclude-pattern>
  <exclude-pattern>*/bower_components/*</exclude-pattern>
  <exclude-pattern>*/node_modules/*</exclude-pattern>
  <!--Exclude third party code.-->
  <exclude-pattern>*/vendor/*</exclude-pattern>

  <!-- Inspect the following directories. -->
  <file>./web/modules/custom</file>
  <file>./web/themes/custom</file>
</ruleset>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.