TransitionRegistry   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Test Coverage

Coverage 98.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 44
dl 0
loc 143
ccs 59
cts 60
cp 0.9833
rs 10
c 1
b 0
f 0
wmc 22

12 Methods

Rating   Name   Duplication   Size   Complexity  
A acceptStateVisitor() 0 4 2
A addAttribute() 0 8 2
A hasState() 0 3 1
A initState() 0 4 2
A acceptTransitionVisitor() 0 9 3
A registerStartingState() 0 5 1
A transitionStartsFrom() 0 8 2
A addAttributes() 0 7 1
A addTransition() 0 8 2
A hasAttribute() 0 7 2
A getTransition() 0 12 3
A registerDestinationState() 0 5 1
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 "TransitionRegistry.php" doesn't match the expected filename "transitionregistry.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\Transitions\ReadOnlyTransition;
11
12
final class TransitionRegistry implements StateRegistry
0 ignored issues
show
Coding Style Documentation introduced by
Missing class doc comment
Loading history...
13
{
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration for class TransitionRegistry
Loading history...
14
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
15
     * @var array[] Collection of states indexed by transition name
16
     */
17
    private $transitions = [];
0 ignored issues
show
Coding Style introduced by
Private member variable "transitions" 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 "transitions" must be prefixed with an underscore
Loading history...
Coding Style introduced by
Short array syntax is not allowed
Loading history...
18
19
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
20
     * @var array[] Collection of attributes indexed by state name
21
     */
22
    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...
Coding Style introduced by
Short array syntax is not allowed
Loading history...
23
24
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
25
     * @param StateTransition $transition
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
26
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
27 47
    public function addTransition(StateTransition $transition): void
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
28
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
29 47
        $name = $transition->getName();
30 47
        if (isset($this->transitions[$name])) {
31 1
            throw DuplicateEntryException::duplicateTransition($name);
32
        }
33
34 47
        $transition->onRegister($this);
35 47
    }
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 addTransition()
Loading history...
36
37
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
38
     * @param string $name The transition name
0 ignored issues
show
introduced by
Parameter comment must end with a full stop
Loading history...
39
     *
40
     * @return StateTransition
41
     * @throws NotFoundException
0 ignored issues
show
introduced by
Comment missing for @throws tag in function comment
Loading history...
42
     */
43 32
    public function getTransition(string $name): StateTransition
44
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
45 32
        $transition = null;
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...
46 32
        if (isset($this->transitions[$name]['to'])) {
47 30
            $transition = new ReadOnlyTransition($this->transitions[$name]['to']);
48
        }
49
50 32
        if (! $transition) {
51 2
            throw NotFoundException::transitionNotFound($name);
52
        }
53
54 30
        return $transition;
55
    }
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 getTransition()
Loading history...
56
57 35
    public function addAttribute(string $state, string $attribute): void
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
58
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
59 35
        $attributes = [$attribute];
0 ignored issues
show
Coding Style introduced by
Short array syntax is not allowed
Loading history...
60 35
        if ($this->hasState($state)) {
61 35
            $attributes = \array_merge($this->states[$state], $attributes);
62
        }
63
64 35
        $this->states[$state] = \array_unique($attributes);
65 35
    }
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 addAttribute()
Loading history...
66
67
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
68
     * @param string $state
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
69
     * @param string[] $attributes
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
70
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
71 52
    private function addAttributes(string $state, array $attributes): void
0 ignored issues
show
Coding Style introduced by
Private method name "TransitionRegistry::addAttributes" must be prefixed with an underscore
Loading history...
72
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
73 52
        \array_map(
74
            function ($attribute) use ($state) {
75 3
                $this->addAttribute($state, $attribute);
76 52
            },
77 52
            $attributes
78
        );
79 52
    }
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 addAttributes()
Loading history...
80
81 30
    public function transitionStartsFrom(string $transition, string $state): bool
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
82
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
83 30
        $from = [];
0 ignored issues
show
Coding Style introduced by
Short array syntax is not allowed
Loading history...
84 30
        if (isset($this->transitions[$transition]['from'])) {
85 30
            $from = $this->transitions[$transition]['from'];
86
        }
87
88 30
        return \in_array($state, $from, true);
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL should be uppercase as per the configured coding-style; instead of true please use TRUE.
Loading history...
89
    }
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 transitionStartsFrom()
Loading history...
90
91 10
    public function hasAttribute(string $state, string $attribute): bool
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
92
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
93 10
        if (! $this->hasState($state)) {
94
            return false;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL should be uppercase as per the configured coding-style; instead of false please use FALSE.
Loading history...
95
        }
96
97 10
        return \in_array($attribute, $this->states[$state]);
98
    }
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...
99
100 54
    public function hasState(string $name): bool
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
101
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
102 54
        return \array_key_exists($name, $this->states);
103
    }
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 hasState()
Loading history...
104
105 3
    public function acceptTransitionVisitor(TransitionVisitor $visitor): void
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
106
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
107 3
        foreach ($this->transitions as $transition => $states) {
108 3
            $visitor->visitTransition($transition);
109
110 3
            foreach ($states['from'] as $from) {
111 3
                $visitor->visitFromState($from, $this->states[$from]);
112
            }
113 3
            $visitor->visitToState($states['to'], $this->states[$states['to']]);
114
        }
115 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...
116
117 2
    public function acceptStateVisitor(StateVisitor $visitor): void
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
118
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
119 2
        foreach ($this->states as $state => $attributes) {
120 2
            $visitor->visitState($state, $attributes);
121
        }
122 2
    }
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...
123
124
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
125
     * @param string $transition
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
126
     * @param string $stateName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
127
     * @param string[] $attributes
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
128
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
129 52
    public function registerStartingState(string $transition, string $stateName, array $attributes = []): void
0 ignored issues
show
Coding Style introduced by
Short array syntax is not allowed
Loading history...
130
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
131 52
        $this->initState($stateName);
132 52
        $this->addAttributes($stateName, $attributes);
133 52
        $this->transitions[$transition]['from'][] = $stateName;
134 52
    }
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 registerStartingState()
Loading history...
135
136
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
137
     * @param string $transition
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
138
     * @param string $stateName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
139
     * @param string[] $attributes
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
140
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
141 48
    public function registerDestinationState(string $transition, string $stateName, array $attributes = []): void
0 ignored issues
show
Coding Style introduced by
Short array syntax is not allowed
Loading history...
142
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
143 48
        $this->initState($stateName);
144 48
        $this->addAttributes($stateName, $attributes);
145 48
        $this->transitions[$transition]['to'] = $stateName;
146 48
    }
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 registerDestinationState()
Loading history...
147
148
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
149
     * @param string $state
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
150
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
151 52
    private function initState(string $state): void
0 ignored issues
show
Coding Style introduced by
Private method name "TransitionRegistry::initState" must be prefixed with an underscore
Loading history...
152
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
153 52
        if (!$this->hasState($state)) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
154 52
            $this->states[$state] = [];
0 ignored issues
show
Coding Style introduced by
Short array syntax is not allowed
Loading history...
155
        }
156 52
    }
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 initState()
Loading history...
157
}
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...
158