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 |
||
4 | class XMLStreamObject |
||
5 | { |
||
6 | use \PHPDaemon\Traits\ClassWatchdog; |
||
7 | use \PHPDaemon\Traits\StaticObjectWatchdog; |
||
8 | |||
9 | /** |
||
10 | * Tag name |
||
11 | * |
||
12 | * @var string |
||
13 | */ |
||
14 | public $name; |
||
15 | |||
16 | /** |
||
17 | * Namespace |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | public $ns; |
||
22 | |||
23 | /** |
||
24 | * Attributes |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | public $attrs = []; |
||
29 | |||
30 | /** |
||
31 | * Subs? |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | public $subs = []; |
||
36 | |||
37 | /** |
||
38 | * Node data |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | public $data = ''; |
||
43 | |||
44 | /** |
||
45 | * Constructor |
||
46 | * |
||
47 | * @param string $name |
||
48 | * @param string $ns |
||
49 | * @param array $attrs |
||
50 | * @param string $data |
||
51 | */ |
||
52 | public function __construct($name, $ns = '', $attrs = [], $data = '') |
||
63 | |||
64 | /** |
||
65 | * Dump this XML Object to output. |
||
66 | * |
||
67 | * @param integer $depth |
||
68 | */ |
||
69 | public function printObj($depth = 0) |
||
77 | |||
78 | /** |
||
79 | * Return this XML Object in xml notation |
||
80 | * |
||
81 | * @param string $str |
||
82 | */ |
||
83 | public function toString($str = '') |
||
100 | |||
101 | /** |
||
102 | * Has this XML Object the given sub? |
||
103 | * |
||
104 | * @param string $name |
||
105 | * @return boolean |
||
106 | */ |
||
107 | View Code Duplication | public function hasSub($name, $ns = null) |
|
116 | |||
117 | /** |
||
118 | * Return a sub |
||
119 | * |
||
120 | * @param string $name |
||
121 | * @param string $attrs |
||
122 | * @param string $ns |
||
123 | */ |
||
124 | View Code Duplication | public function sub($name, $attrs = null, $ns = null) |
|
134 | } |
||
135 |
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.