Token::name()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 40
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
eloc 30
c 1
b 0
f 1
nc 4
nop 1
dl 0
loc 40
rs 9.1288
1
<?php
2
/** @file
3
 * Parser tokens.
4
 */
5
6
namespace QueryPath\CSS;
7
8
/**
9
 * Tokens for CSS.
10
 * This class defines the recognized tokens for the parser, and also
11
 * provides utility functions for error reporting.
12
 *
13
 * @ingroup querypath_css
14
 */
15
final class Token
16
{
17
    public const CHAR    = 0x0;
18
    public const STAR    = 0x1;
19
    public const RANGLE  = 0x2;
20
    public const DOT     = 0x3;
21
    public const OCTO    = 0x4;
22
    public const RSQUARE = 0x5;
23
    public const LSQUARE = 0x6;
24
    public const COLON   = 0x7;
25
    public const RPAREN  = 0x8;
26
    public const LPAREN  = 0x9;
27
    public const PLUS    = 0xA;
28
    public const TILDE   = 0xB;
29
    public const EQ      = 0xC;
30
    public const PIPE    = 0xD;
31
    public const COMMA   = 0xE;
32
    public const WHITE   = 0xF;
33
    public const QUOTE   = 0x10;
34
    public const SQUOTE  = 0x11;
35
    public const BSLASH  = 0x12;
36
    public const CARAT   = 0x13;
37
    public const DOLLAR  = 0x14;
38
    public const AT      = 0x15; // This is not in the spec. Apparently, old broken CSS uses it.
39
40
    // In legal range for string.
41
    public const STRING_LEGAL = 0x63;
42
43
    /**
44
     * Get a name for a given constant. Used for error handling.
45
     */
46
    public static function name($const_int)
47
    {
48
        $a = [
49
            'character',
50
            'star',
51
            'right angle bracket',
52
            'dot',
53
            'octothorp',
54
            'right square bracket',
55
            'left square bracket',
56
            'colon',
57
            'right parenthesis',
58
            'left parenthesis',
59
            'plus',
60
            'tilde',
61
            'equals',
62
            'vertical bar',
63
            'comma',
64
            'space',
65
            'quote',
66
            'single quote',
67
            'backslash',
68
            'carat',
69
            'dollar',
70
            'at',
71
        ];
72
73
        if (isset($a[$const_int]) && is_numeric($const_int)) {
74
            return $a[$const_int];
75
        }
76
77
        if ($const_int === self::STRING_LEGAL) {
78
            return 'a legal non-alphanumeric character';
79
        }
80
81
        if ($const_int === false) {
82
            return 'end of file';
83
        }
84
85
        return sprintf('illegal character (%s)', $const_int);
86
    }
87
}
88