comparison vendor/nikic/php-parser/lib/PhpParser/Error.php @ 13:5fb285c0d0e3

Update Drupal core to 8.4.7 via Composer. Security update; I *think* we've been lucky to get away with this so far, as we don't support self-registration which seems to be used by the so-called "drupalgeddon 2" attack that 8.4.5 was vulnerable to.
author Chris Cannam
date Mon, 23 Apr 2018 09:33:26 +0100
parents 4c8ae668cc8c
children c2387f117808
comparison
equal deleted inserted replaced
12:7a779792577d 13:5fb285c0d0e3
1 <?php 1 <?php declare(strict_types=1);
2 2
3 namespace PhpParser; 3 namespace PhpParser;
4 4
5 class Error extends \RuntimeException 5 class Error extends \RuntimeException
6 { 6 {
12 * 12 *
13 * @param string $message Error message 13 * @param string $message Error message
14 * @param array|int $attributes Attributes of node/token where error occurred 14 * @param array|int $attributes Attributes of node/token where error occurred
15 * (or start line of error -- deprecated) 15 * (or start line of error -- deprecated)
16 */ 16 */
17 public function __construct($message, $attributes = array()) { 17 public function __construct(string $message, $attributes = []) {
18 $this->rawMessage = (string) $message; 18 $this->rawMessage = (string) $message;
19 if (is_array($attributes)) { 19 if (is_array($attributes)) {
20 $this->attributes = $attributes; 20 $this->attributes = $attributes;
21 } else { 21 } else {
22 $this->attributes = array('startLine' => $attributes); 22 $this->attributes = ['startLine' => $attributes];
23 } 23 }
24 $this->updateMessage(); 24 $this->updateMessage();
25 } 25 }
26 26
27 /** 27 /**
28 * Gets the error message 28 * Gets the error message
29 * 29 *
30 * @return string Error message 30 * @return string Error message
31 */ 31 */
32 public function getRawMessage() { 32 public function getRawMessage() : string {
33 return $this->rawMessage; 33 return $this->rawMessage;
34 } 34 }
35 35
36 /** 36 /**
37 * Gets the line the error starts in. 37 * Gets the line the error starts in.
38 * 38 *
39 * @return int Error start line 39 * @return int Error start line
40 */ 40 */
41 public function getStartLine() { 41 public function getStartLine() : int {
42 return isset($this->attributes['startLine']) ? $this->attributes['startLine'] : -1; 42 return $this->attributes['startLine'] ?? -1;
43 } 43 }
44 44
45 /** 45 /**
46 * Gets the line the error ends in. 46 * Gets the line the error ends in.
47 * 47 *
48 * @return int Error end line 48 * @return int Error end line
49 */ 49 */
50 public function getEndLine() { 50 public function getEndLine() : int {
51 return isset($this->attributes['endLine']) ? $this->attributes['endLine'] : -1; 51 return $this->attributes['endLine'] ?? -1;
52 } 52 }
53
54 53
55 /** 54 /**
56 * Gets the attributes of the node/token the error occurred at. 55 * Gets the attributes of the node/token the error occurred at.
57 * 56 *
58 * @return array 57 * @return array
59 */ 58 */
60 public function getAttributes() { 59 public function getAttributes() : array {
61 return $this->attributes; 60 return $this->attributes;
62 } 61 }
63 62
64 /** 63 /**
65 * Sets the attributes of the node/token the error occured at. 64 * Sets the attributes of the node/token the error occurred at.
66 * 65 *
67 * @param array $attributes 66 * @param array $attributes
68 */ 67 */
69 public function setAttributes(array $attributes) { 68 public function setAttributes(array $attributes) {
70 $this->attributes = $attributes; 69 $this->attributes = $attributes;
74 /** 73 /**
75 * Sets the line of the PHP file the error occurred in. 74 * Sets the line of the PHP file the error occurred in.
76 * 75 *
77 * @param string $message Error message 76 * @param string $message Error message
78 */ 77 */
79 public function setRawMessage($message) { 78 public function setRawMessage(string $message) {
80 $this->rawMessage = (string) $message; 79 $this->rawMessage = (string) $message;
81 $this->updateMessage(); 80 $this->updateMessage();
82 } 81 }
83 82
84 /** 83 /**
85 * Sets the line the error starts in. 84 * Sets the line the error starts in.
86 * 85 *
87 * @param int $line Error start line 86 * @param int $line Error start line
88 */ 87 */
89 public function setStartLine($line) { 88 public function setStartLine(int $line) {
90 $this->attributes['startLine'] = (int) $line; 89 $this->attributes['startLine'] = (int) $line;
91 $this->updateMessage(); 90 $this->updateMessage();
92 } 91 }
93 92
94 /** 93 /**
96 * 95 *
97 * For column information enable the startFilePos and endFilePos in the lexer options. 96 * For column information enable the startFilePos and endFilePos in the lexer options.
98 * 97 *
99 * @return bool 98 * @return bool
100 */ 99 */
101 public function hasColumnInfo() { 100 public function hasColumnInfo() : bool {
102 return isset($this->attributes['startFilePos']) && isset($this->attributes['endFilePos']); 101 return isset($this->attributes['startFilePos'], $this->attributes['endFilePos']);
103 } 102 }
104 103
105 /** 104 /**
106 * Gets the start column (1-based) into the line where the error started. 105 * Gets the start column (1-based) into the line where the error started.
107 * 106 *
108 * @param string $code Source code of the file 107 * @param string $code Source code of the file
109 * @return int 108 * @return int
110 */ 109 */
111 public function getStartColumn($code) { 110 public function getStartColumn(string $code) : int {
112 if (!$this->hasColumnInfo()) { 111 if (!$this->hasColumnInfo()) {
113 throw new \RuntimeException('Error does not have column information'); 112 throw new \RuntimeException('Error does not have column information');
114 } 113 }
115 114
116 return $this->toColumn($code, $this->attributes['startFilePos']); 115 return $this->toColumn($code, $this->attributes['startFilePos']);
120 * Gets the end column (1-based) into the line where the error ended. 119 * Gets the end column (1-based) into the line where the error ended.
121 * 120 *
122 * @param string $code Source code of the file 121 * @param string $code Source code of the file
123 * @return int 122 * @return int
124 */ 123 */
125 public function getEndColumn($code) { 124 public function getEndColumn(string $code) : int {
126 if (!$this->hasColumnInfo()) { 125 if (!$this->hasColumnInfo()) {
127 throw new \RuntimeException('Error does not have column information'); 126 throw new \RuntimeException('Error does not have column information');
128 } 127 }
129 128
130 return $this->toColumn($code, $this->attributes['endFilePos']); 129 return $this->toColumn($code, $this->attributes['endFilePos']);
131 } 130 }
132 131
133 public function getMessageWithColumnInfo($code) { 132 /**
133 * Formats message including line and column information.
134 *
135 * @param string $code Source code associated with the error, for calculation of the columns
136 *
137 * @return string Formatted message
138 */
139 public function getMessageWithColumnInfo(string $code) : string {
134 return sprintf( 140 return sprintf(
135 '%s from %d:%d to %d:%d', $this->getRawMessage(), 141 '%s from %d:%d to %d:%d', $this->getRawMessage(),
136 $this->getStartLine(), $this->getStartColumn($code), 142 $this->getStartLine(), $this->getStartColumn($code),
137 $this->getEndLine(), $this->getEndColumn($code) 143 $this->getEndLine(), $this->getEndColumn($code)
138 ); 144 );
139 } 145 }
140 146
141 private function toColumn($code, $pos) { 147 /**
148 * Converts a file offset into a column.
149 *
150 * @param string $code Source code that $pos indexes into
151 * @param int $pos 0-based position in $code
152 *
153 * @return int 1-based column (relative to start of line)
154 */
155 private function toColumn(string $code, int $pos) : int {
142 if ($pos > strlen($code)) { 156 if ($pos > strlen($code)) {
143 throw new \RuntimeException('Invalid position information'); 157 throw new \RuntimeException('Invalid position information');
144 } 158 }
145 159
146 $lineStartPos = strrpos($code, "\n", $pos - strlen($code)); 160 $lineStartPos = strrpos($code, "\n", $pos - strlen($code));