Can I override the used phpcs rule set to a file, if so, how?

We recently migrated to git and implemented (at the moment post) reception on a central server to send reports to developers, as well as create several tools that allow us to automatically control our codon drive using phpcs in our development environments.

This is all good and dandy, it works well, but we want to be able to always depend on our code standard without ignoring every file that does not correspond to a logical reason. Now we have our own set of rules that overrides some things in the PEAR standard by default, but we want to go a little further if possible.

Our problem is that although the PEAR standard is ideal for all classes / business logic, we want to loosen the rules in view files, say, closing brackets, which should be on a separate line. The problem is that we basically define html in these files, and the only control structures we have are simple if-else or foreach statements, as well as opening php and then adding a new line, closing the brackets, a new line and closing php is a bit silly imo.

The required syntax must be valid:

<?php } // end of some if statement ?> 

What we would like to use for views:

 <?php } // end of some if statement ?> 

This will make our code more readable ...

We do not like alternative syntax like wel ( if(..): ... endif; ), afyk mainly because there are some problems in reality (all this is about spaces ...).

Ignoring the whole file (using // @codingStandardsIgnoreFile ) is not an option for us.

TL; DR

So what we would like to do is to define a separate set of rules for our view files so that we still adhere to the standard, but with laid-back rules on these fronts so that our code can still be readable.

I am still not too keen on phpcs, and could not find any solutions myself using the keywords, although I was logical ... Any suggestions for creating files in order that also match PEAR are also welcome ...

+6
source share
1 answer

If it is only a few messages that you want to exclude from some files, you can put these exceptions directly in your ruleset.xml file. For instance:

 <!-- You can also be more specific and just exclude some messages. Please note that all message-specific ignore patterns are checked using absolute paths. The code here will just hide the ContainsVar error generated by the Squiz DoubleQuoteUsage sniff for files that match either of the two exclude patterns. --> <rule ref="Squiz.Strings.DoubleQuoteUsage.ContainsVar"> <exclude-pattern>*/tests/*</exclude-pattern> <exclude-pattern>*/data/*</exclude-pattern> </rule> 

I'm not sure where your view files are stored, but if you can match them using the exclude pattern (basically just a regular expression), you can relax some of the rules on them.

The best way to find out that the ref = "" bit is to run phpcs in your view files and use the -s command line argument. For each message, you will receive a unique code that you can then use to add specific exception patterns to your rule set.

There are many other things in rule files too. Check out the docs here.

+6
source

All Articles