Error::getEndColumn()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace PhpParser;
4
5
class Error extends \RuntimeException
6
{
7
    protected $rawMessage;
8
    protected $attributes;
9
10
    /**
11
     * Creates an Exception signifying a parse error.
12
     *
13
     * @param string    $message    Error message
14
     * @param array|int $attributes Attributes of node/token where error occurred
15
     *                              (or start line of error -- deprecated)
16
     */
17
    public function __construct($message, $attributes = array()) {
18
        $this->rawMessage = (string) $message;
19
        if (is_array($attributes)) {
20
            $this->attributes = $attributes;
21
        } else {
22
            $this->attributes = array('startLine' => $attributes);
23
        }
24
        $this->updateMessage();
25
    }
26
27
    /**
28
     * Gets the error message
29
     *
30
     * @return string Error message
31
     */
32
    public function getRawMessage() {
33
        return $this->rawMessage;
34
    }
35
36
    /**
37
     * Gets the line the error starts in.
38
     *
39
     * @return int Error start line
40
     */
41
    public function getStartLine() {
42
        return isset($this->attributes['startLine']) ? $this->attributes['startLine'] : -1;
43
    }
44
45
    /**
46
     * Gets the line the error ends in.
47
     *
48
     * @return int Error end line
49
     */
50
    public function getEndLine() {
51
        return isset($this->attributes['endLine']) ? $this->attributes['endLine'] : -1;
52
    }
53
54
55
    /**
56
     * Gets the attributes of the node/token the error occurred at.
57
     *
58
     * @return array
59
     */
60
    public function getAttributes() {
61
        return $this->attributes;
62
    }
63
64
    /**
65
     * Sets the line of the PHP file the error occurred in.
66
     *
67
     * @param string $message Error message
68
     */
69
    public function setRawMessage($message) {
70
        $this->rawMessage = (string) $message;
71
        $this->updateMessage();
72
    }
73
74
    /**
75
     * Sets the line the error starts in.
76
     *
77
     * @param int $line Error start line
78
     */
79
    public function setStartLine($line) {
80
        $this->attributes['startLine'] = (int) $line;
81
        $this->updateMessage();
82
    }
83
84
    /**
85
     * Returns whether the error has start and end column information.
86
     *
87
     * For column information enable the startFilePos and endFilePos in the lexer options.
88
     *
89
     * @return bool
90
     */
91
    public function hasColumnInfo() {
92
        return isset($this->attributes['startFilePos']) && isset($this->attributes['endFilePos']);
93
    }
94
95
    /**
96
     * Gets the start column (1-based) into the line where the error started.
97
     *
98
     * @param string $code Source code of the file
99
     * @return int
100
     */
101
    public function getStartColumn($code) {
102
        if (!$this->hasColumnInfo()) {
103
            throw new \RuntimeException('Error does not have column information');
104
        }
105
106
        return $this->toColumn($code, $this->attributes['startFilePos']);
107
    }
108
109
    /**
110
     * Gets the end column (1-based) into the line where the error ended.
111
     *
112
     * @param string $code Source code of the file
113
     * @return int
114
     */
115
    public function getEndColumn($code) {
116
        if (!$this->hasColumnInfo()) {
117
            throw new \RuntimeException('Error does not have column information');
118
        }
119
120
        return $this->toColumn($code, $this->attributes['endFilePos']);
121
    }
122
123
    private function toColumn($code, $pos) {
124
        if ($pos > strlen($code)) {
125
            throw new \RuntimeException('Invalid position information');
126
        }
127
128
        $lineStartPos = strrpos($code, "\n", $pos - strlen($code));
129
        if (false === $lineStartPos) {
130
            $lineStartPos = -1;
131
        }
132
133
        return $pos - $lineStartPos;
134
    }
135
136
    /**
137
     * Updates the exception message after a change to rawMessage or rawLine.
138
     */
139
    protected function updateMessage() {
140
        $this->message = $this->rawMessage;
141
142
        if (-1 === $this->getStartLine()) {
143
            $this->message .= ' on unknown line';
144
        } else {
145
            $this->message .= ' on line ' . $this->getStartLine();
146
        }
147
    }
148
149
    /** @deprecated Use getStartLine() instead */
150
    public function getRawLine() {
151
        return $this->getStartLine();
152
    }
153
154
    /** @deprecated Use setStartLine() instead */
155
    public function setRawLine($line) {
156
        $this->setStartLine($line);
157
    }
158
}
159