Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
27 / 27 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
Less_Tree_Variable | |
100.00% |
27 / 27 |
|
100.00% |
2 / 2 |
10 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
compile | |
100.00% |
24 / 24 |
|
100.00% |
1 / 1 |
9 |
1 | <?php |
2 | /** |
3 | * @private |
4 | */ |
5 | class Less_Tree_Variable extends Less_Tree { |
6 | |
7 | public $name; |
8 | public $index; |
9 | public $currentFileInfo; |
10 | public $evaluating = false; |
11 | |
12 | /** |
13 | * @param string $name |
14 | */ |
15 | public function __construct( $name, $index = null, $currentFileInfo = null ) { |
16 | $this->name = $name; |
17 | $this->index = $index; |
18 | $this->currentFileInfo = $currentFileInfo; |
19 | } |
20 | |
21 | /** |
22 | * @param Less_Environment $env |
23 | * @return Less_Tree|Less_Tree_Keyword|Less_Tree_Quoted |
24 | * @see less-3.13.1.js#Variable.prototype.eval |
25 | */ |
26 | public function compile( $env ) { |
27 | // Optimization: Less.js checks if string starts with @@, we only check if second char is @ |
28 | if ( $this->name[1] === '@' ) { |
29 | $v = new self( substr( $this->name, 1 ), $this->index + 1, $this->currentFileInfo ); |
30 | // While some Less_Tree nodes have no 'value', we know these can't occur after a |
31 | // variable assignment (would have been a ParseError). |
32 | $name = '@' . $v->compile( $env )->value; |
33 | } else { |
34 | $name = $this->name; |
35 | } |
36 | |
37 | if ( $this->evaluating ) { |
38 | throw new Less_Exception_Compiler( "Recursive variable definition for " . $name, null, $this->index, $this->currentFileInfo ); |
39 | } |
40 | |
41 | $this->evaluating = true; |
42 | $variable = null; |
43 | foreach ( $env->frames as $frame ) { |
44 | /** @var Less_Tree_Ruleset $frame */ |
45 | $v = $frame->variable( $name ); |
46 | if ( $v ) { |
47 | if ( isset( $v->important ) && $v->important ) { |
48 | $importantScopeLength = count( $env->importantScope ); |
49 | $env->importantScope[ $importantScopeLength - 1 ]['important'] = $v->important; |
50 | } |
51 | // If in calc, wrap vars in a function call to cascade evaluate args first |
52 | if ( $env->inCalc ) { |
53 | $call = new Less_Tree_Call( '_SELF', [ $v->value ], $this->index, $this->currentFileInfo ); |
54 | $variable = $call->compile( $env ); |
55 | break; |
56 | } else { |
57 | $variable = $v->value->compile( $env ); |
58 | break; |
59 | } |
60 | } |
61 | } |
62 | if ( $variable ) { |
63 | $this->evaluating = false; |
64 | return $variable; |
65 | } |
66 | |
67 | throw new Less_Exception_Compiler( "variable " . $name . " is undefined in file " . $this->currentFileInfo["filename"], null, $this->index, $this->currentFileInfo ); |
68 | } |
69 | |
70 | } |