Completed
Pull Request — master (#6)
by
unknown
01:35
created

Statable::addHistoryLine()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 1
1
<?php
2
3
namespace Sebdesign\SM\Traits;
4
5
use SM\Factory\FactoryInterface;
6
use SM\StateMachine\StateMachine;
7
use Illuminate\Database\Eloquent\Model;
8
9
trait Statable
10
{
11
    /**
12
     * @var StateMachine
13
     */
14
    protected $stateMachine;
15
16
    public function history()
17
    {
18
        if ($this->isEloquent()) {
19
            return $this->hasMany(self::HISTORY_MODEL_NAME);
0 ignored issues
show
Bug introduced by
It seems like hasMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
20
        }
21
22
        /** @var Model $model */
23
        $model = app(self::HISTORY_MODEL_NAME);
24
25
        return $model->where(self::HISTORY_MODEL_FOREIGN_KEY, $this->{self::PRIMARY_KEY});
26
    }
27
28
    public function addHistoryLine(array $transitionData)
29
    {
30
        $transitionData['user_id'] = 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...
31
32
        if ($this->isEloquent()) {
33
            $this->save();
0 ignored issues
show
Bug introduced by
It seems like save() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
34
35
            return $this->history()->create($transitionData);
36
        }
37
38
        $transitionData[self::HISTORY_MODEL_FOREIGN_KEY] = $this->{self::PRIMARY_KEY};
39
        /** @var Model $model */
40
        $model = app(self::HISTORY_MODEL_NAME);
41
42
        return $model->create($transitionData);
0 ignored issues
show
Bug introduced by
The method create() does not exist on Illuminate\Database\Eloquent\Model. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
43
    }
44
45
    public function stateIs()
46
    {
47
        return $this->StateMachine()->getState();
48
    }
49
50
    public function transition($transition)
51
    {
52
        return $this->stateMachine()->apply($transition);
53
    }
54
55
    public function transitionAllowed($transition)
56
    {
57
        return $this->StateMachine()->can($transition);
58
    }
59
60
    /**
61
     * @return StateMachine
62
     */
63
    public function stateMachine()
64
    {
65
        if (! $this->stateMachine) {
66
            $this->stateMachine = app(FactoryInterface::class)->get($this, self::SM_CONFIG);
67
        }
68
69
        return $this->stateMachine;
70
    }
71
72
    public function isEloquent()
73
    {
74
        return $this instanceof Model;
75
    }
76
}
77