These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * ODataParams class |
||
4 | * |
||
5 | * This file describes the ODataParams class |
||
6 | * |
||
7 | * PHP version 5 and 7 |
||
8 | * |
||
9 | * @author Patrick Boyd / [email protected] |
||
10 | * @copyright Copyright (c) 2017, Austin Artistic Reconstruction |
||
11 | * @license http://www.apache.org/licenses/ Apache 2.0 License |
||
12 | */ |
||
13 | |||
14 | /** |
||
15 | * A class representing OData style URI query string parameters. |
||
16 | * |
||
17 | * The REST APIs use OData style URI query string parameters. |
||
18 | * This class abstracts parsing of those into a more PHP friendly format. |
||
19 | */ |
||
20 | class ODataParams |
||
21 | { |
||
22 | /** |
||
23 | * The ODataFilter or false if not set |
||
24 | * @var false|Data\Filter |
||
25 | */ |
||
26 | public $filter = false; |
||
27 | /** |
||
28 | * An array of properties to expand or false if not set |
||
29 | * @var false|array |
||
30 | */ |
||
31 | public $expand = false; |
||
32 | /** |
||
33 | * An array of properties to display or false if not set |
||
34 | * @var false|array |
||
35 | */ |
||
36 | public $select = false; |
||
37 | /** |
||
38 | * An array of properties to sort by or false if not set |
||
39 | * @var false|array |
||
40 | */ |
||
41 | public $orderby = false; |
||
42 | /** |
||
43 | * The number of results to display or false if not set |
||
44 | * @var false|integer |
||
45 | */ |
||
46 | public $top = false; |
||
47 | /** |
||
48 | * The number of results to skip or false if not set |
||
49 | * @var false|integer |
||
50 | */ |
||
51 | public $skip = false; |
||
52 | /** |
||
53 | * Display the count of results |
||
54 | * @var boolean |
||
55 | */ |
||
56 | public $count = false; |
||
57 | /** |
||
58 | * Not yet implemented |
||
59 | */ |
||
60 | public $search = false; |
||
61 | |||
62 | /** |
||
63 | * Parse the parameter array into an ODataParams instance |
||
64 | * |
||
65 | * @param string[] $params An key=>value array of strings representing the query string. |
||
66 | */ |
||
67 | public function __construct($params) |
||
68 | { |
||
69 | $this->processFilter($params); |
||
70 | $this->processExpand($params); |
||
71 | $this->processSelect($params); |
||
72 | $this->processOrderBy($params); |
||
73 | $this->processTop($params); |
||
74 | $this->processSkip($params); |
||
75 | $this->processCount($params); |
||
76 | $this->processSearch($params); |
||
0 ignored issues
–
show
|
|||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Take the parameter array and find the Filter parameter and convert that to a \Data\Filter if present |
||
81 | * |
||
82 | * @param string[] $params An key=>value array of strings representing the query string. |
||
83 | */ |
||
84 | protected function processFilter($params) |
||
85 | { |
||
86 | if(isset($params['filter'])) |
||
87 | { |
||
88 | $this->filter = new \Data\Filter($params['filter']); |
||
89 | } |
||
90 | else if(isset($params['$filter'])) |
||
91 | { |
||
92 | $this->filter = new \Data\Filter($params['$filter']); |
||
93 | } |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Take the parameter array and find the Expand parameter and convert it to a PHP array |
||
98 | * |
||
99 | * @param string[] $params An key=>value array of strings representing the query string. |
||
100 | */ |
||
101 | protected function processExpand($params) |
||
102 | { |
||
103 | if(isset($params['$expand'])) |
||
104 | { |
||
105 | $this->expand = explode(',', $params['$expand']); |
||
106 | } |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Take the parameter array and find the Select parameter and convert it to a PHP array |
||
111 | * |
||
112 | * @param string[] $params An key=>value array of strings representing the query string. |
||
113 | */ |
||
114 | protected function processSelect($params) |
||
115 | { |
||
116 | if(isset($params['select'])) |
||
117 | { |
||
118 | $this->select = explode(',', $params['select']); |
||
119 | } |
||
120 | else if(isset($params['$select'])) |
||
121 | { |
||
122 | $this->select = explode(',', $params['$select']); |
||
123 | } |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Take the parameter array and find the OrderBy parameter and convert it to a PHP array |
||
128 | * |
||
129 | * @param string[] $params An key=>value array of strings representing the query string. |
||
130 | */ |
||
131 | protected function processOrderBy($params) |
||
132 | { |
||
133 | if(isset($params['$orderby'])) |
||
134 | { |
||
135 | $this->orderby = array(); |
||
136 | $orderby = explode(',', $params['$orderby']); |
||
137 | $count = count($orderby); |
||
138 | for($i = 0; $i < $count; $i++) |
||
139 | { |
||
140 | $exp = explode(' ', $orderby[$i]); |
||
141 | if(count($exp) === 1) |
||
142 | { |
||
143 | //Default to assending |
||
144 | $this->orderby[$exp[0]] = 1; |
||
145 | } |
||
146 | else |
||
147 | { |
||
148 | switch($exp[1]) |
||
149 | { |
||
150 | case 'asc': |
||
151 | $this->orderby[$exp[0]] = 1; |
||
152 | break; |
||
153 | case 'desc': |
||
154 | $this->orderby[$exp[0]] = -1; |
||
155 | break; |
||
156 | default: |
||
157 | throw new Exception('Unknown orderby operation'); |
||
158 | } |
||
159 | } |
||
160 | } |
||
161 | } |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Take the parameter array and find the Top parameter and convert it to an int |
||
166 | * |
||
167 | * @param string[] $params An key=>value array of strings representing the query string. |
||
168 | */ |
||
169 | protected function processTop($params) |
||
170 | { |
||
171 | if(isset($params['$top']) && is_numeric($params['$top'])) |
||
172 | { |
||
173 | $this->top = intval($params['$top']); |
||
174 | } |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Take the parameter array and find the Skip parameter and convert it to an int |
||
179 | * |
||
180 | * @param string[] $params An key=>value array of strings representing the query string. |
||
181 | */ |
||
182 | protected function processSkip($params) |
||
183 | { |
||
184 | if(isset($params['$skip']) && is_numeric($params['$skip'])) |
||
185 | { |
||
186 | $this->skip = intval($params['$skip']); |
||
187 | } |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Take the parameter array and find the Count parameter and convert it to a boolean |
||
192 | * |
||
193 | * @param string[] $params An key=>value array of strings representing the query string. |
||
194 | */ |
||
195 | protected function processCount($params) |
||
196 | { |
||
197 | if(isset($params['$count']) && strcasecmp($params['$count'], 'true') === 0) |
||
198 | { |
||
199 | $this->count = true; |
||
200 | } |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Take the parameter array and find the Search parameter and process it |
||
205 | * |
||
206 | * @param string[] $params An key=>value array of strings representing the query string. |
||
207 | */ |
||
208 | protected function processSearch($params) |
||
209 | { |
||
210 | if(isset($params['$search'])) |
||
0 ignored issues
–
show
This
if statement is empty and can be removed.
This check looks for the bodies of These if (rand(1, 6) > 3) {
//print "Check failed";
} else {
print "Check succeeded";
}
could be turned into if (rand(1, 6) <= 3) {
print "Check succeeded";
}
This is much more concise to read. ![]() |
|||
211 | { |
||
212 | //throw new Exception('Search not yet implemented'); |
||
0 ignored issues
–
show
Unused Code
Comprehensibility
introduced
by
67% of this comment could be valid code. Did you maybe forget this after debugging?
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it. The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production. This check looks for comments that seem to be mostly valid code and reports them. ![]() |
|||
213 | } |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Take an input array and filter the array based on the select parameter |
||
218 | * |
||
219 | * @param array $array The array to be filtered |
||
220 | * |
||
221 | * @return array The filtered array |
||
222 | */ |
||
223 | public function filterArrayPerSelect($array) |
||
224 | { |
||
225 | if($this->select === false) |
||
226 | { |
||
227 | return $array; |
||
228 | } |
||
229 | $flip = array_flip($this->select); |
||
230 | $count = count($array); |
||
231 | for($i = 0; $i < $count; $i++) |
||
232 | { |
||
233 | if(is_a($array[$i], 'SerializableObject')) |
||
234 | { |
||
235 | $array[$i] = array_intersect_key($array[$i]->jsonSerialize(), $flip); |
||
236 | continue; |
||
237 | } |
||
238 | $array[$i] = array_intersect_key($array[$i], $flip); |
||
239 | } |
||
240 | return $array; |
||
241 | } |
||
242 | } |
||
243 |
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: