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 |
||
16 | abstract class PACSoapRequest |
||
17 | { |
||
18 | /** |
||
19 | * Client username. |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $username; |
||
24 | |||
25 | /** |
||
26 | * Client password. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $password; |
||
31 | |||
32 | /** |
||
33 | * CFDI XML. |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $xml; |
||
38 | |||
39 | /** |
||
40 | * @param bool |
||
41 | */ |
||
42 | protected $test; |
||
43 | |||
44 | /** @var array */ |
||
45 | protected $options = [ |
||
46 | 'soap_version' => SOAP_1_2, |
||
47 | 'trace' => 1, |
||
48 | ]; |
||
49 | |||
50 | /** |
||
51 | * Create a new invoice one strategy instance. |
||
52 | * |
||
53 | * @param string $username |
||
54 | * @param string $password |
||
55 | * @param string $xml |
||
56 | * @param bool $test |
||
57 | */ |
||
58 | View Code Duplication | public function __construct(string $username, string $password, string $xml, bool $test = false) |
|
67 | |||
68 | /** |
||
69 | * Soap request. |
||
70 | * |
||
71 | * @return mixed |
||
72 | */ |
||
73 | abstract protected function makeRequest(); |
||
74 | |||
75 | /** |
||
76 | * Returns the cfdi with the TFD node. |
||
77 | * |
||
78 | * @return string |
||
79 | */ |
||
80 | abstract public function getXML(); |
||
81 | |||
82 | /** |
||
83 | * @param string $filename |
||
84 | * |
||
85 | * @return integer |
||
86 | */ |
||
87 | public function save(string $filename) |
||
96 | } |
||
97 |
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.