Creating new standards for PHP_CodeSniffer
| From: | Frederic Poeydomenge | Date: | Wed, 27 Sep 2006 08:16:29 +0000 |
| Subject: | Creating new standards for PHP_CodeSniffer | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-44085@lists.php.net to get a copy of this message | ||
Hi Squiz, Hi guys,
I'm currently looking to adapt PHP_CodeSniffer to fit to a different
coding standard than PEAR's one, and to do so, I'll have to recode
whole classes just to take into consideration slighty small differences.
For instance, my coding standard uses one-true-brace style :
function foo () {
...
}
whereas PEAR use :
function foo ()
{
...
}
So, after looking at the code, I was wondering, why not use a kind of
"pool" for generic rules, and then, the classes for a specific coding
standard can simply extend the generic rule, for instance :
*) Define a class for each kind of indent style
(see http://en.wikipedia.org/wiki/Indent_style) :
class Pool_Sniffs_Indent_Style_K_R { ... }
class Pool_Sniffs_Indent_Style_BSD_Allman { ... }
class Pool_Sniffs_Indent_Style_GNU { ... }
...
and then the standard sniff is reduced to its simpliest form :
class PEAR_Sniffs_Methods_OpeningMethodBraceSniff
extends Pool_Sniffs_Indent_Style_BSD_Allman {}
class MyStandard_Sniffs_Methods_OpeningMethodBraceSniff
extends Pool_Sniffs_Indent_Style_K_R {}
*) Define a class for each kind of multiple-word identifiers :
class Pool_Sniffs_NamingConventions_StudlyCaps { ... }
class Pool_Sniffs_NamingConventions_CamelCaps { ... }
class Pool_Sniffs_NamingConventions_SeparatorUnderscore { ... }
...
and then we can have :
class PEAR_Sniffs_NamingConventions_ValidFunctionNameSniff
extends Pool_Sniffs_NamingConventions_CamelCaps {}
class MyStandard_Sniffs_NamingConventions_ValidFunctionNameSniff
extends Pool_Sniffs_NamingConventions_SeparatorUnderscore {}
*) It would also be possible to overload classes members :
class Pool_Sniffs_Files_LineLengthSniff
{
...
public $line_limit = 80;
...
}
class PEAR_Sniffs_Files_LineLengthSniff
extends Pool_Sniffs_Files_LineLengthSniff
{
public $line_limit = 85;
}
class MyStandard_Sniffs_Files_LineLengthSniff
extends Pool_Sniffs_Files_LineLengthSniff
{
public $line_limit = 120;
}
*) Define 2 classes to check that true/false/null are lower/upper case :
class Pool_Sniffs_PHP_LowerCaseConstantSniff { ... }
class Pool_Sniffs_PHP_UpperCaseConstantSniff { ... }
and then we can have :
class PEAR_Sniffs_PHP_ConstantSniff
extends Pool_Sniffs_PHP_LowerCaseConstantSniff {}
class MyStandard_Sniffs_PHP_ConstantSniff
extends Pool_Sniffs_PHP_UpperCaseConstantSniff {}
That way, it would help to spread this useful package if the only thing
you have to do to define a coding standard is to "extend" some generic
classes (and eventually to write the specific stuff for your coding
standard).
Regards,
--
Frederic Poeydomenge
Attachment: [application/pgp-signature]
Attachment: [application/pgp-signature]