Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
92.86% |
26 / 28 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
Less_Exception_Parser | |
92.86% |
26 / 28 |
|
80.00% |
4 / 5 |
16.09 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
getInput | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
5 | |||
genMessage | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
6 | |||
getLineNumber | |
60.00% |
3 / 5 |
|
0.00% |
0 / 1 |
3.58 | |||
getColumn | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | /** |
4 | * Parser Exception |
5 | */ |
6 | class Less_Exception_Parser extends Exception { |
7 | |
8 | /** |
9 | * The current file |
10 | * |
11 | * @var array |
12 | */ |
13 | public $currentFile; |
14 | |
15 | /** |
16 | * The current parser index |
17 | * |
18 | * @var int |
19 | */ |
20 | public $index; |
21 | |
22 | protected $input; |
23 | |
24 | protected $details = []; |
25 | |
26 | /** |
27 | * @param string|null $message |
28 | * @param Exception|null $previous Previous exception |
29 | * @param int|null $index The current parser index |
30 | * @param array|null $currentFile The file |
31 | * @param int $code The exception code |
32 | */ |
33 | public function __construct( $message = null, ?Exception $previous = null, $index = null, $currentFile = null, $code = 0 ) { |
34 | parent::__construct( $message, $code, $previous ); |
35 | |
36 | $this->currentFile = $currentFile; |
37 | $this->index = $index; |
38 | |
39 | $this->genMessage(); |
40 | } |
41 | |
42 | protected function getInput() { |
43 | if ( !$this->input && $this->currentFile && $this->currentFile['filename'] && file_exists( $this->currentFile['filename'] ) ) { |
44 | $this->input = file_get_contents( $this->currentFile['filename'] ); |
45 | } |
46 | } |
47 | |
48 | /** |
49 | * Set a message based on the exception info |
50 | */ |
51 | public function genMessage() { |
52 | if ( $this->currentFile && $this->currentFile['filename'] ) { |
53 | $this->message .= ' in ' . basename( $this->currentFile['filename'] ); |
54 | } |
55 | |
56 | if ( $this->index !== null ) { |
57 | $this->getInput(); |
58 | if ( $this->input ) { |
59 | $line = self::getLineNumber(); |
60 | $this->message .= ' on line ' . $line . ', column ' . self::getColumn(); |
61 | |
62 | $lines = explode( "\n", $this->input ); |
63 | |
64 | $count = count( $lines ); |
65 | $start_line = max( 0, $line - 3 ); |
66 | $last_line = min( $count, $start_line + 6 ); |
67 | $num_len = strlen( $last_line ); |
68 | for ( $i = $start_line; $i < $last_line; $i++ ) { |
69 | $this->message .= "\n" . str_pad( (string)( $i + 1 ), $num_len, '0', STR_PAD_LEFT ) . '| ' . $lines[$i]; |
70 | } |
71 | } |
72 | } |
73 | } |
74 | |
75 | /** |
76 | * Returns the line number the error was encountered |
77 | * |
78 | * @return int |
79 | */ |
80 | public function getLineNumber() { |
81 | if ( $this->index ) { |
82 | // https://bugs.php.net/bug.php?id=49790 |
83 | if ( ini_get( "mbstring.func_overload" ) ) { |
84 | return substr_count( substr( $this->input, 0, $this->index ), "\n" ) + 1; |
85 | } else { |
86 | return substr_count( $this->input, "\n", 0, $this->index ) + 1; |
87 | } |
88 | } |
89 | return 1; |
90 | } |
91 | |
92 | /** |
93 | * Returns the column the error was encountered |
94 | * |
95 | * @return int |
96 | */ |
97 | public function getColumn() { |
98 | $part = substr( $this->input, 0, $this->index ); |
99 | $pos = strrpos( $part, "\n" ); |
100 | return $this->index - $pos; |
101 | } |
102 | |
103 | } |