LaraCedObserver::getUserId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace LaraCed;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
class LaraCedObserver
8
{
9
    /**
10
     * Get User id.
11
     *
12
     * @return int
13
     */
14
    protected function getUserId()
15
    {
16
        return auth()->id();
0 ignored issues
show
Bug introduced by
The method id 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...
17
    }
18
19
    public function creating(Model $model)
20
    {
21
        $model->{$model->getCreatorColumn()}
22
        = $this->getUserId();
23
    }
24
25
    public function updating(Model $model)
26
    {
27
        if ($model->isDirty($model->{$model->getDestroyerColumn()})) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
28
            //do nothing
29
        } elseif ($model->isDirty()) {
30
            $model->{$model->getEditorColumn()}
31
            = $this->getUserId();
32
        }
33
    }
34
35
    public function deleting(Model $model)
36
    {
37
        $model->{$model->getDestroyerColumn()}
38
        = $this->getUserId();
39
        $model->save();
40
    }
41
}
42