Completed
Pull Request — master (#6)
by
unknown
09:19
created

Statable::stateIs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Sebdesign\SM\Traits;
4
5
use SM\StateMachine\StateMachine;
6
use Sebdesign\SM\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->id) {
0 ignored issues
show
Bug introduced by
The property id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
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 transition($transition)
57
    {
58
        return $this->stateMachine()->apply($transition);
59
    }
60
61
    /**
62
     * @param $transition
63
     * @return bool
64
     * @throws \SM\SMException|\Illuminate\Container\EntryNotFoundException
65
     */
66
    public function transitionAllowed($transition)
67
    {
68
        return $this->stateMachine()->can($transition);
69
    }
70
71
    /**
72
     * @return mixed|\SM\StateMachine\StateMachine
73
     * @throws \Illuminate\Container\EntryNotFoundException
74
     */
75
    public function stateMachine()
76
    {
77
        if (! $this->SM) {
78
            $this->SM = app('sm.factory')->get($this, $this->getGraph());
79
        }
80
81
        return $this->SM;
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    protected function getGraph()
88
    {
89
        return 'default';
90
    }
91
}
92