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