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 |
||
18 | abstract class SeederTaskBase extends AppShell { |
||
19 | |||
20 | /** |
||
21 | * Faker (generator) instance |
||
22 | * |
||
23 | * @var null|Generator |
||
24 | */ |
||
25 | public $faker = null; |
||
26 | |||
27 | /** |
||
28 | * The seeds to be seeded |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | public $seeds = array(); |
||
33 | |||
34 | /** |
||
35 | * The config key to read, 'FakeSeeder.$_configKey.valueKey' |
||
36 | * |
||
37 | * Does not need to be set, uses the name of the seeder class by default, e.g. "Article" for "ArticleSeederShell". |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $_configKey = ''; |
||
42 | |||
43 | /** |
||
44 | * The name of the model to seed |
||
45 | * |
||
46 | * Does not need to be set, uses the name of the seeder class by default, e.g. "Article" for "ArticleSeederTask". |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $_modelName = ''; |
||
51 | |||
52 | /** |
||
53 | * Models to truncate |
||
54 | * |
||
55 | * Does not need to be set, uses the name of the seeder class by default, e.g. "Article" for "ArticleSeederTask". |
||
56 | * |
||
57 | * @var array |
||
58 | */ |
||
59 | protected $_modelsToTruncate = array(); |
||
60 | |||
61 | /** |
||
62 | * Fixture records which are processed additionally and before the faked ones |
||
63 | * |
||
64 | * @var array |
||
65 | */ |
||
66 | protected $_fixtureRecords = array(); |
||
67 | |||
68 | /** |
||
69 | * The fields and their formatter |
||
70 | * |
||
71 | * @var array |
||
72 | */ |
||
73 | protected $_fieldFormatters = array(); |
||
74 | |||
75 | /** |
||
76 | * The seeding mode, optional. |
||
77 | * |
||
78 | * @var null|string |
||
79 | */ |
||
80 | protected $_mode = null; |
||
81 | |||
82 | /** |
||
83 | * The locale to use for Faker, optional |
||
84 | * |
||
85 | * @var null|int |
||
86 | */ |
||
87 | protected $_locale = null; |
||
88 | |||
89 | /** |
||
90 | * Set the minimum record count for a seeder task, null means no minimum. |
||
91 | * |
||
92 | * @var null|int |
||
93 | */ |
||
94 | protected $_minRecords = null; |
||
95 | |||
96 | /** |
||
97 | * Set the maximum record count for a seeder task, null means no maximum. |
||
98 | * |
||
99 | * @var null|int |
||
100 | */ |
||
101 | protected $_maxRecords = null; |
||
102 | |||
103 | /** |
||
104 | * The records to seed, optional |
||
105 | * |
||
106 | * @var null|int |
||
107 | */ |
||
108 | protected $_records = null; |
||
109 | |||
110 | /** |
||
111 | * Whether or not to validate the seeding data when saving, optional |
||
112 | * |
||
113 | * @var null|bool|string |
||
114 | * @see Model::saveAll() See for possible values for `validate`. |
||
115 | */ |
||
116 | protected $_validateSeeding = null; |
||
117 | |||
118 | /** |
||
119 | * The seeding number for Faker to use |
||
120 | * |
||
121 | * @var null|bool|int |
||
122 | * @see Generator::seed Faker's seed method. |
||
123 | */ |
||
124 | protected $_seedingNumber = null; |
||
125 | |||
126 | /** |
||
127 | * Whether or not to truncate the model , optional. |
||
128 | * |
||
129 | * @var null|bool |
||
130 | */ |
||
131 | protected $_noTruncate = null; |
||
132 | |||
133 | /** |
||
134 | * Task execution method |
||
135 | * |
||
136 | * @return void |
||
137 | */ |
||
138 | public function execute() { |
||
154 | |||
155 | /** |
||
156 | * Get a ShellSeedProcessor instance |
||
157 | * |
||
158 | * @return ShellSeedProcessor An instance of ShellSeedProcessor. |
||
159 | */ |
||
160 | protected function _getSeedProcessor() { |
||
163 | |||
164 | /** |
||
165 | * Get the Faker generator with the (optionally) configured locale |
||
166 | * |
||
167 | * @return Generator |
||
168 | */ |
||
169 | protected function _getFaker() { |
||
182 | |||
183 | /** |
||
184 | * Truncate the models |
||
185 | * |
||
186 | * @return void |
||
187 | * @see ShellModelTruncator::truncateModels |
||
188 | */ |
||
189 | protected function _truncateModels() { |
||
195 | |||
196 | /** |
||
197 | * Get an instance of the ShellModelTruncator, for delegating the model truncation |
||
198 | * |
||
199 | * @return ShellModelTruncator The shell model truncator instance. |
||
200 | */ |
||
201 | protected function _getModelTruncator() { |
||
204 | |||
205 | /** |
||
206 | * Get models to truncate |
||
207 | * |
||
208 | * Returns the ones set in $_modelsToTruncate oo |
||
209 | * gets the model name based on the current |
||
210 | * seeder shell task name. |
||
211 | * |
||
212 | * @return array The models to truncate. |
||
213 | */ |
||
214 | public function getModelsToTruncate() { |
||
222 | |||
223 | /** |
||
224 | * Set/get the fixture records |
||
225 | * |
||
226 | * @return array The fixture records. |
||
227 | */ |
||
228 | public function fixtureRecords() { |
||
231 | |||
232 | /** |
||
233 | * Set/get the field formatters |
||
234 | * |
||
235 | * @return array The formatters per field. |
||
236 | * @link https://github.com/fzaninotto/Faker#formatters |
||
237 | */ |
||
238 | abstract public function fieldFormatters(); |
||
239 | |||
240 | /** |
||
241 | * Merges the given field formatters with the exiting ones |
||
242 | * |
||
243 | * @param array $fieldFormatters The field formatters to merge. |
||
244 | * @return array The merged field formatters. |
||
245 | */ |
||
246 | protected function _mergeFieldFormatters($fieldFormatters) { |
||
253 | |||
254 | /** |
||
255 | * Set/get state per record |
||
256 | * |
||
257 | * Can be overridden to return some state with data per record. |
||
258 | * |
||
259 | * @return array The state per record. |
||
260 | */ |
||
261 | public function recordState() { |
||
264 | |||
265 | /** |
||
266 | * Get the model name |
||
267 | * |
||
268 | * @return string The model name. |
||
269 | */ |
||
270 | public function getModelName() { |
||
278 | |||
279 | /** |
||
280 | * Get the seeding mode |
||
281 | * |
||
282 | * @return mixed The the seeding mode. |
||
283 | */ |
||
284 | public function getSeedingMode() { |
||
290 | |||
291 | /** |
||
292 | * Get the locale to use for Faker |
||
293 | * |
||
294 | * @return string The locale for Faker. |
||
295 | */ |
||
296 | public function getLocale() { |
||
302 | |||
303 | /** |
||
304 | * Get record count to create |
||
305 | * |
||
306 | * @return mixed The amount of records to create. |
||
307 | */ |
||
308 | public function getRecordsCount() { |
||
319 | |||
320 | /** |
||
321 | * Enforce the maximum amount of records to be seeded |
||
322 | * |
||
323 | * @param int $records The amount of records to check/reduce. |
||
324 | * @return int The enforced maximum amount of records. |
||
325 | */ |
||
326 | View Code Duplication | protected function _enforceRecordMaximum($records) { |
|
336 | |||
337 | /** |
||
338 | * Enforce the minimum amount of records to be seeded |
||
339 | * |
||
340 | * @param int $records The amount of records to check/increase. |
||
341 | * @return int The enforced minimum amount of records. |
||
342 | */ |
||
343 | View Code Duplication | protected function _enforceRecordMinimum($records) { |
|
353 | |||
354 | /** |
||
355 | * Get whether or not to validate seeding |
||
356 | * |
||
357 | * @return bool|string Whether or not to validate seeding. |
||
358 | * @see Model::saveAll() See for possible values for `validate`. |
||
359 | */ |
||
360 | public function getValidateSeeding() { |
||
366 | |||
367 | /** |
||
368 | * Get the seed number for Faker to use |
||
369 | * |
||
370 | * @return bool|string The seed number for Faker to use |
||
371 | * @see Generator::seed Faker's seed method. |
||
372 | */ |
||
373 | public function getSeedingNumber() { |
||
379 | |||
380 | /** |
||
381 | * Get whether or not to truncate the model |
||
382 | * |
||
383 | * @return bool Whether or not to truncate the model |
||
384 | */ |
||
385 | public function getNoTruncate() { |
||
391 | |||
392 | /** |
||
393 | * Get the value of a parameter |
||
394 | * |
||
395 | * Inspects |
||
396 | * 1. The CLI parameters, e.g. "--records" |
||
397 | * 2. The seeder specific configuration, e.g. "FakeSeeder.Article.records" |
||
398 | * 3. The general seeder configuration, e.g "FakeSeeder.records" |
||
399 | * 4. The seeder shell task class properties, e.g. "$_records" |
||
400 | * 4. Falls back to an optional default value |
||
401 | * |
||
402 | * @param string $configKey The name of the config key to check. |
||
403 | * @param string $propertyName The name of the class property to check. |
||
404 | * @param string $defaultValue The default value to use as fallback, optional. |
||
405 | * @return mixed The value of the parameter. |
||
406 | */ |
||
407 | protected function _getParameter($configKey, $propertyName, $defaultValue = null) { |
||
438 | |||
439 | /** |
||
440 | * Get the seeder specific config key |
||
441 | * |
||
442 | * Can be overridden by setting $_configKey |
||
443 | * |
||
444 | * @return string The seeder specific config key. |
||
445 | * @see ::$_configKey |
||
446 | */ |
||
447 | protected function _getSeederConfigKey() { |
||
455 | |||
456 | /** |
||
457 | * Get the prefix of the seeder (class) shell task name |
||
458 | * |
||
459 | * "Article" for "ArticleSeederTask". |
||
460 | * |
||
461 | * @return string The prefix of the seeder (class) shell task name. |
||
462 | */ |
||
463 | protected function _getSeederNamePrefix() { |
||
468 | |||
469 | /** |
||
470 | * Get the name of the seeder shell |
||
471 | * |
||
472 | * @return string The name of the seeder shell. |
||
473 | * @todo Return actual name of the seeder shell (not task!). |
||
474 | */ |
||
475 | protected function _getSeederShellName() { |
||
478 | } |
||
479 |
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.