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