Completed
Pull Request — 2.0 (#98)
by
unknown
22:52
created

ShortcodeMiddleware::handle()   C

Complexity

Conditions 9
Paths 11

Size

Total Lines 52
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 23
c 1
b 0
f 0
nc 11
nop 2
dl 0
loc 52
rs 6.5703

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
namespace Modules\Core\Http\Middleware;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
8
class ShortcodeMiddleware
9
{
10
    /**
11
     * Intercept a response to replace shortcodes
12
     *
13
     * @param  \Illuminate\Http\Request $request
14
     * @param  \Closure $next
15
     * @return mixed
16
     */
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+=&quot;.*&quot;/', $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, '&quot;'));
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();
0 ignored issues
show
Bug introduced by
The method render does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
56
                    } else {
57
                        // If there is no view configured, it assumes that the callback himself is rendering the view
58
                        $view = $results;
0 ignored issues
show
Bug introduced by
The variable $results does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
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
70
}
71