Skip to content

Commit 0cb1cff

Browse files
committed
phpDoc + code cleanup
1 parent f5659e2 commit 0cb1cff

9 files changed

Lines changed: 148 additions & 120 deletions

src/Assertion/AlwaysTrueAssertion.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class AlwaysTrueAssertion extends Assertion
88
{
99

10-
public function make() : bool
10+
public function make(): bool
1111
{
1212
return true;
1313
}

src/Assertion/AssertionInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66

77
interface AssertionInterface
88
{
9-
public function make() : bool;
9+
public function make(): bool;
1010
}

src/Assertion/CallbackAssertion.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ public function __construct(callable $callback = [], array $params = [])
2121
$this->params = $params;
2222
}
2323

24-
public function setCallback(callable $callback) {
24+
public function setCallback(callable $callback)
25+
{
2526
$this->callback = $callback;
2627

2728
return $this;
2829
}
2930

30-
public function setParams($params = []) {
31+
public function setParams($params = [])
32+
{
3133
$this->params = $params;
3234

3335
return $this;
@@ -41,8 +43,7 @@ public function make(): bool
4143
{
4244
if ($this->callback) {
4345
return call_user_func_array($this->callback, $this->params);
44-
}
45-
else {
46+
} else {
4647
throw new AssertionException('Callback not configured!');
4748
}
4849
}

src/Assertion/CommonCallbackAssertion.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,24 @@ class CommonCallbackAssertion extends CallbackAssertion
1515

1616
/** @var TransitionInterface $transition */
1717
protected $transition;
18-
18+
1919
public function __construct()
2020
{
2121
parent::__construct();
2222
}
23-
24-
25-
public function setObject(MachineInterface $object)
23+
24+
25+
public function setObject(MachineInterface $object)
2626
{
2727
$this->object = $object;
28-
29-
return $this;
28+
29+
return $this;
3030
}
31-
32-
public function setTransition(TransitionInterface $transition)
31+
32+
public function setTransition(TransitionInterface $transition)
3333
{
34-
$this->transition = $transition;
35-
34+
$this->transition = $transition;
35+
3636
return $this;
3737
}
3838

@@ -42,12 +42,9 @@ public function setTransition(TransitionInterface $transition)
4242
*/
4343
public function make(): bool
4444
{
45-
if ($this->object instanceof MachineInterface && $this->transition instanceof TransitionInterface)
46-
{
47-
return call_user_func_array([$this->object,'assertTransition'], [$this->transition]);
48-
}
49-
else
50-
{
45+
if ($this->object instanceof MachineInterface && $this->transition instanceof TransitionInterface) {
46+
return call_user_func_array([$this->object, 'assertTransition'], [$this->transition]);
47+
} else {
5148
throw new AssertionException('Callback not configured!');
5249
}
5350
}

src/Assertion/DefaultCallbackAssertion.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ public function __construct()
2121
{
2222
parent::__construct();
2323
}
24-
25-
public function setObject(MachineInterface $object)
24+
25+
public function setObject(MachineInterface $object)
2626
{
2727
$this->object = $object;
28-
29-
return $this;
28+
29+
return $this;
3030
}
31-
32-
public function setTransition(TransitionInterface $transition)
31+
32+
public function setTransition(TransitionInterface $transition)
3333
{
34-
$this->transition = $transition;
35-
34+
$this->transition = $transition;
35+
3636
return $this;
3737
}
3838

@@ -42,12 +42,9 @@ public function setTransition(TransitionInterface $transition)
4242
*/
4343
public function make(): bool
4444
{
45-
if ($this->object instanceof MachineInterface && $this->transition instanceof TransitionInterface)
46-
{
47-
return call_user_func_array([$this->object,'assert'], [$this->transition]);
48-
}
49-
else
50-
{
45+
if ($this->object instanceof MachineInterface && $this->transition instanceof TransitionInterface) {
46+
return call_user_func_array([$this->object, 'assert'], [$this->transition]);
47+
} else {
5148
throw new AssertionException('Callback not configured!');
5249
}
5350
}

src/Machine.php

Lines changed: 92 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Coff\SMF;
44

5-
65
use Coff\SMF\Assertion\Assertion;
76
use Coff\SMF\Assertion\CommonCallbackAssertion;
87
use Coff\SMF\Assertion\DefaultCallbackAssertion;
@@ -34,20 +33,14 @@ public function allowTransition(StateEnum $stateFrom, StateEnum $stateTo, Assert
3433
{
3534
$transition = new Transition($stateFrom, $stateTo);
3635

37-
/*
38-
* Default assertion when no assertions
39-
*/
40-
if (null == $assertion)
41-
{
36+
// Default assertion when no assertions
37+
if (null == $assertion) {
4238
$assertion = new DefaultCallbackAssertion();
4339
}
4440

4541
$transition->addAssertion($assertion);
4642

47-
48-
/*
49-
* Wire it up
50-
*/
43+
// Wire it up
5144
switch (true) {
5245
case $assertion instanceof CommonCallbackAssertion:
5346
// no break
@@ -73,7 +66,8 @@ public function allowTransition(StateEnum $stateFrom, StateEnum $stateTo, Assert
7366
* @return $this
7467
* @throws MachineException
7568
*/
76-
public function addTransition(TransitionInterface $transition) {
69+
public function addTransition(TransitionInterface $transition)
70+
{
7771

7872
if (!$transition->getFromState() instanceof StateEnum) {
7973
throw new MachineException('Transition is not ready to be set as allowed!');
@@ -89,63 +83,120 @@ public function addTransition(TransitionInterface $transition) {
8983
}
9084

9185
/**
92-
* @param StateEnum $stateFrom
93-
* @param StateEnum $stateTo
94-
* @return Transition
95-
* @throws MachineException
86+
* Verifies if machine's current state is equal to state given in parameter
87+
*
88+
* @param StateEnum $state
89+
* @return bool
9690
*/
97-
public function getTransition(StateEnum $stateFrom, StateEnum $stateTo) {
98-
if (isset($this->allowedTransitions[(string)$stateFrom][(string)$stateTo])) {
99-
return $this->allowedTransitions[(string)$stateFrom][(string)$stateTo];
100-
} else {
101-
throw new MachineException('No transition object for ' . $stateFrom . ' to ' . $stateTo);
102-
}
91+
public function isMachineState(StateEnum $state): bool
92+
{
93+
return (string)$this->getMachineState() == (string)$state ? true : false;
10394
}
10495

96+
/**
97+
* Returns machine's current state
98+
* @return StateEnum
99+
*/
100+
public function getMachineState(): StateEnum
101+
{
102+
return $this->machineState;
103+
}
105104

106-
public function getAllowedTransitions(StateEnum $state = null) : array {
105+
/**
106+
* Machine-internal method for setting new state. Normally this should only be allowed from within the machine
107+
* assertState() method. To set machine's state externally create dedicated methods like:
108+
*
109+
* public function on() {
110+
* $this->setMachineState(...);
111+
* }
112+
*
113+
* @param $newState
114+
* @return $this
115+
* @throws TransitionException
116+
* @throws MachineException
117+
*/
118+
protected function setMachineState(StateEnum $newState)
119+
{
107120

108-
if (null === $state) {
109-
$state = $this->getMachineState();
121+
if (false === $this->isTransitionAllowed($newState)) {
122+
throw new TransitionException('State transition from ' . (string)$this->getMachineState() . ' to ' . (string)$newState . 'is not allowed.');
110123
}
111124

112-
return $this->allowedTransitions[(string)$state];
125+
$oldState = $this->machineState;
126+
127+
$this->machineState = $newState;
128+
129+
$this->onTransition($this->getTransition($oldState, $newState));
130+
131+
return $this;
113132
}
114133

115-
public function isTransitionAllowed(StateEnum $state)
134+
/**
135+
* Verifies if transition is allowed from current state to the state given in parameter
136+
* @param StateEnum $state
137+
* @return bool
138+
*/
139+
public function isTransitionAllowed(StateEnum $state): bool
116140
{
117141
return isset($this->allowedTransitions[(string)$this->getMachineState()][$state]) ? true : false;
118142
}
119143

120-
public function getMachineState() : StateEnum
144+
/**
145+
* Method called on any state transition occurence
146+
* @param Transition $transition
147+
* @return MachineInterface|void
148+
*/
149+
public function onTransition(Transition $transition)
121150
{
122-
return $this->machineState;
151+
// Default implementation does nothing. This can be used to dispatch events in kind-of EventAwareMachine you can
152+
// implement yourself.
123153
}
124154

125-
public function isMachineState(StateEnum $state) : bool
155+
/**
156+
* @param StateEnum $stateFrom
157+
* @param StateEnum $stateTo
158+
* @return Transition
159+
* @throws MachineException
160+
*/
161+
public function getTransition(StateEnum $stateFrom, StateEnum $stateTo)
126162
{
127-
return (string) $this->getMachineState() == (string) $state ? true : false;
163+
if (isset($this->allowedTransitions[(string)$stateFrom][(string)$stateTo])) {
164+
return $this->allowedTransitions[(string)$stateFrom][(string)$stateTo];
165+
} else {
166+
throw new MachineException('No transition object for ' . $stateFrom . ' to ' . $stateTo);
167+
}
128168
}
129169

170+
/**
171+
* Sets machine's initial state
172+
* @param StateEnum $state
173+
* @return $this|MachineInterface
174+
*/
130175
public function setInitState(StateEnum $state)
131176
{
132177
$this->initState = $state;
133178

134179
return $this;
135180
}
136181

137-
public function assertTransition(Transition $transition) : bool
182+
/**
183+
* Default handler method for CommonCallbackAssertion
184+
* @param Transition $transition
185+
* @return bool
186+
*/
187+
public function assertTransition(Transition $transition): bool
138188
{
139-
140189
// default method just returns true
141190
return true;
142191
}
143192

144193
/**
194+
* Runs the machine as far as following transitions return true
145195
* @return StateEnum
146196
* @throws TransitionException
147197
*/
148-
public function run() {
198+
public function run()
199+
{
149200

150201
do {
151202
$result = false;
@@ -172,32 +223,18 @@ public function run() {
172223
return $this->machineState;
173224
}
174225

175-
176226
/**
177-
* Machine-internal method for setting new state. Normally this should only be allowed from within the machine
178-
* assertState() method. To set machine's state externally create dedicated methods like:
179-
*
180-
* public function on() {
181-
* $this->setMachineState(...);
182-
* }
183-
*
184-
* @param $newState
185-
* @return $this
186-
* @throws TransitionException
187-
* @throws MachineException
227+
* Returns Transitions allowed for current state or state specified in parameter
228+
* @param StateEnum|null $state
229+
* @return array
188230
*/
189-
protected function setMachineState(StateEnum $newState) {
231+
public function getAllowedTransitions(StateEnum $state = null): array
232+
{
190233

191-
if (false === $this->isTransitionAllowed($newState)){
192-
throw new TransitionException('State transition from ' . (string) $this->getMachineState() . ' to ' . (string) $newState . 'is not allowed.');
234+
if (null === $state) {
235+
$state = $this->getMachineState();
193236
}
194237

195-
$oldState = $this->machineState;
196-
197-
$this->machineState = $newState;
198-
199-
$this->onTransition($this->getTransition($oldState, $newState));
200-
201-
return $this;
238+
return $this->allowedTransitions[(string)$state];
202239
}
203240
}

src/MachineInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,21 @@ public function getInitState();
2424
* Returns
2525
* @return StateEnum
2626
*/
27-
public function getMachineState() : StateEnum;
27+
public function getMachineState(): StateEnum;
2828

2929
/**
3030
* Checks whether machine is in certain state
3131
* @param StateEnum $state
3232
* @return bool
3333
*/
34-
public function isMachineState(StateEnum $state) : bool;
34+
public function isMachineState(StateEnum $state): bool;
3535

3636
/**
3737
* Default transition assertion method for CommonCallbackAssertion
3838
* @param Transition $transition
3939
* @return bool
4040
*/
41-
public function assertTransition(Transition $transition) : bool;
41+
public function assertTransition(Transition $transition): bool;
4242

4343
/**
4444
* This method is called on any state transition

0 commit comments

Comments
 (0)