Statable::saveBeforeTransition()   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 Iben\Statable;
4
5
use Iben\Statable\Models\StateHistory;
6
use Illuminate\Support\Facades\Config;
7
use Sebdesign\SM\StateMachine\StateMachine;
8
9
trait Statable
10
{
11
    /**
12
     * @var StateMachine
13
     */
14
    protected $SM;
15
16
    /**
17
     * @return \Illuminate\Database\Eloquent\Model
18
     */
19
    public function stateHistory()
20
    {
21
        return $this->morphMany(
0 ignored issues
show
Bug introduced by
It seems like morphMany() 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...
22
            Config::get('laravel-statable.models.state_history', StateHistory::class),
23
            'statable'
24
        );
25
    }
26
27
    /**
28
     * @param array $transitionData
29
     */
30
    public function addHistoryLine(array $transitionData)
31
    {
32
        if ($this->getKey()) {
0 ignored issues
show
Bug introduced by
It seems like getKey() 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...
33
            $transitionData['actor_id'] = $this->getActorId();
34
            $this->stateHistory()->create($transitionData);
35
        }
36
    }
37
38
    /**
39
     * @return int|null
40
     */
41
    public function getActorId()
42
    {
43
        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...
44
    }
45
46
    /**
47
     * @return mixed|string
48
     * @throws \Illuminate\Container\EntryNotFoundException
49
     */
50
    public function stateIs()
51
    {
52
        return $this->stateMachine()->getState();
53
    }
54
55
    /**
56
     * @param $transition
57
     * @param bool $soft
58
     * @param array $context
59
     * @return bool
60
     * @throws \Illuminate\Container\EntryNotFoundException
61
     * @throws \SM\SMException
62
     */
63
    public function apply($transition, $soft = false, $context = [])
64
    {
65
        if ($this->getKey() === null && $this->saveBeforeTransition()) {
0 ignored issues
show
Bug introduced by
It seems like getKey() 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...
66
            $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...
67
        }
68
69
        return $this->stateMachine()->apply($transition, $soft, $context);
70
    }
71
72
    /**
73
     * @param $transition
74
     * @param array $context
75
     * @return bool
76
     * @throws \Illuminate\Container\EntryNotFoundException
77
     * @throws \SM\SMException
78
     */
79
    public function canApply($transition, $context = [])
80
    {
81
        return $this->stateMachine()->can($transition, $context);
82
    }
83
84
    /**
85
     * @return mixed|\Sebdesign\SM\StateMachine\StateMachine
86
     * @throws \Illuminate\Container\EntryNotFoundException
87
     */
88
    public function stateMachine()
89
    {
90
        if (! $this->SM) {
91
            $this->SM = app('sm.factory')->get($this, $this->getGraph());
92
        }
93
94
        return $this->SM;
95
    }
96
97
    /**
98
     * @return string
99
     */
100
    protected function getGraph()
101
    {
102
        return 'default';
103
    }
104
105
    /**
106
     * @return bool
107
     */
108
    protected function saveBeforeTransition()
109
    {
110
        return false;
111
    }
112
}
113