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 |
||
21 | * @author Filippo Galante <[email protected]> |
||
22 | */ |
||
23 | View Code Duplication | class BuzzConnector implements ConnectorInterface |
|
|
|||
24 | { |
||
25 | |||
26 | /** |
||
27 | * Establish an adapter connection. |
||
28 | * |
||
29 | * @param string[] $config |
||
30 | * |
||
31 | * @return \IlGala\SMSFactor\Adapters\BuzzAdapter |
||
32 | */ |
||
33 | public function connect(array $config) |
||
38 | |||
39 | /** |
||
40 | * Get the configuration. |
||
41 | * |
||
42 | * @param string[] $config |
||
43 | * |
||
44 | * @throws \InvalidArgumentException |
||
45 | * |
||
46 | * @return string[] |
||
47 | */ |
||
48 | protected function getConfig(array $config) |
||
55 | |||
56 | /** |
||
57 | * Get the buzz adapter. |
||
58 | * |
||
59 | * @param string[] $config |
||
60 | * |
||
61 | * @return \IlGala\SMSFactor\Adapters\BuzzAdapter |
||
62 | */ |
||
63 | protected function getAdapter(array $config) |
||
64 | { |
||
65 | return new BuzzAdapter($config['username'], $config['password'], $config['accept']); |
||
66 | } |
||
69 |
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.