| Conditions | 9 |
| Paths | 11 |
| Total Lines | 52 |
| Code Lines | 23 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 17 | public function handle($request, Closure $next) |
||
| 18 | { |
||
| 19 | $response = $next($request); |
||
| 20 | |||
| 21 | // Get defined shortcodes from the config file |
||
| 22 | $shotcodesDefinitions = config('asgard.core.shortcodes', []); |
||
| 23 | |||
| 24 | foreach ($shotcodesDefinitions as $shortcodeName => $options) { |
||
| 25 | |||
| 26 | // Search for shortcodes in the response's content |
||
| 27 | if (preg_match("/\[$shortcodeName.*\]/", $response->getContent(), $shortcodes)) { |
||
| 28 | foreach ($shortcodes as $shortcode) { |
||
| 29 | |||
| 30 | // Search for parameters |
||
| 31 | preg_match('/\w+=".*"/', $shortcode, $rawParameters); |
||
| 32 | |||
| 33 | if (! empty($rawParameters)) { |
||
| 34 | $parameters = new Request(); |
||
| 35 | |||
| 36 | // Parse every parameters and put them in a Request object |
||
| 37 | foreach ($rawParameters as $rawParameter) { |
||
| 38 | list($name, $value) = explode('=', $rawParameter); |
||
| 39 | |||
| 40 | $parameters->query->set($name, trim($value, '"')); |
||
| 41 | } |
||
| 42 | |||
| 43 | if (array_key_exists('callback', $options)) { |
||
| 44 | // Call a function to interpret parameters and get the results as an array |
||
| 45 | $results = call_user_func($options['callback'], $parameters); |
||
| 46 | } |
||
| 47 | } else { |
||
| 48 | if (array_key_exists('callback', $options)) { |
||
| 49 | // Call a function to interpret parameters and get the results as an array |
||
| 50 | $results = call_user_func($options['callback']); |
||
| 51 | } |
||
| 52 | } |
||
| 53 | |||
| 54 | if (isset($options['view'])) { |
||
| 55 | $view = view($options['view'], compact('results'))->render(); |
||
|
|
|||
| 56 | } else { |
||
| 57 | // If there is no view configured, it assumes that the callback himself is rendering the view |
||
| 58 | $view = $results; |
||
| 59 | } |
||
| 60 | |||
| 61 | // Replace the shortcode by the corresponfing view and datas |
||
| 62 | $response->setContent(preg_replace("/\[$shortcodeName.*\]/", $view, $response->getContent())); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | return $response; |
||
| 68 | } |
||
| 69 | |||
| 71 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: