Completed
Pull Request — master (#372)
by Mike
31:01 queued 16:07
created

NameBeginsWith   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 8
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A apply() 0 5 1
1
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures\Scopes;
2
3
use Illuminate\Database\Eloquent\Scope;
4
use Illuminate\Database\Eloquent\Model;
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Support\Str;
7
8
class NameBeginsWith implements Scope
9
{
10
    public function apply(Builder $builder, Model $model)
11
    {
12
        $letter = (new Str)->substr(auth()->user()->name, 0, 1);
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\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...
13
        $builder->where('name', 'LIKE', "{$letter}%");
14
    }
15
}
16