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 |
||
24 | final class LiipImagineSerializableField implements Annotation |
||
25 | { |
||
26 | /** |
||
27 | * @var string $filter LiipImagine Filter |
||
28 | */ |
||
29 | private $filter; |
||
30 | |||
31 | /** |
||
32 | * @var string $vichUploaderField Field |
||
33 | */ |
||
34 | private $vichUploaderField; |
||
35 | |||
36 | /** |
||
37 | * @var string $virtualField Virtual Field |
||
38 | */ |
||
39 | private $virtualField; |
||
40 | |||
41 | /** |
||
42 | * @var array $options Options |
||
43 | */ |
||
44 | private $options; |
||
45 | |||
46 | |||
47 | /** |
||
48 | * Constructor |
||
49 | * |
||
50 | * @param array $options Options |
||
51 | * |
||
52 | * @throws \Exception |
||
53 | */ |
||
54 | public function __construct(array $options) |
||
55 | { |
||
56 | $this->options = $options; |
||
57 | |||
58 | if (!array_key_exists('value', $this->options) && !array_key_exists('filter', $this->options)) { |
||
59 | throw new \LogicException(sprintf('Either "value" or "filter" option must be set.')); |
||
60 | } |
||
61 | |||
62 | if ($this->checkArrayOption('value')) { |
||
63 | $this->setFilter($options['value']); |
||
64 | } elseif ($this->checkArrayOption('filter')) { |
||
65 | $this->setFilter($this->options['filter']); |
||
66 | } |
||
67 | |||
68 | if ($this->checkStringOption('vichUploaderField')) { |
||
69 | $this->setVichUploaderField($this->options['vichUploaderField']); |
||
70 | } |
||
71 | |||
72 | if ($this->checkStringOption('virtualField')) { |
||
73 | $this->setVirtualField($this->options['virtualField']); |
||
74 | } |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @return string |
||
79 | */ |
||
80 | public function getFilter() |
||
84 | |||
85 | /** |
||
86 | * @param string $filter |
||
87 | * @return $this |
||
88 | */ |
||
89 | public function setFilter($filter) |
||
95 | |||
96 | /** |
||
97 | * @return string |
||
98 | */ |
||
99 | public function getVichUploaderField() |
||
103 | |||
104 | /** |
||
105 | * @param string $vichUploaderField |
||
106 | * @return $this |
||
107 | */ |
||
108 | public function setVichUploaderField($vichUploaderField) |
||
114 | |||
115 | /** |
||
116 | * @return string |
||
117 | */ |
||
118 | public function getVirtualField() |
||
122 | |||
123 | /** |
||
124 | * @param string $virtualField |
||
125 | * @return $this |
||
126 | */ |
||
127 | public function setVirtualField($virtualField) |
||
133 | |||
134 | /** |
||
135 | * @param $optionName |
||
136 | * @return bool |
||
137 | * @throws \Exception |
||
138 | */ |
||
139 | private function checkStringOption($optionName) |
||
151 | |||
152 | /** |
||
153 | * @param $optionName |
||
154 | * @return bool |
||
155 | * @throws \Exception |
||
156 | */ |
||
157 | private function checkArrayOption($optionName) |
||
169 | } |
||
170 |
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.