~ubuntu-branches/ubuntu/trusty/php-codesniffer/trusty

« back to all changes in this revision

Viewing changes to PHP_CodeSniffer-1.0.1/CodeSniffer/Tokens.php

  • Committer: Bazaar Package Importer
  • Author(s): Jan Wagner
  • Date: 2008-03-21 23:29:33 UTC
  • Revision ID: james.westby@ubuntu.com-20080321232933-za8kvi1bgvrvud6z
Tags: upstream-1.0.1
ImportĀ upstreamĀ versionĀ 1.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * The Tokens class contains weightings for tokens based on their
 
4
 * probability of occurance in a file.
 
5
 *
 
6
 * PHP version 5
 
7
 *
 
8
 * @category  PHP
 
9
 * @package   PHP_CodeSniffer
 
10
 * @author    Greg Sherwood <gsherwood@squiz.net>
 
11
 * @author    Marc McIntyre <mmcintyre@squiz.net>
 
12
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
 
13
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
 
14
 * @version   CVS: $Id: Tokens.php,v 1.13 2007/11/15 22:26:23 squiz Exp $
 
15
 * @link      http://pear.php.net/package/PHP_CodeSniffer
 
16
 */
 
17
 
 
18
define('T_NONE', 0);
 
19
define('T_OPEN_CURLY_BRACKET', 1000);
 
20
define('T_CLOSE_CURLY_BRACKET', 1001);
 
21
define('T_OPEN_SQUARE_BRACKET', 1002);
 
22
define('T_CLOSE_SQUARE_BRACKET', 1003);
 
23
define('T_OPEN_PARENTHESIS', 1004);
 
24
define('T_CLOSE_PARENTHESIS', 1005);
 
25
define('T_COLON', 1006);
 
26
define('T_STRING_CONCAT', 1007);
 
27
define('T_INLINE_THEN', 1008);
 
28
define('T_NULL', 1009);
 
29
define('T_FALSE', 1010);
 
30
define('T_TRUE', 1011);
 
31
define('T_SEMICOLON', 1012);
 
32
define('T_EQUAL', 1013);
 
33
define('T_MULTIPLY', 1015);
 
34
define('T_DIVIDE', 1016);
 
35
define('T_PLUS', 1017);
 
36
define('T_MINUS', 1018);
 
37
define('T_MODULUS', 1019);
 
38
define('T_POWER', 1020);
 
39
define('T_BITWISE_AND', 1021);
 
40
define('T_BITWISE_OR', 1022);
 
41
define('T_ARRAY_HINT', 1023);
 
42
define('T_GREATER_THAN', 1024);
 
43
define('T_LESS_THAN', 1025);
 
44
define('T_BOOLEAN_NOT', 1026);
 
45
define('T_SELF', 1027);
 
46
define('T_PARENT', 1028);
 
47
define('T_DOUBLE_QUOTED_STRING', 1029);
 
48
define('T_COMMA', 1030);
 
49
define('T_HEREDOC', 1031);
 
50
 
 
51
/**
 
52
 * The Tokens class contains weightings for tokens based on their
 
53
 * probability of occurance in a file.
 
54
 *
 
55
 * The less the chance of a high occurance of an abitrary token, the higher
 
56
 * the weighting.
 
57
 *
 
58
 * @category  PHP
 
59
 * @package   PHP_CodeSniffer
 
60
 * @author    Greg Sherwood <gsherwood@squiz.net>
 
61
 * @author    Marc McIntyre <mmcintyre@squiz.net>
 
62
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
 
63
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
 
64
 * @version   Release: 1.0.1
 
65
 * @link      http://pear.php.net/package/PHP_CodeSniffer
 
66
 */
 
67
final class PHP_CodeSniffer_Tokens
 
68
{
 
69
 
 
70
    /**
 
71
     * The token weightings.
 
72
     *
 
73
     * @var array(int => int)
 
74
     */
 
75
    public static $weightings = array(
 
76
                                 T_CLASS               => 1000,
 
77
                                 T_FUNCTION            => 100,
 
78
 
 
79
                                 /*
 
80
                                     Conditions.
 
81
                                 */
 
82
 
 
83
                                 T_WHILE               => 50,
 
84
                                 T_FOR                 => 50,
 
85
                                 T_FOREACH             => 50,
 
86
                                 T_IF                  => 50,
 
87
                                 T_ELSE                => 50,
 
88
                                 T_ELSEIF              => 50,
 
89
                                 T_WHILE               => 50,
 
90
                                 T_DO                  => 50,
 
91
                                 T_TRY                 => 50,
 
92
                                 T_CATCH               => 50,
 
93
                                 T_SWITCH              => 50,
 
94
 
 
95
                                 T_SELF                => 25,
 
96
                                 T_PARENT              => 25,
 
97
 
 
98
                                 /*
 
99
                                     Operators and arithmetic.
 
100
                                 */
 
101
 
 
102
                                 T_BITWISE_AND         => 8,
 
103
                                 T_BITWISE_OR          => 8,
 
104
 
 
105
                                 T_MULTIPLY            => 5,
 
106
                                 T_DIVIDE              => 5,
 
107
                                 T_PLUS                => 5,
 
108
                                 T_MINUS               => 5,
 
109
                                 T_MODULUS             => 5,
 
110
                                 T_POWER               => 5,
 
111
 
 
112
                                 T_EQUAL               => 5,
 
113
                                 T_AND_EQUAL           => 5,
 
114
                                 T_CONCAT_EQUAL        => 5,
 
115
                                 T_DIV_EQUAL           => 5,
 
116
                                 T_MINUS_EQUAL         => 5,
 
117
                                 T_MOD_EQUAL           => 5,
 
118
                                 T_MUL_EQUAL           => 5,
 
119
                                 T_OR_EQUAL            => 5,
 
120
                                 T_PLUS_EQUAL          => 5,
 
121
                                 T_XOR_EQUAL           => 5,
 
122
 
 
123
                                 T_BOOLEAN_AND         => 5,
 
124
                                 T_BOOLEAN_OR          => 5,
 
125
 
 
126
                                 /*
 
127
                                     Equality.
 
128
                                 */
 
129
 
 
130
                                 T_IS_EQUAL            => 5,
 
131
                                 T_IS_NOT_EQUAL        => 5,
 
132
                                 T_IS_IDENTICAL        => 5,
 
133
                                 T_IS_NOT_IDENTICAL    => 5,
 
134
                                 T_IS_SMALLER_OR_EQUAL => 5,
 
135
                                 T_IS_GREATER_OR_EQUAL => 5,
 
136
 
 
137
                                 T_WHITESPACE          => 0,
 
138
                                );
 
139
 
 
140
    /**
 
141
     * Tokens that represent assignments.
 
142
     *
 
143
     * @var array(int)
 
144
     */
 
145
    public static $assignmentTokens = array(
 
146
                                       T_EQUAL,
 
147
                                       T_AND_EQUAL,
 
148
                                       T_CONCAT_EQUAL,
 
149
                                       T_DIV_EQUAL,
 
150
                                       T_MINUS_EQUAL,
 
151
                                       T_MOD_EQUAL,
 
152
                                       T_MUL_EQUAL,
 
153
                                       T_PLUS_EQUAL,
 
154
                                       T_XOR_EQUAL,
 
155
                                      );
 
156
 
 
157
    /**
 
158
     * Tokens that represent equality comparisons.
 
159
     *
 
160
     * @var array(int)
 
161
     */
 
162
    public static $equalityTokens = array(
 
163
                                     T_IS_EQUAL,
 
164
                                     T_IS_NOT_EQUAL,
 
165
                                     T_IS_IDENTICAL,
 
166
                                     T_IS_NOT_IDENTICAL,
 
167
                                     T_IS_SMALLER_OR_EQUAL,
 
168
                                     T_IS_GREATER_OR_EQUAL,
 
169
                                    );
 
170
 
 
171
    /**
 
172
     * Tokens that represent comparison operator.
 
173
     *
 
174
     * @var array(int)
 
175
     */
 
176
    public static $comparisonTokens = array(
 
177
                                       T_IS_EQUAL,
 
178
                                       T_IS_IDENTICAL,
 
179
                                       T_IS_NOT_EQUAL,
 
180
                                       T_IS_NOT_IDENTICAL,
 
181
                                       T_LESS_THAN,
 
182
                                       T_GREATER_THAN,
 
183
                                       T_IS_SMALLER_OR_EQUAL,
 
184
                                       T_IS_GREATER_OR_EQUAL,
 
185
                                      );
 
186
 
 
187
    /**
 
188
     * Tokens that represent arithmetic operators.
 
189
     *
 
190
     * @var array(int)
 
191
     */
 
192
    public static $arithmeticTokens = array(
 
193
                                       T_PLUS,
 
194
                                       T_MINUS,
 
195
                                       T_MULTIPLY,
 
196
                                       T_DIVIDE,
 
197
                                       T_MODULUS,
 
198
                                      );
 
199
 
 
200
    /**
 
201
     * Tokens that represent casting.
 
202
     *
 
203
     * @var array(int)
 
204
     */
 
205
    public static $castTokens = array(
 
206
                                 T_INT_CAST,
 
207
                                 T_STRING_CAST,
 
208
                                 T_DOUBLE_CAST,
 
209
                                 T_ARRAY_CAST,
 
210
                                 T_BOOL_CAST,
 
211
                                 T_OBJECT_CAST,
 
212
                                 T_UNSET_CAST,
 
213
                                );
 
214
 
 
215
    /**
 
216
     * Token types that open parethesis.
 
217
     *
 
218
     * @var array(int)
 
219
     */
 
220
    public static $parenthesisOpeners = array(
 
221
                                         T_ARRAY,
 
222
                                         T_FUNCTION,
 
223
                                         T_WHILE,
 
224
                                         T_FOR,
 
225
                                         T_FOREACH,
 
226
                                         T_SWITCH,
 
227
                                         T_IF,
 
228
                                         T_ELSEIF,
 
229
                                         T_CATCH,
 
230
                                        );
 
231
 
 
232
    /**
 
233
     * Tokens that are allowed to open scopes.
 
234
     *
 
235
     * @var array(int)
 
236
     */
 
237
    public static $scopeOpeners = array(
 
238
                                   T_CLASS,
 
239
                                   T_FUNCTION,
 
240
                                   T_IF,
 
241
                                   T_SWITCH,
 
242
                                   T_CASE,
 
243
                                   T_DEFAULT,
 
244
                                   T_WHILE,
 
245
                                   T_ELSE,
 
246
                                   T_ELSEIF,
 
247
                                   T_FOR,
 
248
                                   T_FOREACH,
 
249
                                   T_DO,
 
250
                                   T_TRY,
 
251
                                   T_CATCH,
 
252
                                  );
 
253
 
 
254
    /**
 
255
     * Tokens that represent scope modifiers.
 
256
     *
 
257
     * @var array(int)
 
258
     */
 
259
    public static $scopeModifiers = array(
 
260
                                     T_PRIVATE,
 
261
                                     T_PUBLIC,
 
262
                                     T_PROTECTED,
 
263
                                    );
 
264
 
 
265
    /**
 
266
     * Tokens that perform operations.
 
267
     *
 
268
     * @var array(int)
 
269
     */
 
270
    public static $operators = array(
 
271
                                T_MINUS,
 
272
                                T_PLUS,
 
273
                                T_MULTIPLY,
 
274
                                T_DIVIDE,
 
275
                                T_MODULUS,
 
276
                                T_POWER,
 
277
                                T_BITWISE_AND,
 
278
                                T_BITWISE_OR,
 
279
                               );
 
280
 
 
281
    /**
 
282
     * Tokens that perform operations.
 
283
     *
 
284
     * @var array(int)
 
285
     */
 
286
    public static $blockOpeners = array(
 
287
                                   T_OPEN_CURLY_BRACKET,
 
288
                                   T_OPEN_SQUARE_BRACKET,
 
289
                                   T_OPEN_PARENTHESIS,
 
290
                                  );
 
291
 
 
292
    /**
 
293
     * Tokens that don't represent code.
 
294
     *
 
295
     * @var array(int)
 
296
     */
 
297
    public static $emptyTokens = array(
 
298
                                  T_WHITESPACE,
 
299
                                  T_COMMENT,
 
300
                                  T_DOC_COMMENT,
 
301
                                 );
 
302
 
 
303
    /**
 
304
     * Tokens that are comments.
 
305
     *
 
306
     * @var array(int)
 
307
     */
 
308
    public static $commentTokens = array(
 
309
                                    T_COMMENT,
 
310
                                    T_DOC_COMMENT,
 
311
                                   );
 
312
 
 
313
    /**
 
314
     * Tokens that represent strings.
 
315
     *
 
316
     * Note that T_STRINGS are NOT represented in this list.
 
317
     *
 
318
     * @var array(int)
 
319
     */
 
320
    public static $stringTokens = array(
 
321
                                   T_CONSTANT_ENCAPSED_STRING,
 
322
                                   T_DOUBLE_QUOTED_STRING,
 
323
                                  );
 
324
 
 
325
    /**
 
326
     * Tokens that include files.
 
327
     *
 
328
     * @var array(int)
 
329
     */
 
330
    public static $includeTokens = array(
 
331
                                    T_REQUIRE_ONCE,
 
332
                                    T_REQUIRE,
 
333
                                    T_INCLUDE_ONCE,
 
334
                                    T_INCLUDE,
 
335
                                   );
 
336
 
 
337
 
 
338
    /**
 
339
     * A PHP_CodeSniffer_Tokens class cannot be constructed.
 
340
     *
 
341
     * Only static calls are allowed.
 
342
     */
 
343
    private function __construct()
 
344
    {
 
345
 
 
346
    }//end __construct()
 
347
 
 
348
 
 
349
    /**
 
350
     * Returns the highest weighted token type.
 
351
     *
 
352
     * Tokens are weighted by their approximate frequency of appearance in code
 
353
     * - the less frequently they appear in the code, the higher the weighting.
 
354
     * For example T_CLASS tokens apprear very infrequently in a file, and
 
355
     * therefore have a high weighting.
 
356
     *
 
357
     * Returns false if there are no weightings for any of the specified tokens.
 
358
     *
 
359
     * @param array(int) $tokens The token types to get the highest weighted
 
360
     *                           type for.
 
361
     *
 
362
     * @return int The highest weighted token.
 
363
     */
 
364
    public static function getHighestWeightedToken(array $tokens)
 
365
    {
 
366
        $highest     = -1;
 
367
        $highestType = false;
 
368
 
 
369
        $weights = self::$weightings;
 
370
 
 
371
        foreach ($tokens as $token) {
 
372
            if (isset($weights[$token]) === true && $weights[$token] > $highest) {
 
373
                $highest     = $weights[$token];
 
374
                $highestType = $token;
 
375
            }
 
376
        }
 
377
 
 
378
        return $highestType;
 
379
 
 
380
    }//end getHighestWeightedToken()
 
381
 
 
382
 
 
383
}//end class
 
384
 
 
385
?>