Completed
Pull Request — master (#90)
by
unknown
17:00
created

ShortcodeMiddleware::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 14
rs 9.4285
1
<?php
2
3
namespace Modules\Boats\Http\Middleware;
4
5
use Closure;
6
7
class ShortcodeMiddleware {
8
9
    /**
10
     * Intercept a response to replace shortcodes
11
     *
12
     * @param  \Illuminate\Http\Request  $request
13
     * @param  \Closure  $next
14
     * @return mixed
15
     */
16
    public function handle($request, Closure $next)
17
    {
18
        $response = $next($request);
19
20
        $shotcodes = config('asgard.core.shortcodes');
21
22
        foreach ($shotcodes as $shortcode => $options) {
23
            $response->setContent(
24
                str_replace("[$shortcode]", view($options['view'], $options['data'])->render(), $response->getContent())
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...
25
            );
26
        }
27
28
    	return $response;
29
    }
30
    
31
}
32