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 |
||
11 | class Project |
||
12 | { |
||
13 | |||
14 | /** |
||
15 | * @var null|string |
||
16 | */ |
||
17 | private $name; |
||
18 | |||
19 | /** |
||
20 | * @var HttpClient |
||
21 | */ |
||
22 | private $client; |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | private $actions = [ |
||
28 | "jobs" => ["xml"], |
||
29 | "jobs/export" => ["xml"], |
||
30 | "resources" => ["xml"], |
||
31 | "executions" => ["xml"], |
||
32 | "executions/running" => ["xml"], |
||
33 | "export" => ["xml"], |
||
34 | "history" => ["xml"], |
||
35 | ]; |
||
36 | |||
37 | /** |
||
38 | * @param HttpClient $client |
||
39 | * @param string $name : Execution ID |
||
40 | */ |
||
41 | 12 | public function __construct(HttpClient $client, $name = null) |
|
46 | |||
47 | /** |
||
48 | * Get project info |
||
49 | * @param string $alt xml|json |
||
50 | * @return array |
||
51 | */ |
||
52 | 3 | public function find($alt = "xml") |
|
57 | |||
58 | /** |
||
59 | * Find all projects |
||
60 | * @param string $alt |
||
61 | * @return array |
||
62 | */ |
||
63 | 3 | public function findAll($alt = "xml") |
|
68 | |||
69 | /** |
||
70 | * Get Project action |
||
71 | * @param $action |
||
72 | * @param string $alt xml|json |
||
73 | * @return array |
||
74 | * @throws \Exception |
||
75 | */ |
||
76 | 3 | View Code Duplication | public function get($action, $alt = "xml") |
88 | |||
89 | /** |
||
90 | * Get project resource |
||
91 | * @param $name |
||
92 | * @param string $alt xml|json |
||
93 | * @return array |
||
94 | */ |
||
95 | 3 | public function resource($name, $alt = "xml") |
|
100 | } |
||
101 |
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.