Completed
Push — master ( 1c25ec...c649d9 )
by Bence
09:35
created

Statable::saveBeforeTransition()   A

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