Conditions | 19 |
Paths | 24 |
Total Lines | 121 |
Code Lines | 56 |
Lines | 0 |
Ratio | 0 % |
Changes | 6 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
203 | public function isEndpoint($api, $endpoint) |
||
204 | { |
||
205 | /** |
||
206 | * Return if it's a system endpoint |
||
207 | */ |
||
208 | if (null !== ($endpointSettings = $this->isSystemEndpoint($api, $endpoint))) { |
||
209 | return $endpointSettings; |
||
210 | } |
||
211 | |||
212 | $endpoint = htmlspecialchars($endpoint, ENT_QUOTES, 'UTF-8'); |
||
213 | $index = array_search($api, $this->BASE_ENDPOINTS); |
||
214 | |||
215 | if ($index !== false) { |
||
216 | if (isset($this->registry[$api])) { |
||
217 | $endpointSettings = array( |
||
218 | 'path' => $endpoint, |
||
219 | 'model' => array( |
||
220 | 'namespace' => $this->NAMESPACE_ENDPOINTS[$api], |
||
221 | 'class' => $this->BASE_ENDPOINTS[$index], |
||
222 | 'method' => basename($endpoint), |
||
223 | 'scope' => 'private', |
||
224 | ), |
||
225 | ); |
||
226 | |||
227 | /** |
||
228 | * Nothing dynamic, found an exact match |
||
229 | * @var array |
||
230 | */ |
||
231 | if (array_search($endpoint, $this->registry[$api]) !== false) { |
||
232 | if( method_exists($this->NAMESPACE_ENDPOINTS[$api], 'scope') ) { |
||
233 | $classScope = (new $this->NAMESPACE_ENDPOINTS[$api])->scope(); |
||
234 | $position = array_search($endpoint, $this->registry[$api]); |
||
235 | |||
236 | if( is_array($classScope) && isset($classScope[$position]) ) { |
||
237 | $endpointSettings['model']['scope'] = $classScope[$position]; |
||
238 | |||
239 | }else{ |
||
240 | |||
241 | if( !is_array($classScope) ) { |
||
242 | $endpointSettings['model']['scope'] = $classScope; |
||
243 | } |
||
244 | } |
||
245 | } |
||
246 | return (object) $endpointSettings; |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * Check for dynamic uri eg: {asset_id} |
||
251 | * Dynamic uri's must be wrapped in {} for a true return |
||
252 | */ |
||
253 | foreach ($this->registry[$api] as $i => $path) { |
||
254 | $endpointRegister = $path; |
||
255 | $methodArgs = []; |
||
256 | |||
257 | /** |
||
258 | * If comparing the two sizes are not equal |
||
259 | * then no use continuing through the loop |
||
260 | */ |
||
261 | if (!$this->uriCheckSize($endpointRegister, $endpoint)) { |
||
262 | continue; |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * This replacment will create a pattern to use as a match all expression |
||
267 | * @var [string] |
||
268 | */ |
||
269 | $endpointRegister = preg_replace('@/{(.*?)}@', '/(.*?)', $endpointRegister); |
||
270 | |||
271 | if (preg_match_all('@^' . $endpointRegister . '$@i', $endpoint, $dynamicParts)) { |
||
272 | $endpointFilter = $this->filterParts($endpoint, $dynamicParts); |
||
273 | $model = $this->getClassModel($path); |
||
274 | |||
275 | /** |
||
276 | * Find the dynamic parts and set them as argument key(s) |
||
277 | * then combine them with the endpoint request and set the request parts |
||
278 | * as argument value(s) |
||
279 | */ |
||
280 | if (preg_match_all("/(?<={).*?(?=})/", $path, $registerParts)) { |
||
281 | if (isset($registerParts[0][0])) { |
||
282 | $registerParts = $registerParts[0]; |
||
283 | |||
284 | if (sizeof($endpointFilter) == sizeof($registerParts)) { |
||
285 | $methodArgs = array_combine($registerParts, $endpointFilter); |
||
286 | } |
||
287 | } |
||
288 | } |
||
289 | |||
290 | $scope = 'private'; |
||
291 | |||
292 | if( method_exists($this->NAMESPACE_ENDPOINTS[$api], 'scope') ) { |
||
293 | $classScope = (new $this->NAMESPACE_ENDPOINTS[$api])->scope(); |
||
294 | $position = array_search($path, $this->registry[$api]); |
||
295 | |||
296 | if( is_array($classScope) && isset($classScope[$position]) ) { |
||
297 | $scope = $classScope[$position]; |
||
298 | |||
299 | }else{ |
||
300 | |||
301 | if( !is_array($classScope) ) { |
||
302 | $scope = $classScope; |
||
303 | } |
||
304 | } |
||
305 | } |
||
306 | |||
307 | $endpointSettings['model'] = array( |
||
308 | 'scope' => $scope, |
||
309 | 'namespace' => $this->NAMESPACE_ENDPOINTS[$api], |
||
310 | 'class' => $model['class'], |
||
311 | 'method' => $model['method'], |
||
312 | 'arguments' => $methodArgs, |
||
313 | ); |
||
314 | |||
315 | return (object) $endpointSettings; |
||
316 | } else { |
||
317 | continue; |
||
318 | } |
||
319 | } |
||
320 | } |
||
321 | } |
||
322 | |||
323 | return; |
||
324 | } |
||
382 |