StateMachine::acceptStateVisitor()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
0 ignored issues
show
Coding Style introduced by
This file is missing a doc comment.
Loading history...
Coding Style introduced by
The PHP open tag does not have a corresponding PHP close tag
Loading history...
Coding Style introduced by
Filename "StateMachine.php" doesn't match the expected filename "statemachine.php"
Loading history...
2
/**
0 ignored issues
show
Coding Style introduced by
Inline doc block comments are not allowed; use "/* Comment */" or "// Comment" instead
Loading history...
Coding Style introduced by
Block comments must be started with /*
Loading history...
3
 * This file is part of the php-state project.
4
 *
5
 * (c) Yannick Voyer <[email protected]> (http://github.com/yvoyer)
6
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @link tag in file comment
Loading history...
7
8
namespace Star\Component\State;
9
10
use Star\Component\State\Callbacks\AlwaysThrowExceptionOnFailure;
11
use Star\Component\State\Callbacks\TransitionCallback;
12
use Star\Component\State\Event\StateEventStore;
13
use Star\Component\State\Event\TransitionWasFailed;
14
use Star\Component\State\Event\TransitionWasSuccessful;
15
use Star\Component\State\Event\TransitionWasRequested;
16
17
final class StateMachine
0 ignored issues
show
Coding Style Documentation introduced by
Missing class doc comment
Loading history...
18
{
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration for class StateMachine
Loading history...
19
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
20
     * @var EventRegistry
21
     */
22
    private $listeners;
0 ignored issues
show
Coding Style introduced by
Private member variable "listeners" must contain a leading underscore
Loading history...
Coding Style introduced by
Expected 1 blank line before member var; 0 found
Loading history...
Coding Style introduced by
Private member variable "listeners" must be prefixed with an underscore
Loading history...
23
24
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
25
     * @var StateRegistry
26
     */
27
    private $states;
0 ignored issues
show
Coding Style introduced by
Private member variable "states" must contain a leading underscore
Loading history...
Coding Style introduced by
Private member variable "states" must be prefixed with an underscore
Loading history...
28
29
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
30
     * @var string
31
     */
32
    private $currentState;
0 ignored issues
show
Coding Style introduced by
Private member variable "currentState" must contain a leading underscore
Loading history...
Coding Style introduced by
Private member variable "currentState" must be prefixed with an underscore
Loading history...
33
34 46
    public function __construct(
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
Coding Style introduced by
Missing function doc comment
Loading history...
35
        string $currentState,
36
        StateRegistry $states,
37
        EventRegistry $listeners
38
    ) {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on a new line
Loading history...
39 46
        $this->listeners = $listeners;
40 46
        $this->states = $states;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
41 46
        $this->setCurrentState($currentState);
42 46
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end __construct()
Loading history...
43
44
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
45
     * @param string $transitionName The transition name
0 ignored issues
show
Coding Style introduced by
Expected 18 spaces after parameter type; 1 found
Loading history...
introduced by
Parameter comment must end with a full stop
Loading history...
46
     * @param mixed $context
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 19 spaces after parameter type; 1 found
Loading history...
47
     * @param TransitionCallback|null $callback
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
48
     *
49
     * @return string The next state to store on your context
50
     * @throws InvalidStateTransitionException
0 ignored issues
show
introduced by
Comment missing for @throws tag in function comment
Loading history...
51
     * @throws NotFoundException
0 ignored issues
show
introduced by
Comment missing for @throws tag in function comment
Loading history...
52
     */
53 30
    public function transit(string $transitionName, $context, TransitionCallback $callback = null): string
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL should be uppercase as per the configured coding-style; instead of null please use NULL.
Loading history...
54
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
55 30
        if (! $callback) {
56 29
            $callback = new AlwaysThrowExceptionOnFailure();
57
        }
58
59 30
        $this->listeners->dispatch(
60 30
            StateEventStore::BEFORE_TRANSITION,
61 30
            new TransitionWasRequested($transitionName)
62
        );
63
64 30
        $transition = $this->states->getTransition($transitionName);
65 29
        $callback->beforeStateChange($context, $this);
66
67 29
        $newState = $transition->getDestinationState();
68 29
        $allowed = $this->states->transitionStartsFrom($transitionName, $this->currentState);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
69 29
        if (! $allowed) {
70 11
            $exception = InvalidStateTransitionException::notAllowedTransition(
71 11
                $transitionName,
72
                $context,
73 11
                $this->currentState
74
            );
75
76 11
            $this->listeners->dispatch(
77 11
                StateEventStore::FAILURE_TRANSITION,
78 11
                new TransitionWasFailed($transitionName, $exception)
79
            );
80
81 11
            $newState = $callback->onFailure($exception, $context, $this);
82
        }
83
84 19
        $this->setCurrentState($newState);
85
86 19
        $callback->afterStateChange($context, $this);
87
88 19
        $this->listeners->dispatch(
89 19
            StateEventStore::AFTER_TRANSITION,
90 19
            new TransitionWasSuccessful($transitionName)
91
        );
92
93 19
        return $this->currentState;
94
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end transit()
Loading history...
95
96
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
97
     * @param string $stateName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
98
     * @return bool
0 ignored issues
show
Coding Style introduced by
Expected "boolean" but found "bool" for function return type
Loading history...
99
     * @throws NotFoundException
0 ignored issues
show
introduced by
Comment missing for @throws tag in function comment
Loading history...
100
     */
101 34
    public function isInState(string $stateName): bool
102
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
103 34
        if (! $this->states->hasState($stateName)) {
104 1
            throw NotFoundException::stateNotFound($stateName);
105
        }
106
107 33
        return $this->currentState === $stateName;
108
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end isInState()
Loading history...
109
110 9
    public function hasAttribute(string $attribute): bool
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
111
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
112 9
        return $this->states->hasAttribute($this->currentState, $attribute);
113
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end hasAttribute()
Loading history...
114
115
    public function addListener(string $event, \Closure $listener): void
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
116
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
117
        $this->listeners->addListener($event, $listener);
118
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end addListener()
Loading history...
119
120 3
    public function acceptTransitionVisitor(TransitionVisitor $visitor): void
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
121
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
122 3
        $this->states->acceptTransitionVisitor($visitor);
123 3
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end acceptTransitionVisitor()
Loading history...
124
125 1
    public function acceptStateVisitor(StateVisitor $visitor): void
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
126
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
127 1
        $this->states->acceptStateVisitor($visitor);
128 1
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end acceptStateVisitor()
Loading history...
129
130 46
    private function setCurrentState(string $state): void
0 ignored issues
show
Coding Style introduced by
Private method name "StateMachine::setCurrentState" must be prefixed with an underscore
Loading history...
Coding Style introduced by
Missing function doc comment
Loading history...
131
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
132 46
        $this->currentState = $state;
133 46
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end setCurrentState()
Loading history...
134
}
0 ignored issues
show
Coding Style introduced by
Expected //end class
Loading history...
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
135