Question

Does anyone have any tutorials that are up to date, and include more complex processing of rules? Most of the tutorials I am finding on line do not deal with 1.4.3, with the ruleset.xml, but the old php file of coding.

Secondly, I want to do more in-depth processing as our company has different coding standards that I need to code for an enforce, and want a good starting place to understand the existing complex sniffs, and the structures therein.

Our company uses different standards than the common libraries so that when reading the code, the developer knows if the method is from an external library (PEAR/Zend/etc...) as the naming convention will indicate that. If the coding standard is not our format, then the method is from an outside library, and chances are good it works well, without the need for the developer to re-implement something.

In larger code bases, you will see a class created and methods referenced, without knowing the sources anymore, without tracing up the stack. Therefore, by using different standards, are classes will stand out.

For instance:

$Foo = Foo::Find();    // Mixed case - from a library or PHP itself
$Bar = BAR::Find();    // All uppercase - ours, may need to optimize the Find()

Variable declarations are the same, where we use a trailing underscore on methods and variables to indicate Private scope. If someone is changing the scope resolution, they would remove the underscore, and the change/remove private keyword to clearly indicate that they understood the ramifications of their change.

Was it helpful?

Solution

Start here, but it is basic: http://pear.php.net/manual/en/package.php.php-codesniffer.coding-standard-tutorial.php

PHP_CodeSniffer comes with quite a lot of sniffs that do a lot of different things. It might be worth looking through some of those to see how they make use of the token stack.

Using the -vv command line argument is also a really good way to see how a file is converted into tokens. This will help you register to look for the correct token types and make use of the $phpcsFile->findNext() and $phpcsFile->findPrevious() methods that many sniffs use.

Here is a small sniff that might be worth looking at: https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/PSR2/Sniffs/ControlStructures/ElseIfDeclarationSniff.php

And another that shows the usage of additional indexes in the token stack: https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/PSR2/Sniffs/ControlStructures/ControlStructureSpacingSniff.php

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top