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 |
||
8 | class ManipulationSequence implements IteratorAggregate |
||
9 | { |
||
10 | /** @var array */ |
||
11 | protected $groups = []; |
||
12 | |||
13 | public function __construct(array $sequenceArray = []) |
||
14 | { |
||
15 | $this->startNewGroup(); |
||
16 | $this->mergeArray($sequenceArray); |
||
17 | } |
||
18 | |||
19 | /** |
||
20 | * @param string $operation |
||
21 | * @param string $argument |
||
22 | * |
||
23 | * @return $this |
||
24 | */ |
||
25 | public function addManipulation(string $operation, string $argument) |
||
26 | { |
||
27 | $lastIndex = count($this->groups) - 1; |
||
28 | |||
29 | $this->groups[$lastIndex][$operation] = $argument; |
||
30 | |||
31 | return $this; |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * @param \Spatie\Image\ManipulationSequence $sequence |
||
36 | * |
||
37 | * @return $this |
||
38 | */ |
||
39 | public function merge(ManipulationSequence $sequence) |
||
40 | { |
||
41 | $sequenceArray = $sequence->toArray(); |
||
42 | |||
43 | $this->mergeArray($sequenceArray); |
||
44 | |||
45 | return $this; |
||
46 | } |
||
47 | |||
48 | public function mergeArray(array $sequenceArray) |
||
49 | { |
||
50 | foreach ($sequenceArray as $group) { |
||
51 | foreach ($group as $name => $argument) { |
||
52 | $this->addManipulation($name, $argument); |
||
53 | } |
||
54 | |||
55 | if (next($sequenceArray)) { |
||
56 | $this->startNewGroup(); |
||
57 | } |
||
58 | } |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @return $this |
||
63 | */ |
||
64 | public function startNewGroup() |
||
70 | |||
71 | public function toArray(): array |
||
75 | |||
76 | public function getGroups(): array |
||
80 | |||
81 | public function getIterator(): ArrayIterator |
||
85 | |||
86 | /** |
||
87 | * @param string $manipulationName |
||
88 | * |
||
89 | * @return $this |
||
90 | */ |
||
91 | public function removeManipulation(string $manipulationName) |
||
101 | |||
102 | public function isEmpty(): bool |
||
103 | { |
||
104 | if (count($this->groups) > 1) { |
||
105 | return false; |
||
114 | |||
115 | protected function sanitizeManipulationSets(array $groups): array |
||
121 | |||
122 | /* |
||
123 | * Determine if the sequences contain a manipulation with the given name. |
||
124 | */ |
||
125 | View Code Duplication | public function getFirstManipulationArgument($searchManipulationName) |
|
136 | |||
137 | /* |
||
138 | * Determine if the sequences contain a manipulation with the given name. |
||
139 | */ |
||
140 | View Code Duplication | public function contains($searchManipulationName) |
|
151 | } |
||
152 |
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.