4
* You can extend this class and set `xhpast.naminghook` in your `.arclint` to
5
* have an opportunity to override lint results for symbol names.
7
* @task override Overriding Symbol Name Lint Messages
8
* @task util Name Utilities
9
* @task internal Internals
12
abstract class ArcanistXHPASTLintNamingHook {
15
/* -( Internals )---------------------------------------------------------- */
18
* The constructor is final because @{class:ArcanistXHPASTLinter} is
19
* responsible for hook instantiation.
24
final public function __construct() {
29
/* -( Overriding Symbol Name Lint Messages )------------------------------- */
32
* Callback invoked for each symbol, which can override the default
33
* determination of name validity or accept it by returning $default. The
34
* symbol types are: xhp-class, class, interface, function, method, parameter,
35
* constant, and member.
37
* For example, if you want to ban all symbols with "quack" in them and
38
* otherwise accept all the defaults, except allow any naming convention for
39
* methods with "duck" in them, you might implement the method like this:
41
* if (preg_match('/quack/i', $name)) {
42
* return 'Symbol names containing "quack" are forbidden.';
44
* if ($type == 'method' && preg_match('/duck/i', $name)) {
45
* return null; // Always accept.
49
* @param string The symbol type.
50
* @param string The symbol name.
51
* @param string|null The default result from the main rule engine.
52
* @return string|null Null to accept the name, or a message to reject it
53
* with. You should return the default value if you don't
54
* want to specifically provide an override.
57
abstract public function lintSymbolName($type, $name, $default);
60
/* -( Name Utilities )----------------------------------------------------- */
63
* Returns true if a symbol name is UpperCamelCase.
65
* @param string Symbol name.
66
* @return bool True if the symbol is UpperCamelCase.
69
public static function isUpperCamelCase($symbol) {
70
return preg_match('/^[A-Z][A-Za-z0-9]*$/', $symbol);
74
* Returns true if a symbol name is lowerCamelCase.
76
* @param string Symbol name.
77
* @return bool True if the symbol is lowerCamelCase.
80
public static function isLowerCamelCase($symbol) {
81
return preg_match('/^[a-z][A-Za-z0-9]*$/', $symbol);
85
* Returns true if a symbol name is UPPERCASE_WITH_UNDERSCORES.
87
* @param string Symbol name.
88
* @return bool True if the symbol is UPPERCASE_WITH_UNDERSCORES.
91
public static function isUppercaseWithUnderscores($symbol) {
92
return preg_match('/^[A-Z0-9_]+$/', $symbol);
96
* Returns true if a symbol name is lowercase_with_underscores.
98
* @param string Symbol name.
99
* @return bool True if the symbol is lowercase_with_underscores.
102
public static function isLowercaseWithUnderscores($symbol) {
103
return preg_match('/^[a-z0-9_]+$/', $symbol);
107
* Strip non-name components from PHP function symbols. Notably, this discards
108
* the "__" magic-method signifier, to make a symbol appropriate for testing
109
* with methods like @{method:isLowerCamelCase}.
111
* @param string Symbol name.
112
* @return string Stripped symbol.
115
public static function stripPHPFunction($symbol) {
117
case '__assign_concat':
134
return preg_replace('/^__/', '', $symbol);
142
* Strip non-name components from PHP variable symbols. Notably, this discards
143
* the "$", to make a symbol appropriate for testing with methods like
144
* @{method:isLowercaseWithUnderscores}.
146
* @param string Symbol name.
147
* @return string Stripped symbol.
150
public static function stripPHPVariable($symbol) {
151
return preg_replace('/^\$/', '', $symbol);