ThisUserCriteria   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A apply() 0 8 2
1
<?php
2
3
namespace App\Ship\Criterias\Eloquent;
4
5
use App\Ship\Parents\Criterias\Criteria;
6
use Illuminate\Support\Facades\Auth;
7
use Prettus\Repository\Contracts\RepositoryInterface as PrettusRepositoryInterface;
8
9
/**
10
 * Class ThisUserCriteria.
11
 *
12
 * @author  Mahmoud Zalt <[email protected]>
13
 */
14
class ThisUserCriteria extends Criteria
15
{
16
17
    /**
18
     * @var int
19
     */
20
    private $userId;
21
22
    /**
23
     * ThisUserCriteria constructor.
24
     *
25
     * @param $userId
26
     */
27
    public function __construct($userId = null)
28
    {
29
        $this->userId = $userId;
30
    }
31
32
    /**
33
     * @param                                                   $model
34
     * @param \Prettus\Repository\Contracts\RepositoryInterface $repository
35
     *
36
     * @return mixed
37
     */
38
    public function apply($model, PrettusRepositoryInterface $repository)
39
    {
40
        if(!$this->userId){
41
            $this->userId = Auth::user()->id;
0 ignored issues
show
Bug introduced by Mahmoud Zalt
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
42
        }
43
44
        return $model->where('user_id', '=', $this->userId);
45
    }
46
47
}
48