Subversion Repositories Applications.framework

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5 aurelien 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.25 2008/12/03 04:42:07 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
define('T_PROTOTYPE', 1032);
51
define('T_THIS', 1033);
52
define('T_REGULAR_EXPRESSION', 1034);
53
define('T_PROPERTY', 1035);
54
define('T_LABEL', 1036);
55
define('T_OBJECT', 1037);
56
define('T_COLOUR', 1038);
57
define('T_HASH', 1039);
58
define('T_URL', 1040);
59
define('T_STYLE', 1041);
60
define('T_ASPERAND', 1042);
61
 
62
/**
63
 * The Tokens class contains weightings for tokens based on their
64
 * probability of occurance in a file.
65
 *
66
 * The less the chance of a high occurance of an abitrary token, the higher
67
 * the weighting.
68
 *
69
 * @category  PHP
70
 * @package   PHP_CodeSniffer
71
 * @author    Greg Sherwood <gsherwood@squiz.net>
72
 * @author    Marc McIntyre <mmcintyre@squiz.net>
73
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
74
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
75
 * @version   Release: 1.2.0RC1
76
 * @link      http://pear.php.net/package/PHP_CodeSniffer
77
 */
78
final class PHP_CodeSniffer_Tokens
79
{
80
 
81
    /**
82
     * The token weightings.
83
     *
84
     * @var array(int => int)
85
     */
86
    public static $weightings = array(
87
                                 T_CLASS               => 1000,
88
                                 T_FUNCTION            => 100,
89
 
90
                                 /*
91
                                     Conditions.
92
                                 */
93
 
94
                                 T_WHILE               => 50,
95
                                 T_FOR                 => 50,
96
                                 T_FOREACH             => 50,
97
                                 T_IF                  => 50,
98
                                 T_ELSE                => 50,
99
                                 T_ELSEIF              => 50,
100
                                 T_WHILE               => 50,
101
                                 T_DO                  => 50,
102
                                 T_TRY                 => 50,
103
                                 T_CATCH               => 50,
104
                                 T_SWITCH              => 50,
105
 
106
                                 T_SELF                => 25,
107
                                 T_PARENT              => 25,
108
 
109
                                 /*
110
                                     Operators and arithmetic.
111
                                 */
112
 
113
                                 T_BITWISE_AND         => 8,
114
                                 T_BITWISE_OR          => 8,
115
 
116
                                 T_MULTIPLY            => 5,
117
                                 T_DIVIDE              => 5,
118
                                 T_PLUS                => 5,
119
                                 T_MINUS               => 5,
120
                                 T_MODULUS             => 5,
121
                                 T_POWER               => 5,
122
 
123
                                 T_EQUAL               => 5,
124
                                 T_AND_EQUAL           => 5,
125
                                 T_CONCAT_EQUAL        => 5,
126
                                 T_DIV_EQUAL           => 5,
127
                                 T_MINUS_EQUAL         => 5,
128
                                 T_MOD_EQUAL           => 5,
129
                                 T_MUL_EQUAL           => 5,
130
                                 T_OR_EQUAL            => 5,
131
                                 T_PLUS_EQUAL          => 5,
132
                                 T_XOR_EQUAL           => 5,
133
 
134
                                 T_BOOLEAN_AND         => 5,
135
                                 T_BOOLEAN_OR          => 5,
136
 
137
                                 /*
138
                                     Equality.
139
                                 */
140
 
141
                                 T_IS_EQUAL            => 5,
142
                                 T_IS_NOT_EQUAL        => 5,
143
                                 T_IS_IDENTICAL        => 5,
144
                                 T_IS_NOT_IDENTICAL    => 5,
145
                                 T_IS_SMALLER_OR_EQUAL => 5,
146
                                 T_IS_GREATER_OR_EQUAL => 5,
147
                                );
148
 
149
    /**
150
     * Tokens that represent assignments.
151
     *
152
     * @var array(int)
153
     */
154
    public static $assignmentTokens = array(
155
                                       T_EQUAL,
156
                                       T_AND_EQUAL,
157
                                       T_CONCAT_EQUAL,
158
                                       T_DIV_EQUAL,
159
                                       T_MINUS_EQUAL,
160
                                       T_MOD_EQUAL,
161
                                       T_MUL_EQUAL,
162
                                       T_PLUS_EQUAL,
163
                                       T_XOR_EQUAL,
164
                                      );
165
 
166
    /**
167
     * Tokens that represent equality comparisons.
168
     *
169
     * @var array(int)
170
     */
171
    public static $equalityTokens = array(
172
                                     T_IS_EQUAL,
173
                                     T_IS_NOT_EQUAL,
174
                                     T_IS_IDENTICAL,
175
                                     T_IS_NOT_IDENTICAL,
176
                                     T_IS_SMALLER_OR_EQUAL,
177
                                     T_IS_GREATER_OR_EQUAL,
178
                                    );
179
 
180
    /**
181
     * Tokens that represent comparison operator.
182
     *
183
     * @var array(int)
184
     */
185
    public static $comparisonTokens = array(
186
                                       T_IS_EQUAL,
187
                                       T_IS_IDENTICAL,
188
                                       T_IS_NOT_EQUAL,
189
                                       T_IS_NOT_IDENTICAL,
190
                                       T_LESS_THAN,
191
                                       T_GREATER_THAN,
192
                                       T_IS_SMALLER_OR_EQUAL,
193
                                       T_IS_GREATER_OR_EQUAL,
194
                                      );
195
 
196
    /**
197
     * Tokens that represent arithmetic operators.
198
     *
199
     * @var array(int)
200
     */
201
    public static $arithmeticTokens = array(
202
                                       T_PLUS,
203
                                       T_MINUS,
204
                                       T_MULTIPLY,
205
                                       T_DIVIDE,
206
                                       T_MODULUS,
207
                                      );
208
 
209
    /**
210
     * Tokens that represent casting.
211
     *
212
     * @var array(int)
213
     */
214
    public static $castTokens = array(
215
                                 T_INT_CAST,
216
                                 T_STRING_CAST,
217
                                 T_DOUBLE_CAST,
218
                                 T_ARRAY_CAST,
219
                                 T_BOOL_CAST,
220
                                 T_OBJECT_CAST,
221
                                 T_UNSET_CAST,
222
                                );
223
 
224
    /**
225
     * Token types that open parethesis.
226
     *
227
     * @var array(int)
228
     */
229
    public static $parenthesisOpeners = array(
230
                                         T_ARRAY,
231
                                         T_FUNCTION,
232
                                         T_WHILE,
233
                                         T_FOR,
234
                                         T_FOREACH,
235
                                         T_SWITCH,
236
                                         T_IF,
237
                                         T_ELSEIF,
238
                                         T_CATCH,
239
                                        );
240
 
241
    /**
242
     * Tokens that are allowed to open scopes.
243
     *
244
     * @var array(int)
245
     */
246
    public static $scopeOpeners = array(
247
                                   T_CLASS,
248
                                   T_INTERFACE,
249
                                   T_FUNCTION,
250
                                   T_IF,
251
                                   T_SWITCH,
252
                                   T_CASE,
253
                                   T_DEFAULT,
254
                                   T_WHILE,
255
                                   T_ELSE,
256
                                   T_ELSEIF,
257
                                   T_FOR,
258
                                   T_FOREACH,
259
                                   T_DO,
260
                                   T_TRY,
261
                                   T_CATCH,
262
                                  );
263
 
264
    /**
265
     * Tokens that represent scope modifiers.
266
     *
267
     * @var array(int)
268
     */
269
    public static $scopeModifiers = array(
270
                                     T_PRIVATE,
271
                                     T_PUBLIC,
272
                                     T_PROTECTED,
273
                                    );
274
 
275
    /**
276
     * Tokens that perform operations.
277
     *
278
     * @var array(int)
279
     */
280
    public static $operators = array(
281
                                T_MINUS,
282
                                T_PLUS,
283
                                T_MULTIPLY,
284
                                T_DIVIDE,
285
                                T_MODULUS,
286
                                T_POWER,
287
                                T_BITWISE_AND,
288
                                T_BITWISE_OR,
289
                               );
290
 
291
    /**
292
     * Tokens that perform boolean operations.
293
     *
294
     * @var array(int)
295
     */
296
    public static $booleanOperators = array(
297
                                       T_BOOLEAN_AND,
298
                                       T_BOOLEAN_OR,
299
                                       T_LOGICAL_AND,
300
                                       T_LOGICAL_OR,
301
                                      );
302
 
303
    /**
304
     * Tokens that perform operations.
305
     *
306
     * @var array(int)
307
     */
308
    public static $blockOpeners = array(
309
                                   T_OPEN_CURLY_BRACKET,
310
                                   T_OPEN_SQUARE_BRACKET,
311
                                   T_OPEN_PARENTHESIS,
312
                                  );
313
 
314
    /**
315
     * Tokens that don't represent code.
316
     *
317
     * @var array(int)
318
     */
319
    public static $emptyTokens = array(
320
                                  T_WHITESPACE,
321
                                  T_COMMENT,
322
                                  T_DOC_COMMENT,
323
                                 );
324
 
325
    /**
326
     * Tokens that are comments.
327
     *
328
     * @var array(int)
329
     */
330
    public static $commentTokens = array(
331
                                    T_COMMENT,
332
                                    T_DOC_COMMENT,
333
                                   );
334
 
335
    /**
336
     * Tokens that represent strings.
337
     *
338
     * Note that T_STRINGS are NOT represented in this list.
339
     *
340
     * @var array(int)
341
     */
342
    public static $stringTokens = array(
343
                                   T_CONSTANT_ENCAPSED_STRING,
344
                                   T_DOUBLE_QUOTED_STRING,
345
                                  );
346
 
347
    /**
348
     * Tokens that include files.
349
     *
350
     * @var array(int)
351
     */
352
    public static $includeTokens = array(
353
                                    T_REQUIRE_ONCE,
354
                                    T_REQUIRE,
355
                                    T_INCLUDE_ONCE,
356
                                    T_INCLUDE,
357
                                   );
358
 
359
    /**
360
     * Tokens that make up a heredoc string.
361
     *
362
     * @var array(int)
363
     */
364
    public static $heredocTokens = array(
365
                                    T_START_HEREDOC,
366
                                    T_END_HEREDOC,
367
                                    T_HEREDOC,
368
                                   );
369
 
370
 
371
    /**
372
     * A PHP_CodeSniffer_Tokens class cannot be constructed.
373
     *
374
     * Only static calls are allowed.
375
     */
376
    private function __construct()
377
    {
378
 
379
    }//end __construct()
380
 
381
 
382
    /**
383
     * Returns the highest weighted token type.
384
     *
385
     * Tokens are weighted by their approximate frequency of appearance in code
386
     * - the less frequently they appear in the code, the higher the weighting.
387
     * For example T_CLASS tokens apprear very infrequently in a file, and
388
     * therefore have a high weighting.
389
     *
390
     * Returns false if there are no weightings for any of the specified tokens.
391
     *
392
     * @param array(int) $tokens The token types to get the highest weighted
393
     *                           type for.
394
     *
395
     * @return int The highest weighted token.
396
     */
397
    public static function getHighestWeightedToken(array $tokens)
398
    {
399
        $highest     = -1;
400
        $highestType = false;
401
 
402
        $weights = self::$weightings;
403
 
404
        foreach ($tokens as $token) {
405
            if (isset($weights[$token]) === true) {
406
                $weight = $weights[$token];
407
            } else {
408
                $weight = 0;
409
            }
410
 
411
            if ($weight > $highest) {
412
                $highest     = $weight;
413
                $highestType = $token;
414
            }
415
        }
416
 
417
        return $highestType;
418
 
419
    }//end getHighestWeightedToken()
420
 
421
 
422
}//end class
423
 
424
?>