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

ShortcodeMiddleware   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
rs 10
wmc 2
lcom 0
cbo 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 14 2
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