GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Statemachine::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3
Metric Value
dl 0
loc 23
ccs 11
cts 11
cp 1
rs 9.0856
cc 3
eloc 14
nc 4
nop 3
crap 3
1
<?php
2
3
namespace Noodle\Statemachine;
4
5
use ArrayObject as Context;
6
use Generator;
7
use League\Event\Emitter;
8
use League\Event\Event;
9
use League\Event\ListenerInterface as Listener;
10
use Noodle\Listener\ChangesState;
11
use Noodle\Listener\ReportsTransitionFailures;
12
use Noodle\State\FlyweightState;
13
use Noodle\State\State;
14
use Noodle\Stateful\Stateful;
15
use Noodle\Statemachine\Exception as NoodleException;
16
use Noodle\Statemachine\Exception\StateTransitionFailed;
17
use Noodle\Transition\Input\FlyweightInput;
18
use Noodle\Transition\Input\Input;
19
use Noodle\Transition\Table\TransitionTable;
20
21
final class Statemachine implements EventfulStatemachine
22
{
23
    /**
24
     * The emitter used to emit events
25
     *
26
     * @var Emitter
27
     */
28
    private $emitter;
29
30
    /**
31
     * Transition table used to determine which states support which actions
32
     *
33
     * @var TransitionTable
34
     */
35
    private $stateTransitionTable;
36
37
    /**
38
     * Constructor
39
     *
40
     * @param TransitionTable $stateTransitionTable
41
     * @param Listener $failureHandler (Optional) Listener that handles transition failures
42
     * @param Listener $stateChanger (Optional) Listener that updates the object's state
43
     */
44 8
    public function __construct(
45
        TransitionTable $stateTransitionTable,
46
        Listener $failureHandler = null,
47
        Listener $stateChanger = null
48
    ) {
49 8
        $this->stateTransitionTable = $stateTransitionTable;
50 8
        $this->emitter = new Emitter();
51
52 8
        if (!$stateChanger) {
53 7
            $stateChanger = new ChangesState();
54
        }
55
56 8
        $this->emitter->addListener(
57 8
            $this->getEventName('on', FlyweightInput::any(), FlyweightState::any()),
58
            $stateChanger
59
        );
60
61 8
        if (!$failureHandler) {
62 8
            $failureHandler = new ReportsTransitionFailures();
63
        }
64
65 8
        $this->emitter->addListener('failed', $failureHandler);
66 8
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 8
    public function trigger(Input $input, Stateful $object)
72
    {
73 8
        $context = new Context();
74 8
        $nextState = $this->stateTransitionTable->resolve($object->getCurrentState(), $input);
75
76 7
        $this->emitEvents($input, $object, $context, $nextState);
77 5
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 1
    public function after(Input $input, State $currentState, Listener $listener, int $priority = Emitter::P_NORMAL)
83
    {
84 1
        $this->emitter->addListener(
85 1
            $this->getEventName('after', $input, $currentState),
86
            $listener,
0 ignored issues
show
Documentation introduced by
$listener is of type object<League\Event\ListenerInterface>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
87
            $priority
88
        );
89 1
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 3
    public function before(Input $input, State $currentState, Listener $listener, int $priority = Emitter::P_NORMAL)
95
    {
96 3
        $this->emitter->addListener(
97 3
            $this->getEventName('before', $input, $currentState),
98
            $listener,
0 ignored issues
show
Documentation introduced by
$listener is of type object<League\Event\ListenerInterface>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
99
            $priority
100
        );
101 3
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 1
    public function on(Input $input, State $currentState, Listener $listener, int $priority = Emitter::P_NORMAL)
107
    {
108 1
        $this->emitter->addListener(
109 1
            $this->getEventName('on', $input, $currentState),
110
            $listener,
0 ignored issues
show
Documentation introduced by
$listener is of type object<League\Event\ListenerInterface>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
111
            $priority
112
        );
113 1
    }
114
115
    /**
116
     * Creates an event name based on an input, current state, and when that event should be emitted
117
     *
118
     * @param string $executedWhen
119
     * @param Input $input
120
     * @param State $currentState
121
     *
122
     * @return string
123
     */
124 8
    private function getEventName(string $executedWhen, Input $input, State $currentState) : string
125
    {
126 8
        return sprintf('%s %s %s', $executedWhen, $input->getName(), $currentState->getName());
127
    }
128
129
    /**
130
     * Emits the events corresponding to applying the provided input on the provided object.
131
     *
132
     * @param Input $input
133
     * @param Stateful $object
134
     * @param Context $context
135
     * @param State $nextState
136
     *
137
     * @return void
138
     *
139
     * @throws StateTransitionFailed
140
     */
141 7
    private function emitEvents(Input $input, Stateful $object, Context $context, State $nextState)
142
    {
143
        /** @var Event $event */
144 7
        foreach ($this->eventProvider($input, $object->getCurrentState()) as $event) {
145 7
            if (!$this->emitter->hasListeners($event->getName())) {
146 5
                continue;
147
            }
148
149 7
            if ($this->emitter->emit($event, $object, $context, $input, $nextState)->isPropagationStopped()) {
150 1
                $this->emitter->emit(Event::named('failed'), $object, $context, $input, $nextState);
151
152 5
                throw new StateTransitionFailed($input, $object, $context, $nextState);
153
            }
154
        }
155 5
    }
156
157
    /**
158
     * Returns events to be emitted whenever a state transition is attempted
159
     *
160
     * @param Input $input
161
     * @param State $currentState
162
     *
163
     * @return Generator
164
     */
165 7
    private function eventProvider(Input $input, State $currentState) : Generator
166
    {
167 7
        $anyInput = FlyweightInput::any();
168 7
        $anyState = FlyweightState::any();
169
170 7
        yield Event::named($this->getEventName('before', $input, $currentState));
171 5
        yield Event::named($this->getEventName('before', $anyInput, $currentState));
172 5
        yield Event::named($this->getEventName('before', $input, $anyState));
173 5
        yield Event::named($this->getEventName('before', $anyInput, $anyState));
174
175 5
        yield Event::named($this->getEventName('on', $anyInput, $anyState));
176
177 5
        yield Event::named($this->getEventName('after', $input, $currentState));
178 5
        yield Event::named($this->getEventName('after', $anyInput, $currentState));
179 5
        yield Event::named($this->getEventName('after', $input, $anyState));
180 5
        yield Event::named($this->getEventName('after', $anyInput, $anyState));
181 5
    }
182
}
183