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 ProductsSeeder extends Seeder |
||
|
|||
9 | { |
||
10 | /** |
||
11 | * @var Carbon |
||
12 | */ |
||
13 | protected $now; |
||
14 | |||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | protected $products = [ |
||
19 | [ |
||
20 | 'id' => 1, |
||
21 | 'name' => 'iPhone 6 32GB', |
||
22 | 'fields' => [ |
||
23 | 1 => 1, // Operating System: iOS |
||
24 | 2 => 5, // RAM Size: 1GB |
||
25 | 3 => 8, // Storage Size: 32GB |
||
26 | 4 => 12, // Screen Resolution: 375x667 |
||
27 | 5 => 21, // CPU Cores Count: 2 |
||
28 | ], |
||
29 | ], |
||
30 | [ |
||
31 | 'id' => 2, |
||
32 | 'name' => 'iPhone 7 32GB', |
||
33 | 'fields' => [ |
||
34 | 1 => 1, // Operating System: iOS |
||
35 | 2 => 6, // RAM Size: 1GB |
||
36 | 3 => 8, // Storage Size: 32GB |
||
37 | 4 => 18, // Screen Resolution: 375x667 |
||
38 | 5 => 21, // CPU Cores Count: 2 |
||
39 | ], |
||
40 | ], |
||
41 | [ |
||
42 | 'id' => 3, |
||
43 | 'name' => 'Samsung Galaxy S7', |
||
44 | 'fields' => [ |
||
45 | 1 => 2, // Operating System: Android |
||
46 | 2 => 7, // RAM Size: 2GB |
||
47 | 3 => 8, // Storage Size: 32GB |
||
48 | 4 => 19, // Screen Resolution: 1920x1080 |
||
49 | 5 => 23, // CPU Cores Count: 6 |
||
50 | ], |
||
51 | ], |
||
52 | [ |
||
53 | 'id' => 4, |
||
54 | 'name' => 'Samsung Galaxy S8', |
||
55 | 'fields' => [ |
||
56 | 1 => 2, // Operating System: Android |
||
57 | 2 => 7, // RAM Size: 2GB |
||
58 | 3 => 9, // Storage Size: 64GB |
||
59 | 4 => 19, // Screen Resolution: 1920x1080 |
||
60 | 5 => 24, // CPU Cores Count: 8 |
||
61 | ], |
||
62 | ], |
||
63 | ]; |
||
64 | |||
65 | /** |
||
66 | * Run the products seeder. |
||
67 | */ |
||
68 | public function run() |
||
77 | |||
78 | /** |
||
79 | * @param array $product |
||
80 | * |
||
81 | * @return array |
||
82 | */ |
||
83 | View Code Duplication | protected function createProduct(array $product) |
|
92 | |||
93 | /** |
||
94 | * @param int $productId |
||
95 | * @param array $fields |
||
96 | * |
||
97 | * @return array |
||
98 | */ |
||
99 | protected function productFields(int $productId, array $fields) |
||
114 | } |
||
115 |
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.