8000 [Workflow] Better code · mikeSimonson/symfony@b7b2bc3 · GitHub
[go: up one dir, main page]

Skip to content

Commit b7b2bc3

Browse files
committed
[Workflow] Better code
1 parent f261af3 commit b7b2bc3

18 files changed

+181
-175
lines changed

src/Symfony/Component/Workflow/Definition.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ class Definition
3434
*/
3535
private $initialPlace;
3636

37-
3837
/**
3938
* Definition constructor.
40-
* @param Place[] $places
39+
*
40+
* @param Place[] $places
4141
* @param Transition[] $transitions
4242
*/
4343
public function __construct(array $places, array $transitions)
@@ -106,6 +106,7 @@ public function addPlaces(array $places)
106106

107107
/**
108108
* @param Transition[] $transitions
109+
*
109110
* @throws LogicException
110111
*/
111112
public function addTransitions(array $transitions)
@@ -117,6 +118,7 @@ public function addTransitions(array $transitions)
117118

118119
/**
119120
* @param Transition $transition
121+
*
120122
* @throws LogicException
121123
*/
122124
public function addTransition(Transition $transition)

src/Symfony/Component/Workflow/Dumper/GraphvizDumper.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ private function findPlaces(Definition $definition, Marking $marking = null)
7373
{
7474
$places = array();
7575

76-
/** @var Place $place */
7776
foreach ($definition->getPlaces() as $place) {
7877
$attributes = [];
7978
if ($place === $definition->getInitialPlace()) {
@@ -153,17 +152,14 @@ private function findEdges(Definition $definition)
153152
{
154153
$dotEdges = array();
155154

156-
/** @var Transition $transition */
157155
foreach ($definition->getTransitions() as $transition) {
158-
/** @var Place $from */
159156
foreach ($transition->getFroms() as $from) {
160157
$dotEdges[] = array(
161158
'from' => $from->getName(),
162159
'to' => $transition->getName(),
163160
'direction' => 'from',
164161
);
165162
}
166-
/** @var Place $to */
167163
foreach ($transition->getTos() as $to) {
168164
$dotEdges[] = array(
169165
'from' => $transition->getName(),

src/Symfony/Component/Workflow/Event/Event.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ class Event extends BaseEvent
3838

3939
/**
4040
* Event constructor.
41-
* @param mixed $subject
42-
* @param Marking $marking
41+
*
42+
* @param mixed $subject
43+
* @param Marking $marking
4344
* @param Transition $transition
4445
*/
4546
public function __construct($subject, Marking $marking, Transition $transition)

src/Symfony/Component/Workflow/Marking.php

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616
*
1717
* @author Grégoire Pineau <lyrixx@lyrixx.info>
1818
*/
19-
/**
20-
* Class Marking
21-
* @package Symfony\Component\Workflow
22-
*/
2319
class Marking
2420
{
2521
/**
@@ -28,26 +24,13 @@ class Marking
2824
private $places = array();
2925

3026
/**
31-
* @param string[] $places keys are the place name and values should be 1
32-
*/
33-
public function __construct(array $places = array())
34-
{
35-
foreach ($places as $place => $nbToken) {
36-
$this->markPlaceNamed($place);
37-
}
38-
}
39-
40-
/**
41-
* @param Place[] $places
42-
* @return Marking
27+
* @param string[] $representation keys are the place name and values should be 1
4328
*/
44-
public static function fromPlaces(array $places = array())
29+
public function __construct(array $representation = array())
4530
{
46-
$placeNames = [];
47-
foreach($places as $place) {
48-
$placeNames[$place->getName()] = 1;
31+
foreach ($representation as $placeName => $nbToken) {
32+
$this->markPlaceNamed($placeName);
4933
}
50-
return new self($placeNames);
5134
}
5235

5336
/**
@@ -67,7 +50,7 @@ public function unmark(Place $place)
6750
}
6851

6952
/**
70-
* @param string $placename place name
53+
* @param string $placeName place name
7154
*/
7255
public function markPlaceNamed($placeName)
7356
{
@@ -84,6 +67,7 @@ public function unmarkPlaceNamed($placeName)
8467

8568
/**
8669
* @param Place $place
70+
*
8771
* @return bool
8872
*/
8973
public function has(Place $place)
@@ -92,12 +76,13 @@ public function has(Place $place)
9276
}
9377

9478
/**
95-
* @param string $place place name
79+
* @param string $placeName place name
80+
*
9681
* @return bool
9782
*/
98-
public function hasPlaceNamed($name)
83+
public function hasPlaceNamed($placeName)
9984
{
100-
return isset($this->places[$name]);
85+
return isset($this->places[$placeName]);
10186
}
10287

10388
/**

src/Symfony/Component/Workflow/MarkingStore/PropertyAccessorMarkingStore.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ class PropertyAccessorMarkingStore implements MarkingStoreInterface
3232
*/
3333
private $propertyAccessor;
3434

35-
3635
/**
3736
* PropertyAccessorMarkingStore constructor.
38-
* @param string $property
37+
*
38+
* @param string $property
3939
* @param PropertyAccessorInterface|null $propertyAccessor
4040
*/
4141
public function __construct($property = 'marking', PropertyAccessorInterface $propertyAccessor = null)
@@ -46,6 +46,7 @@ public function __construct($property = 'marking', PropertyAccessorInterface $pr
4646

4747
/**
4848
* @param object $subject
49+
*
4950
* @return Marking
5051
*/
5152
public function getMarking($subject)
@@ -54,7 +55,7 @@ public function getMarking($subject)
5455
}
5556

5657
/**
57-
* @param object $subject
58+
* @param object $subject
5859
* @param Marking $marking
5960
*/
6061
public function setMarking($subject, Marking $marking)

src/Symfony/Component/Workflow/MarkingStore/ScalarMarkingStore.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ class ScalarMarkingStore implements MarkingStoreInterface, UniqueTransitionOutpu
3131
* @var PropertyAccessorInterface
3232
*/
3333
private $propertyAccessor;
34-
34+
3535
/**
3636
* ScalarMarkingStore constructor.
37-
* @param string $property
37+
*
38+
* @param string $property
3839
* @param PropertyAccessorInterface|null $propertyAccessor
3940
*/
4041
public function __construct($property = 'marking', PropertyAccessorInterface $propertyAccessor = null)
@@ -45,21 +46,22 @@ public function __construct($property = 'marking', PropertyAccessorInterface $pr
4546

4647
/**
4748
* @param object $subject
49+
*
4850
* @return Marking
4951
*/
5052
public function getMarking($subject)
5153
{
52-
$place = $this->propertyAccessor->getValue($subject, $this->property);
54+
$placeName = $this->propertyAccessor->getValue($subject, $this->property);
5355

54-
if (!$place) {
56+
if (!$placeName) {
5557
return new Marking();
5658
}
5759

58-
return new Marking(array($place => true));
60+
return new Marking(array($placeName => 1));
5961
}
6062

6163
/**
62-
* @param object $subject
64+
* @param object $subject
6365
* @param Marking $marking
6466
*/
6567
public function setMarking($subject, Marking $marking)

src/Symfony/Component/Workflow/Place.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,28 @@
1212

1313
use Symfony\Component\Workflow\Exception\InvalidArgumentException;
1414

15+
/**
16+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
17+
*/
1518
class Place
1619
{
17-
1820
/**
1921
* @var string
2022
*/
2123
private $name;
2224

25+
/**
26+
* Place constructor.
27+
*
28+
* @param string $name
29+
*/
2330
public function __construct($name)
2431
{
2532
if (!preg_match('{^[\w\d_-]+$}', $name)) {
2633
throw new InvalidArgumentException(sprintf('The place "%s" contains invalid characters.', $name));
2734
}
28-
$this->name = $name;
29-
}
30-
31-
public static function fromNames(array $names)
32-
{
33-
$places = [];
34-
foreach ($names as $name) {
35-
$places[] = new self($name);
36-
}
3735

38-
return $places;
36+
$this->name = $name;
3937
}
4038

4139
/**

src/Symfony/Component/Workflow/Registry.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,17 @@ class Registry
2626

2727
/**
2828
* @param Workflow $workflow
29-
* @param string $classname
29+
* @param string $classname
3030
*/
3131
public function add(Workflow $workflow, $classname)
3232
{
3333
$this->workflows[] = array($workflow, $classname);
3434
}
3535

3636
/**
37-
* @param string $subject
37+
* @param string $subject
3838
* @param string|null $name
39+
*
3940
* @return Workflow
4041
*/
4142
public function get($subject, $name = null)
@@ -59,9 +60,10 @@ public function get($subject, $name = null)
5960

6061
/**
6162
* @param Workflow $workflow
62-
* @param string $classname
63-
* @param string $subject
64-
* @param string $name
63+
* @param string $classname
64+
* @param string $subject
65+
* @param string $name
66+
*
6567
* @return bool
6668
*/
6769
private function supports(Workflow $workflow, $classname, $subject, $name)

src/Symfony/Component/Workflow/Tests/DefinitionTest.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88

99
class DefinitionTest extends \PHPUnit_Framework_TestCase
1010
{
11-
1211
public function testAddPlaces()
1312
{
14-
$places = Place::fromNames(range('a', 'e'));
13+
$places = $this->createPlaces(range('a', 'e'));
1514
$definition = new Definition($places, []);
1615

1716
$this->assertCount(5, $definition->getPlaces());
@@ -21,7 +20,7 @@ public function testAddPlaces()
2120

2221
public function testSetInitialPlace()
2322
{
24-
$places = Place::fromNames(range('a', 'e'));
23+
$places = $this->createPlaces(range('a', 'e'));
2524
$definition = new Definition($places, []);
2625

2726
$definition->setInitialPlace($places[3]);
@@ -42,7 +41,7 @@ public function testSetInitialPlaceAndPlaceIsNotDefined()
4241

4342
public function testAddTransition()
4443
{
45-
$places = Place::fromNames(['a', 'b']);
44+
$places = $this->createPlaces(['a', 'b']);
4645

4746
$transition = new Transition('name', $places[0], $places[1]);
4847
$definition = new Definition($places, [$transition]);
@@ -57,7 +56,7 @@ public function testAddTransition()
5756
*/
5857
public function testAddTransitionAndFromPlaceIsNotDefined()
5958
{
60-
$places = Place::fromNames(['a', 'b']);
59+
$places = $this->createPlaces(['a', 'b']);
6160

6261
new Definition($places, [new Transition('name', new Place('c'), $places[1])]);
6362
}
@@ -68,8 +67,19 @@ public function testAddTransitionAndFromPlaceIsNotDefined()
6867
*/
6968
public function testAddTransitionAndToPlaceIsNotDefined()
7069
{
71-
$places = Place::fromNames(['a', 'b']);
70+
$places = $this->createPlaces(['a', 'b']);
7271

7372
new Definition($places, [new Transition('name', $places[0], new Place('c'))]);
7473
}
74+
75+
private function createPlaces($placesName)
76+
{
77+
$places = [];
78+
79+
foreach ($placesName as $name) {
80+
$places[] = new Place($name);
81+
}
82+
83+
return $places;
84+
}
7585
}

0 commit comments

Comments
 (0)
0