Score:1

phpcs: How to ignore Drupal's 80 char line length limit

ru flag

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?

Score:2
ru flag

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>
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.