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 |
||
8 | class RouterComposite implements RouterCompositeInterface |
||
9 | { |
||
10 | /** |
||
11 | * @var RouterInterface[]|RouterCompositeInterface[] |
||
12 | */ |
||
13 | protected $bus; |
||
14 | |||
15 | /** |
||
16 | * @param RouterInterface[]|RouterCompositeInterface[] $bus |
||
17 | */ |
||
18 | 15 | public function __construct($bus = []) |
|
27 | |||
28 | /** |
||
29 | * |
||
30 | */ |
||
31 | 19 | public function __destruct() |
|
35 | |||
36 | /** |
||
37 | * @override |
||
38 | * @inheritDoc |
||
39 | */ |
||
40 | 5 | public function existsBus($name) |
|
44 | |||
45 | /** |
||
46 | * @override |
||
47 | * @inheritDoc |
||
48 | */ |
||
49 | 2 | View Code Duplication | public function getBus($name) |
58 | |||
59 | /** |
||
60 | * @override |
||
61 | * @inheritDoc |
||
62 | */ |
||
63 | 10 | public function setBus($name, $router) |
|
69 | |||
70 | /** |
||
71 | * @override |
||
72 | * @inheritDoc |
||
73 | */ |
||
74 | 2 | public function removeBus($name) |
|
83 | |||
84 | /** |
||
85 | * @override |
||
86 | * @inheritDoc |
||
87 | */ |
||
88 | 1 | public function getBuses() |
|
92 | |||
93 | /** |
||
94 | * @override |
||
95 | * @inheritDoc |
||
96 | */ |
||
97 | 1 | public function handle($name, ProtocolInterface $protocol, $flags = 0, callable $success = null, callable $failure = null, callable $cancel = null, $timeout = 0.0) |
|
98 | { |
||
99 | 1 | $handled = false; |
|
100 | |||
101 | 1 | foreach ($this->bus as $name=>$router) |
|
102 | { |
||
103 | 1 | if ($router->handle($name, $protocol, $flags, $success, $failure, $cancel, $timeout)) |
|
104 | { |
||
105 | 1 | $handled = true; |
|
106 | } |
||
107 | } |
||
108 | |||
109 | 1 | return $handled; |
|
110 | } |
||
111 | |||
112 | /** |
||
113 | * @override |
||
114 | * @inheritDoc |
||
115 | */ |
||
116 | 1 | public function erase() |
|
123 | |||
124 | /** |
||
125 | * @override |
||
126 | * @inheritDoc |
||
127 | */ |
||
128 | 1 | View Code Duplication | public function addRule(callable $matcher, callable $handler, $propagate = false, $limit = 0) |
139 | |||
140 | /** |
||
141 | * @override |
||
142 | * @inheritDoc |
||
143 | */ |
||
144 | 1 | View Code Duplication | public function addDefault(callable $handler, $propagate = false, $limit = 0) |
155 | } |
||
156 |
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.