Issues (12)

src/Token.php (4 issues)

1
<?php
2
3
namespace petitparser;
4
5
/**
6
 * A token represents a parsed part of the input stream. The token holds
7
 * the parsed input, the input buffer, and the start and stop position
8
 * in the input buffer.
9
 */
10
class Token implements Comparable
11
{
12
    /**
13
     * @var mixed
14
     */
15
    private $_value;
16
17
    /**
18
     * @var Buffer
19
     */
20
    private $_buffer;
21
22
    /**
23
     * @var int
24
     */
25
    private $_start;
26
27
    /**
28
     * @var int
29
     */
30
    private $_stop;
31
32
    /**
33
     * @param mixed $value
34
     * @param Buffer $buffer
35
     * @param int $start
36
     * @param int $stop
37
     */
38 1
    public function __construct($value, Buffer $buffer, $start, $stop)
39
    {
40 1
        $this->_value = $value;
41 1
        $this->_buffer = $buffer;
42 1
        $this->_start = $start;
43 1
        $this->_stop = $stop;
44 1
    }
45
46
    /**
47
     * @return Parser Returns a parser for that detects newlines platform independently.
48
     */
49 1
    public static function newLineParser()
50
    {
51 1
        static $parser = null;
52
53 1
        if ($parser === null) {
54 1
            $parser = char("\n")->or_(char("\r")->seq(char("\n")->optional()));
55 1
        }
56
57 1
        return $parser;
58
    }
59
60
    /**
61
     * @param Buffer $buffer
62
     * @param int $position
63
     * @return array [int $line, int $column]
64
     */
65 1
    public static function lineAndColumnOf(Buffer $buffer, $position)
66
    {
67 1
        $line = 1;
68 1
        $offset = 0;
69
70 1
        foreach (Token::newLineParser()->token()->matchesSkipping($buffer) as $token) {
0 ignored issues
show
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
71
            /** @var Token $token */
72 1
            if ($position < $token->getStop()) {
73 1
                return array($line, $position - $offset + 1);
74
            }
75 1
            $line += 1;
76 1
            $offset = $token->getStop();
77 1
        }
78
79 1
        return array($line, $position - $offset + 1);
80
    }
81
82
    /**
83
     * @param mixed $other
84
     *
85
     * @return bool
86
     */
87
    public function isEqualTo($other)
88
    {
89
        return $other instanceof Token
90
            && $this->_value === $other->_value
91
            && $this->_start === $other->_start
92
            && $this->_stop === $other->_stop;
93
    }
94
95
    /**
96
     * @return mixed the parsed value.
97
     */
98 1
    public function getValue()
99
    {
100 1
        return $this->_value;
101
    }
102
103
    /**
104
     * @return Buffer the input buffer.
105
     */
106 1
    public function getBuffer()
107
    {
108 1
        return $this->_buffer;
109
    }
110
111
    /**
112
     * @return int the start position in the input buffer.
113
     */
114 1
    public function getStart()
115
    {
116 1
        return $this->_start;
117
    }
118
119
    /**
120
     * @return int the stop position in the input buffer.
121
     */
122 1
    public function getStop()
123
    {
124 1
        return $this->_stop;
125
    }
126
127
    /**
128
     * @return int the length of the token.
129
     */
130 1
    public function getLength()
131
    {
132 1
        return $this->_stop - $this->_start;
133
    }
134
135
    /**
136
     * @return int the line number of the token.
137
     */
138 1
    public function getLine()
139
    {
140
        /** @noinspection PhpUnusedLocalVariableInspection */
141 1
        list($line, $column) = Token::lineAndColumnOf($this->_buffer, $this->_start);
0 ignored issues
show
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
142
143 1
        return $line;
144
    }
145
146
    /**
147
     * @return int the column number of this token.
148
     */
149 1
    public function getColumn()
150
    {
151
        /** @noinspection PhpUnusedLocalVariableInspection */
152 1
        list($line, $column) = Token::lineAndColumnOf($this->_buffer, $this->_start);
0 ignored issues
show
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
153
154 1
        return $column;
155
    }
156
157
    /**
158
     * @return string the consumed input of the token.
159
     */
160 1
    public function getInput()
161
    {
162 1
        return $this->getBuffer()->slice($this->getStart(), $this->getStop())->getString();
163
    }
164
165
    /**
166
     * @return string
167
     */
168 1
    public function __toString()
169
    {
170 1
        return 'Token[' . self::positionString($this->getBuffer(), $this->getStart()) . ']: [' . implode(', ', $this->getValue()) . ']';
171
    }
172
173
    /**
174
     * @param Buffer $buffer
175
     * @param int $position
176
     *
177
     * @return string
178
     */
179 1
    public static function positionString(Buffer $buffer, $position)
180
    {
181 1
        $lineAndColumn = Token::lineAndColumnOf($buffer, $position);
0 ignored issues
show
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
182
183 1
        return "{$lineAndColumn[0]}:{$lineAndColumn[1]}";
184
    }
185
}
186