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 namespace Anomaly\Streams\Platform\Addon\Module; |
||
15 | View Code Duplication | class ModuleModel extends EloquentModel implements ModuleInterface |
|
|
|||
16 | { |
||
17 | |||
18 | /** |
||
19 | * Define the table name. |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $table = 'addons_modules'; |
||
24 | |||
25 | /** |
||
26 | * Disable timestamps for modules. |
||
27 | * |
||
28 | * @var bool |
||
29 | */ |
||
30 | public $timestamps = false; |
||
31 | |||
32 | /** |
||
33 | * Boot the model |
||
34 | */ |
||
35 | protected static function boot() |
||
41 | |||
42 | /** |
||
43 | * Find a module by it's namespace or return a new |
||
44 | * module with the given namespace. |
||
45 | * |
||
46 | * @param $namespace |
||
47 | * @return ModuleModel |
||
48 | */ |
||
49 | public function findByNamespaceOrNew($namespace) |
||
65 | |||
66 | /** |
||
67 | * Find a module by it's namespace. |
||
68 | * |
||
69 | * @param $namespace |
||
70 | * @return null|ModuleModel |
||
71 | */ |
||
72 | public function findByNamespace($namespace) |
||
76 | |||
77 | /** |
||
78 | * Get all enabled module namespaces. |
||
79 | * |
||
80 | * @return EloquentCollection |
||
81 | */ |
||
82 | public function getEnabledNamespaces() |
||
86 | |||
87 | /** |
||
88 | * Get all installed module namespaces. |
||
89 | * |
||
90 | * @return EloquentCollection |
||
91 | */ |
||
92 | public function getInstalledNamespaces() |
||
96 | |||
97 | /** |
||
98 | * Return a new collection. |
||
99 | * |
||
100 | * @param array $items |
||
101 | * @return EloquentCollection |
||
102 | */ |
||
103 | public function newCollection(array $items = []) |
||
107 | } |
||
108 |
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.