Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
35 | class SyncBindingUuid implements AtomicOperation |
||
36 | { |
||
37 | /** |
||
38 | * @var Uuid |
||
39 | */ |
||
40 | private $uuid; |
||
41 | |||
42 | /** |
||
43 | * @var EditableDiscovery |
||
44 | */ |
||
45 | private $discovery; |
||
46 | |||
47 | /** |
||
48 | * @var BindingDescriptor |
||
49 | */ |
||
50 | private $enabledBindingBefore; |
||
51 | |||
52 | /** |
||
53 | * @var BindingDescriptor |
||
54 | */ |
||
55 | private $enabledBindingAfter; |
||
56 | |||
57 | /** |
||
58 | * @var BindingDescriptorCollection |
||
59 | */ |
||
60 | private $bindingDescriptors; |
||
61 | |||
62 | /** |
||
63 | * @var bool |
||
64 | */ |
||
65 | private $snapshotTaken = false; |
||
66 | |||
67 | 24 | public function __construct(Uuid $uuid, EditableDiscovery $discovery, BindingDescriptorCollection $bindingDescriptors) |
|
68 | { |
||
69 | 24 | $this->uuid = $uuid; |
|
70 | 24 | $this->discovery = $discovery; |
|
71 | 24 | $this->bindingDescriptors = $bindingDescriptors; |
|
72 | 24 | } |
|
73 | |||
74 | /** |
||
75 | * Records whether the UUID is currently enabled. |
||
76 | */ |
||
77 | 24 | public function takeSnapshot() |
|
78 | { |
||
79 | 24 | $this->enabledBindingBefore = null; |
|
80 | |||
81 | 24 | View Code Duplication | if ($this->bindingDescriptors->contains($this->uuid)) { |
|
|||
82 | 19 | $bindingDescriptor = $this->bindingDescriptors->get($this->uuid); |
|
83 | |||
84 | 19 | if ($bindingDescriptor->isEnabled()) { |
|
85 | // Clone so that rollback() works if the binding is unloaded |
||
86 | 11 | $this->enabledBindingBefore = clone $bindingDescriptor; |
|
87 | } |
||
88 | } |
||
89 | |||
90 | 24 | $this->snapshotTaken = true; |
|
91 | 24 | } |
|
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | */ |
||
96 | 23 | public function execute() |
|
97 | { |
||
98 | 23 | if (!$this->snapshotTaken) { |
|
99 | throw new LogicException('takeSnapshot() was not called'); |
||
100 | } |
||
101 | |||
102 | // Remember for rollback() |
||
103 | 23 | $this->enabledBindingAfter = null; |
|
104 | |||
105 | 23 | View Code Duplication | if ($this->bindingDescriptors->contains($this->uuid)) { |
106 | 16 | $bindingDescriptor = $this->bindingDescriptors->get($this->uuid); |
|
107 | |||
108 | 16 | if ($bindingDescriptor->isEnabled()) { |
|
109 | // Clone so that rollback() works if the binding is unloaded |
||
110 | 8 | $this->enabledBindingAfter = clone $bindingDescriptor; |
|
111 | } |
||
112 | } |
||
113 | |||
114 | 23 | $this->syncBindingUuid($this->enabledBindingBefore, $this->enabledBindingAfter); |
|
115 | 23 | } |
|
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | 7 | public function rollback() |
|
121 | { |
||
122 | 7 | $this->syncBindingUuid($this->enabledBindingAfter, $this->enabledBindingBefore); |
|
123 | 7 | } |
|
124 | |||
125 | 23 | private function syncBindingUuid(BindingDescriptor $enabledBefore = null, BindingDescriptor $enabledAfter = null) |
|
136 | } |
||
137 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.