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 |
||
7 | class FieldsSeeder extends Seeder |
||
|
|||
8 | { |
||
9 | /** |
||
10 | * @var \Carbon\Carbon |
||
11 | */ |
||
12 | protected $now; |
||
13 | |||
14 | /** |
||
15 | * @var array |
||
16 | */ |
||
17 | protected $fields = [ |
||
18 | [ |
||
19 | 'id' => 1, |
||
20 | 'name' => 'Operating System', |
||
21 | 'values' => ['iOS', 'Android', 'Windows Phone'], // 1, 2, 3 |
||
22 | ], |
||
23 | [ |
||
24 | 'id' => 2, |
||
25 | 'name' => 'RAM Size', |
||
26 | 'values' => ['512MB', '1GB', '2GB'], // 4, 5, 6 |
||
27 | ], |
||
28 | [ |
||
29 | 'id' => 3, |
||
30 | 'name' => 'Storage Size', |
||
31 | 'values' => ['16GB', '32GB', '64GB', '128GB', '256GB'], // 7, 8, 9, 10, 11 |
||
32 | ], |
||
33 | [ |
||
34 | 'id' => 4, |
||
35 | 'name' => 'Screen Resolution', |
||
36 | 'values' => ['375x667', '414x736', '960x540', '1136x640', '1280x768', '1280x720', '1334x750', '1920x1080'], // 12, 13, 14, 15, 16, 17, 18, 19 |
||
37 | ], |
||
38 | [ |
||
39 | 'id' => 5, |
||
40 | 'name' => 'CPU Cores Count', |
||
41 | 'values' => ['1', '2', '4', '6', '8'], // 20, 21, 22, 23, 24 |
||
42 | ], |
||
43 | ]; |
||
44 | |||
45 | /** |
||
46 | * Run the fields seeder. |
||
47 | */ |
||
48 | public function run() |
||
64 | |||
65 | /** |
||
66 | * @param int $id |
||
67 | * @param string $name |
||
68 | * @return array |
||
69 | */ |
||
70 | View Code Duplication | protected function createField(int $id, string $name) |
|
79 | |||
80 | /** |
||
81 | * @param int $fieldId |
||
82 | * @param int $fieldValueId |
||
83 | * @param array $values |
||
84 | * @return array |
||
85 | */ |
||
86 | protected function createFieldValues(int $fieldId, int $fieldValueId, array $values) |
||
104 | } |
||
105 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.