Completed
Pull Request — develop (#38)
by Neil
08:28
created

SetViewVariable::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
namespace App\Http\Middleware;
3
4
use Closure;
5
use Illuminate\Contracts\Auth\Guard;
6
use Dingo\Api\Http;
7
use Dingo\Api\Routing\Helpers;
8
use Illuminate\Http\Request;
9
10
class SetViewVariable
11
{
12
13
    use Helpers;
14
15
    protected $auth;
16
17
    public function __construct(Guard $auth)
18
    {
19
        $this->auth = $auth;
20
    }
21
22
    public function handle(Request $request, Closure $next)
23
    {
24
        if ($request->user())
25
        {
26
            $notifications_count = $this->api->be(auth()->user())->get('/api/notifications');
0 ignored issues
show
Bug introduced by
The method user() does not seem to exist on object<Illuminate\Contracts\Auth\Factory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
27
            view()->share('notifications_count', $notifications_count);
0 ignored issues
show
Bug introduced by
The method share does only exist in Illuminate\Contracts\View\Factory, but not in Illuminate\View\View.

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...
28
        }
29
        return $next($request);
30
    }
31
32
}
33