Skip to content

Commit 8f81a22

Browse files
authored
Fix code style, improve coverage (#6)
* Fix code style, improve coverage * Tiny change
1 parent fe8dc38 commit 8f81a22

File tree

12 files changed

+66
-43
lines changed

12 files changed

+66
-43
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ This library provides some interfaces and a basic implementation of a State Engi
2525
The recommended and easiest way to install this library is through [Composer](https://getcomposer.org/):
2626

2727
```bash
28-
composer require uuf6429/state-engine-php "^2.0"
28+
composer require "uuf6429/state-engine-php" "^2.0"
2929
```
3030

3131
## 🧐 Why?
@@ -110,7 +110,8 @@ use uuf6429\StateEngine\Implementation\Entities\State;
110110
$door = Door::find(123);
111111

112112
$doorStateMutator = Builder::makeStateMutator(
113-
static function () use ($door): State { // getter
113+
// define how we get the state
114+
static function () use ($door): State {
114115
if ($door->is_locked) {
115116
return new State('locked');
116117
}
@@ -119,7 +120,8 @@ $doorStateMutator = Builder::makeStateMutator(
119120
? new State('open')
120121
: new State('closed');
121122
},
122-
function (State $newState) use ($door): void { // setter
123+
// define how we set the state
124+
static function (State $newState) use ($door): void {
123125
$door->update([
124126
'is_locked' => $newState->getName() === 'locked',
125127
'is_open' => $newState->getName() === 'open',

src/Implementation/AbstractEngine.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Psr\EventDispatcher\EventDispatcherInterface;
66
use uuf6429\StateEngine\Exceptions\TransitionNotDeclaredException;
77
use uuf6429\StateEngine\Interfaces\EngineInterface;
8-
use uuf6429\StateEngine\Interfaces\StateInterface;
98
use uuf6429\StateEngine\Interfaces\StateAwareInterface;
109
use uuf6429\StateEngine\Interfaces\TransitionInterface;
1110
use uuf6429\StateEngine\Interfaces\TransitionRepositoryInterface;

src/Implementation/Entities/TransitionWithData.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22

33
namespace uuf6429\StateEngine\Implementation\Entities;
44

5-
use uuf6429\StateEngine\Interfaces\DescribableInterface;
65
use uuf6429\StateEngine\Interfaces\StateInterface;
7-
use uuf6429\StateEngine\Interfaces\TransitionInterface;
86

97
class TransitionWithData extends Transition
108
{
119
private array $data;
1210

1311
public function __construct(StateInterface $oldState, array $data, StateInterface $newState, ?string $description = null)
1412
{
15-
parent::__construct($oldState,$newState,$description);
13+
parent::__construct($oldState, $newState, $description);
1614

1715
$this->data = $data;
1816
ksort($this->data);

src/Implementation/Events/StateChanged.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,29 @@
11
<?php
22

3-
43
namespace uuf6429\StateEngine\Implementation\Events;
54

6-
7-
use uuf6429\StateEngine\Interfaces\StateInterface;
85
use uuf6429\StateEngine\Interfaces\StateAwareInterface;
6+
use uuf6429\StateEngine\Interfaces\StateInterface;
97

8+
/**
9+
* Represents the event triggered after a state is changed.
10+
*/
1011
class StateChanged
1112
{
1213
private StateAwareInterface $item;
1314
private StateInterface $oldState;
1415

15-
/**
16-
* Event triggered after a state is changed.
17-
*
18-
* @param StateAwareInterface $item
19-
* @param StateInterface $oldState
20-
*/
2116
public function __construct(StateAwareInterface $item, StateInterface $oldState)
2217
{
2318
$this->item = $item;
2419
$this->oldState = $oldState;
2520
}
2621

27-
/**
28-
* @return StateAwareInterface
29-
*/
3022
public function getItem(): StateAwareInterface
3123
{
3224
return $this->item;
3325
}
3426

35-
/**
36-
* @return StateInterface
37-
*/
3827
public function getOldState(): StateInterface
3928
{
4029
return $this->oldState;

src/Implementation/Events/StateChanging.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,29 @@
11
<?php
22

3-
43
namespace uuf6429\StateEngine\Implementation\Events;
54

6-
7-
use uuf6429\StateEngine\Interfaces\StateInterface;
85
use uuf6429\StateEngine\Interfaces\StateAwareInterface;
6+
use uuf6429\StateEngine\Interfaces\StateInterface;
97

8+
/**
9+
* Represents the event triggered before a state is changed.
10+
*/
1011
class StateChanging
1112
{
1213
private StateAwareInterface $item;
1314
private StateInterface $newState;
1415

15-
/**
16-
* Event triggered before a state is changed.
17-
*
18-
* @param StateAwareInterface $item
19-
* @param StateInterface $newState
20-
*/
2116
public function __construct(StateAwareInterface $item, StateInterface $newState)
2217
{
2318
$this->item = $item;
2419
$this->newState = $newState;
2520
}
2621

27-
/**
28-
* @return StateAwareInterface
29-
*/
3022
public function getItem(): StateAwareInterface
3123
{
3224
return $this->item;
3325
}
3426

35-
/**
36-
* @return StateInterface
37-
*/
3827
public function getNewState(): StateInterface
3928
{
4029
return $this->newState;

src/Implementation/Repositories/AbstractTraversable.php

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

33
namespace uuf6429\StateEngine\Implementation\Repositories;
44

5+
use Exception;
56
use IteratorAggregate;
67
use Traversable;
78
use uuf6429\StateEngine\Implementation\Traits\FindsTransition;
@@ -12,6 +13,9 @@ abstract class AbstractTraversable implements TransitionRepositoryInterface, Ite
1213
{
1314
use FindsTransition, StateTraversion;
1415

16+
/**
17+
* @throws Exception
18+
*/
1519
public function all(): Traversable
1620
{
1721
return $this->getIterator();

src/Implementation/StateMachine.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ class StateMachine extends AbstractEngine
1111
* A shortcut to avoid getting a transition.
1212
*
1313
* @param StateAwareInterface $item
14-
* @param array $input
14+
* @param array $inputData
1515
*/
16-
public function processInput(StateAwareInterface $item, array $input): void
16+
public function processInput(StateAwareInterface $item, array $inputData): void
1717
{
18-
$transition = new Entities\TransitionWithData($item->getState(), $input, new State(''));
18+
$transition = new Entities\TransitionWithData($item->getState(), $inputData, new State(''));
1919
$this->execute($item, $transition);
2020
}
2121
}

src/Implementation/Traits/FindsTransition.php

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

33
namespace uuf6429\StateEngine\Implementation\Traits;
44

5+
use Exception;
56
use uuf6429\StateEngine\Interfaces\TransitionInterface;
67
use uuf6429\StateEngine\Interfaces\TransitionRepositoryInterface;
78

@@ -10,6 +11,9 @@
1011
*/
1112
trait FindsTransition
1213
{
14+
/**
15+
* @throws Exception
16+
*/
1317
public function find(TransitionInterface $search): ?TransitionInterface
1418
{
1519
foreach ($this->all() as $match) {

src/Implementation/Traits/StateTraversion.php

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

33
namespace uuf6429\StateEngine\Implementation\Traits;
44

5+
use Exception;
56
use uuf6429\StateEngine\Interfaces\StateInterface;
67
use uuf6429\StateEngine\Interfaces\TransitionInterface;
78
use uuf6429\StateEngine\Interfaces\TransitionRepositoryInterface;
@@ -12,6 +13,9 @@
1213
*/
1314
trait StateTraversion
1415
{
16+
/**
17+
* @throws Exception
18+
*/
1519
public function getForwardTransitions(StateInterface $state): array
1620
{
1721
/** @var $this TransitionRepositoryInterface */
@@ -23,6 +27,9 @@ static function (TransitionInterface $transition) use ($state): bool {
2327
));
2428
}
2529

30+
/**
31+
* @throws Exception
32+
*/
2633
public function getBackwardTransitions(StateInterface $state): array
2734
{
2835
/** @var $this TransitionRepositoryInterface */

src/Interfaces/EngineInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
interface EngineInterface
66
{
77
/**
8-
* Transition an item to a new state given a transition.
8+
* Transition an item to a new state given a {@see TransitionInterface} object.
99
*
1010
* @param StateAwareInterface $item
1111
* @param TransitionInterface $transition

0 commit comments

Comments
 (0)