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 | Copyrights for code authored by Yahoo! Inc. is licensed under the following terms: |
||
5 | MIT License |
||
6 | Copyright (c) 2013-2016 Yahoo! Inc. All Rights Reserved. |
||
7 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
||
8 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
||
9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
||
10 | |||
11 | Origin: https://github.com/zordius/lightncandy |
||
12 | */ |
||
13 | |||
14 | /** |
||
15 | * file of LightnCandy Expression handler |
||
16 | * |
||
17 | * @package LightnCandy |
||
18 | * @author Zordius <[email protected]> |
||
19 | */ |
||
20 | |||
21 | namespace LightnCandy; |
||
22 | |||
23 | use \LightnCandy\Validator; |
||
24 | use \LightnCandy\Token; |
||
25 | |||
26 | /** |
||
27 | * LightnCandy Expression handler |
||
28 | */ |
||
29 | class Expression |
||
30 | { |
||
31 | /** |
||
32 | * return 'true' or 'false' string. |
||
33 | * |
||
34 | * @param integer $v value |
||
35 | * |
||
36 | * @return string 'true' when the value larger then 0 |
||
37 | * |
||
38 | * @expect 'true' when input 1 |
||
39 | * @expect 'true' when input 999 |
||
40 | * @expect 'false' when input 0 |
||
41 | * @expect 'false' when input -1 |
||
42 | */ |
||
43 | 688 | public static function boolString($v) { |
|
44 | 688 | return ($v > 0) ? 'true' : 'false'; |
|
45 | } |
||
46 | |||
47 | /** |
||
48 | * Get string presentation for a string list |
||
49 | * |
||
50 | * @param array<string> $list an array of strings. |
||
51 | * |
||
52 | * @return string PHP list string |
||
53 | * |
||
54 | * @expect '' when input array() |
||
55 | * @expect "'a'" when input array('a') |
||
56 | * @expect "'a','b','c'" when input array('a', 'b', 'c') |
||
57 | */ |
||
58 | 389 | public static function listString($list) { |
|
59 | return implode(',', (array_map(function ($v) { |
||
60 | 388 | return "'$v'"; |
|
61 | 389 | }, $list))); |
|
62 | } |
||
63 | |||
64 | /** |
||
65 | * Get string presentation for an array |
||
66 | * |
||
67 | * @param array<string> $list an array of variable names. |
||
68 | * |
||
69 | * @return string PHP array names string |
||
70 | * |
||
71 | * @expect '' when input array() |
||
72 | * @expect "['a']" when input array('a') |
||
73 | * @expect "['a']['b']['c']" when input array('a', 'b', 'c') |
||
74 | */ |
||
75 | 125 | public static function arrayString($list) { |
|
76 | return implode('', (array_map(function ($v) { |
||
77 | 123 | return "['$v']"; |
|
78 | 125 | }, $list))); |
|
79 | } |
||
80 | |||
81 | /** |
||
82 | * Analyze an expression |
||
83 | * |
||
84 | * @param array<string,array|string|integer> $context Current context |
||
85 | * @param array<array|string|integer> $var variable parsed path |
||
86 | * |
||
87 | * @return array<integer|boolean|array> analyzed result |
||
0 ignored issues
–
show
|
|||
88 | * |
||
89 | * @expect array(0, false, array('foo')) when input array('flags' => array('spvar' => 0)), array(0, 'foo') |
||
90 | * @expect array(1, false, array('foo')) when input array('flags' => array('spvar' => 0)), array(1, 'foo') |
||
91 | */ |
||
92 | 617 | public static function analyze($context, $var) { |
|
93 | 617 | $levels = 0; |
|
94 | 617 | $spvar = false; |
|
95 | |||
96 | 617 | if (isset($var[0])) { |
|
97 | // trace to parent |
||
98 | 583 | if (!is_string($var[0]) && is_int($var[0])) { |
|
99 | 50 | $levels = array_shift($var); |
|
100 | } |
||
101 | } |
||
102 | |||
103 | 617 | if (isset($var[0])) { |
|
104 | // handle @root, @index, @key, @last, etc |
||
105 | 580 | if ($context['flags']['spvar']) { |
|
106 | 410 | if (substr($var[0], 0, 1) === '@') { |
|
107 | 38 | $spvar = true; |
|
108 | 38 | $var[0] = substr($var[0], 1); |
|
109 | } |
||
110 | } |
||
111 | } |
||
112 | |||
113 | 617 | return array($levels, $spvar, $var); |
|
114 | } |
||
115 | |||
116 | /** |
||
117 | * get normalized handlebars expression for a variable |
||
118 | * |
||
119 | * @param integer $levels trace N levels top parent scope |
||
120 | * @param boolean $spvar is the path start with @ or not |
||
121 | * @param array<string|integer> $var variable parsed path |
||
122 | * |
||
123 | * @return string normalized expression for debug display |
||
124 | * |
||
125 | * @expect '[a].[b]' when input 0, false, array('a', 'b') |
||
126 | * @expect '@[root]' when input 0, true, array('root') |
||
127 | * @expect 'this' when input 0, false, null |
||
128 | * @expect 'this.[id]' when input 0, false, array(null, 'id') |
||
129 | * @expect '@[root].[a].[b]' when input 0, true, array('root', 'a', 'b') |
||
130 | * @expect '../../[a].[b]' when input 2, false, array('a', 'b') |
||
131 | * @expect '../[a\'b]' when input 1, false, array('a\'b') |
||
132 | */ |
||
133 | public static function toString($levels, $spvar, $var) { |
||
134 | 617 | return ($spvar ? '@' : '') . str_repeat('../', $levels) . ((is_array($var) && count($var)) ? implode('.', array_map(function($v) { |
|
135 | 580 | return ($v === null) ? 'this' : "[$v]"; |
|
136 | 617 | }, $var)) : 'this'); |
|
137 | } |
||
138 | } |
||
139 | |||
140 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.