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 |
||
28 | class Sunlight |
||
29 | { |
||
30 | |||
31 | private static $key = null; |
||
32 | |||
33 | public $options = [ |
||
34 | 'asnyc' => false, |
||
35 | 'format' => 'json' |
||
36 | ]; |
||
37 | |||
38 | /** |
||
39 | * Create the Sunlight object |
||
40 | * |
||
41 | * @param string $key Sunlight Foundation API Key |
||
42 | * @param array $options global config options |
||
43 | */ |
||
44 | public function __construct($key = null, $options = []) |
||
58 | |||
59 | /** |
||
60 | * Set or retrieve option value |
||
61 | * |
||
62 | * @param string or array $key the option key to set/retrieve, or an array of options to set |
||
63 | * @param object $value value to store |
||
64 | * |
||
65 | * @return object if key is string and value is null, return options[key] value |
||
66 | */ |
||
67 | View Code Duplication | public function option($key, $value = null) |
|
80 | |||
81 | /** |
||
82 | * Get an API instance |
||
83 | * |
||
84 | * @param string $api api class path |
||
85 | * @param array $options local options for api |
||
86 | * |
||
87 | * @return object api instance |
||
88 | */ |
||
89 | public function get($api, $options) |
||
93 | |||
94 | /** |
||
95 | * Get OpenStates API instance |
||
96 | * |
||
97 | * @param array $options OpenStates api options |
||
98 | * |
||
99 | * @return object OpenStates api |
||
100 | */ |
||
101 | public function openStates($options = []) |
||
107 | |||
108 | /** |
||
109 | * Get Congress API instance |
||
110 | * |
||
111 | * @param array $options Congress api options |
||
112 | * |
||
113 | * @return object Congress api |
||
114 | */ |
||
115 | public function congress($options = []) |
||
121 | } |
||
122 |
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.