~ubuntu-branches/ubuntu/vivid/phabricator/vivid-proposed

« back to all changes in this revision

Viewing changes to arcanist/src/lint/linter/xhpast/ArcanistXHPASTLintNamingHook.php

  • Committer: Package Import Robot
  • Author(s): Richard Sellam
  • Date: 2014-10-23 20:49:26 UTC
  • mfrom: (0.2.1) (0.1.1)
  • Revision ID: package-import@ubuntu.com-20141023204926-vq80u1op4df44azb
Tags: 0~git20141023-1
Initial release (closes: #703046)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/**
 
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.
 
6
 *
 
7
 * @task override   Overriding Symbol Name Lint Messages
 
8
 * @task util       Name Utilities
 
9
 * @task internal   Internals
 
10
 * @stable
 
11
 */
 
12
abstract class ArcanistXHPASTLintNamingHook {
 
13
 
 
14
 
 
15
/* -(  Internals  )---------------------------------------------------------- */
 
16
 
 
17
  /**
 
18
   * The constructor is final because @{class:ArcanistXHPASTLinter} is
 
19
   * responsible for hook instantiation.
 
20
   *
 
21
   * @return this
 
22
   * @task internals
 
23
   */
 
24
  final public function __construct() {
 
25
    // <empty>
 
26
  }
 
27
 
 
28
 
 
29
/* -(  Overriding Symbol Name Lint Messages  )------------------------------- */
 
30
 
 
31
  /**
 
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.
 
36
   *
 
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:
 
40
   *
 
41
   *   if (preg_match('/quack/i', $name)) {
 
42
   *     return 'Symbol names containing "quack" are forbidden.';
 
43
   *   }
 
44
   *   if ($type == 'method' && preg_match('/duck/i', $name)) {
 
45
   *     return null; // Always accept.
 
46
   *   }
 
47
   *   return $default;
 
48
   *
 
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.
 
55
   * @task override
 
56
   */
 
57
  abstract public function lintSymbolName($type, $name, $default);
 
58
 
 
59
 
 
60
/* -(  Name Utilities  )----------------------------------------------------- */
 
61
 
 
62
  /**
 
63
   * Returns true if a symbol name is UpperCamelCase.
 
64
   *
 
65
   * @param string Symbol name.
 
66
   * @return bool True if the symbol is UpperCamelCase.
 
67
   * @task util
 
68
   */
 
69
  public static function isUpperCamelCase($symbol) {
 
70
    return preg_match('/^[A-Z][A-Za-z0-9]*$/', $symbol);
 
71
  }
 
72
 
 
73
  /**
 
74
   * Returns true if a symbol name is lowerCamelCase.
 
75
   *
 
76
   * @param string Symbol name.
 
77
   * @return bool True if the symbol is lowerCamelCase.
 
78
   * @task util
 
79
   */
 
80
  public static function isLowerCamelCase($symbol) {
 
81
    return preg_match('/^[a-z][A-Za-z0-9]*$/', $symbol);
 
82
  }
 
83
 
 
84
  /**
 
85
   * Returns true if a symbol name is UPPERCASE_WITH_UNDERSCORES.
 
86
   *
 
87
   * @param string Symbol name.
 
88
   * @return bool True if the symbol is UPPERCASE_WITH_UNDERSCORES.
 
89
   * @task util
 
90
   */
 
91
  public static function isUppercaseWithUnderscores($symbol) {
 
92
    return preg_match('/^[A-Z0-9_]+$/', $symbol);
 
93
  }
 
94
 
 
95
  /**
 
96
   * Returns true if a symbol name is lowercase_with_underscores.
 
97
   *
 
98
   * @param string Symbol name.
 
99
   * @return bool True if the symbol is lowercase_with_underscores.
 
100
   * @task util
 
101
   */
 
102
  public static function isLowercaseWithUnderscores($symbol) {
 
103
    return preg_match('/^[a-z0-9_]+$/', $symbol);
 
104
  }
 
105
 
 
106
  /**
 
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}.
 
110
   *
 
111
   * @param   string Symbol name.
 
112
   * @return  string Stripped symbol.
 
113
   * @task util
 
114
   */
 
115
  public static function stripPHPFunction($symbol) {
 
116
    switch ($symbol) {
 
117
      case '__assign_concat':
 
118
      case '__call':
 
119
      case '__callStatic':
 
120
      case '__clone':
 
121
      case '__concat':
 
122
      case '__construct':
 
123
      case '__debugInfo':
 
124
      case '__destruct':
 
125
      case '__get':
 
126
      case '__invoke':
 
127
      case '__isset':
 
128
      case '__set':
 
129
      case '__set_state':
 
130
      case '__sleep':
 
131
      case '__toString':
 
132
      case '__unset':
 
133
      case '__wakeup':
 
134
        return preg_replace('/^__/', '', $symbol);
 
135
 
 
136
      default:
 
137
        return $symbol;
 
138
    }
 
139
  }
 
140
 
 
141
  /**
 
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}.
 
145
   *
 
146
   * @param string Symbol name.
 
147
   * @return string Stripped symbol.
 
148
   * @task util
 
149
   */
 
150
  public static function stripPHPVariable($symbol) {
 
151
    return preg_replace('/^\$/', '', $symbol);
 
152
  }
 
153
 
 
154
}