Completed
Push — master ( 2b6e28...3d0129 )
by
unknown
10:26
created

Contrib/less.php/Exception/Parser.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * Parser Exception
5
 *
6
 * @package Less
7
 * @subpackage exception
8
 */
9
class Less_Exception_Parser extends Exception{
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
10
11
	/**
12
	 * The current file
13
	 *
14
	 * @var Less_ImportedFile
15
	 */
16
	public $currentFile;
17
18
	/**
19
	 * The current parser index
20
	 *
21
	 * @var integer
22
	 */
23
	public $index;
24
25
	protected $input;
26
27
	protected $details = array();
28
29
30
	/**
31
	 * Constructor
32
	 *
33
	 * @param string $message
34
	 * @param Exception $previous Previous exception
35
	 * @param integer $index The current parser index
36
	 * @param Less_FileInfo|string $currentFile The file
37
	 * @param integer $code The exception code
38
	 */
39
	public function __construct($message = null, Exception $previous = null, $index = null, $currentFile = null, $code = 0){
40
41
		if (PHP_VERSION_ID < 50300) {
42
			$this->previous = $previous;
0 ignored issues
show
The property previous cannot be accessed from this context as it is declared private in class Exception.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
43
			parent::__construct($message, $code);
44
		} else {
45
			parent::__construct($message, $code, $previous);
46
		}
47
48
		$this->currentFile = $currentFile;
0 ignored issues
show
Documentation Bug introduced by
It seems like $currentFile can also be of type object<Less_FileInfo> or string. However, the property $currentFile is declared as type object<Less_ImportedFile>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
49
		$this->index = $index;
50
51
		$this->genMessage();
52
	}
53
54
55
	protected function getInput(){
56
57
		if( !$this->input && $this->currentFile && $this->currentFile['filename'] ){
58
			$this->input = file_get_contents( $this->currentFile['filename'] );
59
		}
60
	}
61
62
63
64
	/**
65
	 * Converts the exception to string
66
	 *
67
	 * @return string
68
	 */
69
	public function genMessage(){
70
71
		if( $this->currentFile && $this->currentFile['filename'] ){
72
			$this->message .= ' in '.basename($this->currentFile['filename']);
73
		}
74
75
		if( $this->index !== null ){
76
			$this->getInput();
77
			if( $this->input ){
78
				$line = self::getLineNumber();
79
				$this->message .= ' on line '.$line.', column '.self::getColumn();
80
81
				$lines = explode("\n",$this->input);
82
83
				$count = count($lines);
84
				$start_line = max(0, $line-3);
85
				$last_line = min($count, $start_line+6);
86
				$num_len = strlen($last_line);
87
				for( $i = $start_line; $i < $last_line; $i++ ){
88
					$this->message .= "\n".str_pad($i+1,$num_len,'0',STR_PAD_LEFT).'| '.$lines[$i];
89
				}
90
			}
91
		}
92
93
	}
94
95
	/**
96
	 * Returns the line number the error was encountered
97
	 *
98
	 * @return integer
99
	 */
100
	public function getLineNumber(){
101
		if( $this->index ){
102
			// https://bugs.php.net/bug.php?id=49790
103
			if (ini_get("mbstring.func_overload")) {
104
				return substr_count(substr($this->input, 0, $this->index), "\n") + 1;
105
			} else {
106
				return substr_count($this->input, "\n", 0, $this->index) + 1;
107
			}
108
		}
109
		return 1;
110
	}
111
112
113
	/**
114
	 * Returns the column the error was encountered
115
	 *
116
	 * @return integer
117
	 */
118
	public function getColumn(){
119
120
		$part = substr($this->input, 0, $this->index);
121
		$pos = strrpos($part,"\n");
122
		return $this->index - $pos;
123
	}
124
125
}
126