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 Job |
||
12 | { |
||
13 | |||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | private $name; |
||
18 | |||
19 | /** |
||
20 | * @var HttpClient |
||
21 | */ |
||
22 | private $client; |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | private $actions = [ |
||
28 | "executions" => ["xml"], |
||
29 | "input/files" => ["xml"], |
||
30 | "info" => ["xml"], |
||
31 | ]; |
||
32 | |||
33 | /** |
||
34 | * @param HttpClient $client |
||
35 | * @param string $name |
||
36 | */ |
||
37 | 6 | public function __construct(HttpClient $client, $name = null) |
|
42 | |||
43 | /** |
||
44 | * @param string $alt |
||
45 | * @return mixed |
||
46 | */ |
||
47 | 3 | public function find($alt = "xml") |
|
52 | |||
53 | /** |
||
54 | * Get Job action |
||
55 | * @param $action |
||
56 | * @param string $alt xml|json |
||
57 | * @return array |
||
58 | * @throws \Exception |
||
59 | */ |
||
60 | 3 | View Code Duplication | public function get($action, $alt = "xml") |
72 | } |
||
73 |
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.