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:
Complex classes like BrowscapPhp often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BrowscapPhp, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class BrowscapPhp extends AbstractProvider |
||
10 | { |
||
11 | protected $defaultValues = [ |
||
12 | 'DefaultProperties', |
||
13 | 'Default Browser', |
||
14 | |||
15 | 'unknown', |
||
16 | ]; |
||
17 | |||
18 | /** |
||
19 | * |
||
20 | * @var Browscap |
||
21 | */ |
||
22 | private $parser; |
||
23 | |||
24 | 15 | public function __construct(Browscap $parser) |
|
28 | |||
29 | 1 | public function getName() |
|
33 | |||
34 | 1 | public function getComposerPackageName() |
|
38 | |||
39 | 1 | public function getVersion() |
|
45 | |||
46 | /** |
||
47 | * |
||
48 | * @param Browscap $parser |
||
49 | */ |
||
50 | 15 | public function setParser(Browscap $parser) |
|
54 | |||
55 | /** |
||
56 | * |
||
57 | * @return Browscap |
||
58 | */ |
||
59 | 13 | public function getParser() |
|
63 | |||
64 | /** |
||
65 | * |
||
66 | * @return array |
||
67 | */ |
||
68 | 1 | private function getDeviceModelDefaultValues() |
|
77 | |||
78 | /** |
||
79 | * |
||
80 | * @param stdClass $resultRaw |
||
81 | * @return boolean |
||
82 | */ |
||
83 | 5 | private function isBot(stdClass $resultRaw) |
|
91 | |||
92 | /** |
||
93 | * |
||
94 | * @param stdClass $resultRaw |
||
95 | * |
||
96 | * @return bool |
||
97 | */ |
||
98 | 11 | private function hasResult(stdClass $resultRaw) |
|
110 | |||
111 | /** |
||
112 | * |
||
113 | * @param stdClass $resultRaw |
||
114 | * @param Model\UserAgent $result |
||
115 | */ |
||
116 | 3 | private function hydrateBotResult(stdClass $resultRaw, Model\UserAgent $result) |
|
134 | |||
135 | /** |
||
136 | * |
||
137 | * @param stdClass $resultRaw |
||
138 | * @param Model\UserAgent $result |
||
139 | */ |
||
140 | 2 | View Code Duplication | private function hydrateBrowser(stdClass $resultRaw, Model\UserAgent $result) |
152 | |||
153 | /** |
||
154 | * |
||
155 | * @param stdClass $resultRaw |
||
156 | * @param Model\UserAgent $result |
||
157 | */ |
||
158 | 2 | View Code Duplication | private function hydrateRenderingEngine(stdClass $resultRaw, Model\UserAgent $result) |
170 | |||
171 | /** |
||
172 | * |
||
173 | * @param stdClass $resultRaw |
||
174 | * @param Model\UserAgent $result |
||
175 | */ |
||
176 | 2 | View Code Duplication | private function hydrateOperatingSystem(stdClass $resultRaw, Model\UserAgent $result) |
188 | |||
189 | /** |
||
190 | * |
||
191 | * @param stdClass $resultRaw |
||
192 | * @param Model\UserAgent $result |
||
193 | */ |
||
194 | 2 | private function hydrateDevice(stdClass $resultRaw, Model\UserAgent $result) |
|
219 | |||
220 | 11 | public function parse($userAgent, array $headers = []) |
|
256 | } |
||
257 |
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.