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 |
||
13 | class Config |
||
14 | { |
||
15 | protected $config = [ |
||
16 | Options::COLUMNS => 48, |
||
17 | Options::IMAGE_CELL_SIZE => 2, |
||
18 | Options::IMAGE_HUE => null, |
||
19 | Options::NEIGHBORHOOD_TYPE => NeighborhoodOptions::NEIGHBORHOOD_TYPE_MOORE, |
||
20 | Options::NEIGHBORHOOD_SIZE => 1, |
||
21 | Options::ROWS => 48, |
||
22 | Options::SEED => null, |
||
23 | Options::STATES => 3, |
||
24 | Options::THRESHOLD => 3, |
||
25 | ]; |
||
26 | |||
27 | 3 | public function __construct() |
|
33 | |||
34 | protected function makeHue(): int |
||
38 | |||
39 | 3 | protected function makeSeed(): int |
|
45 | |||
46 | 3 | public static function createFromPreset(string $preset): self |
|
58 | |||
59 | /** |
||
60 | * The amount of columns in the grid. |
||
61 | * |
||
62 | * @param int number of columns |
||
63 | * |
||
64 | * @return int |
||
65 | */ |
||
66 | 3 | public function columns($columns = null): int |
|
74 | |||
75 | /** |
||
76 | * Set the size of each cell in the image that is created. |
||
77 | * |
||
78 | * @param int $cellsize |
||
79 | * |
||
80 | * @return int |
||
81 | */ |
||
82 | 3 | public function imageCellSize($cellsize = null): int |
|
83 | { |
||
84 | 3 | if (isset($cellsize)) { |
|
85 | 3 | $this->config[Options::IMAGE_CELL_SIZE] = (int) $cellsize; |
|
86 | } |
||
87 | |||
88 | 3 | return $this->config[Options::IMAGE_CELL_SIZE]; |
|
89 | } |
||
90 | |||
91 | public function imageHue($hue = null): int |
||
92 | { |
||
93 | if (isset($hue) && $this->isValidHue($hue)) { |
||
94 | $this->config[Options::IMAGE_HUE] = (int) $hue; |
||
95 | } |
||
96 | |||
97 | return $this->config[Options::IMAGE_HUE]; |
||
98 | } |
||
99 | |||
100 | protected function isValidHue($hue): bool |
||
112 | |||
113 | /** |
||
114 | * Set the size (eg range) of the neighborhood. |
||
115 | * |
||
116 | * @param int $neighborhoodSize size of neighborhood |
||
117 | * |
||
118 | * @return int |
||
119 | */ |
||
120 | 3 | public function neighborhoodSize($neighborhoodSize = null): int |
|
128 | |||
129 | /** |
||
130 | * Set the neighborhood function, eg Moore or Neumann |
||
131 | * |
||
132 | * @see NeighborhoodOptions::NEIGHBORHOOD_TYPE_MOORE |
||
133 | * @see NeighborhoodOptions::NEIGHBORHOOD_TYPE_NEUMANN |
||
134 | * |
||
135 | * @param string $neighborhoodType |
||
136 | * |
||
137 | * @return string type of neighborhood |
||
138 | * |
||
139 | * @throws \Barryvanveen\CCA\Exceptions\InvalidNeighborhoodTypeException |
||
140 | */ |
||
141 | 6 | public function neighborhoodType($neighborhoodType = null): string |
|
153 | |||
154 | /** |
||
155 | * The amount of rows in the grid. |
||
156 | * |
||
157 | * @param int number of rows |
||
158 | * |
||
159 | * @return int |
||
160 | */ |
||
161 | 3 | View Code Duplication | public function rows($rows = null): int |
169 | |||
170 | /** |
||
171 | * Set a seed for the random number generator. Use this for reproducable of runs. |
||
172 | * |
||
173 | * @param int $seed |
||
174 | * |
||
175 | * @return int |
||
176 | */ |
||
177 | 3 | View Code Duplication | public function seed($seed = null): int |
185 | |||
186 | /** |
||
187 | * The number of states that each cell cycles through. |
||
188 | * |
||
189 | * @param int number of states |
||
190 | * |
||
191 | * @return int |
||
192 | */ |
||
193 | 3 | public function states($states = null): int |
|
201 | |||
202 | /** |
||
203 | * The threshold of the cells. Only when <threshold> or more of a cells' neighbors have |
||
204 | * the successor state will the cell cycle to the successor state itself. |
||
205 | * |
||
206 | * @param int threshold |
||
207 | * |
||
208 | * @return int |
||
209 | */ |
||
210 | 3 | View Code Duplication | public function threshold($threshold = null): int |
218 | |||
219 | 3 | public function toArray(): array |
|
223 | } |
||
224 |
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.