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 |
||
6 | class RpcRequestCollection implements RpcRequestCollectionInterface, \IteratorAggregate, \Countable |
||
7 | { |
||
8 | private $priority; |
||
9 | public $requests = []; |
||
10 | |||
11 | public function __construct($priority = RpcRequestCollectionInterface::PRIORITY_LOW) |
||
15 | |||
16 | public function getIterator() |
||
20 | |||
21 | public function count() |
||
25 | |||
26 | View Code Duplication | public function add(RpcRequestInterface $request) |
|
37 | |||
38 | public function all() |
||
42 | |||
43 | public function get($id) |
||
51 | |||
52 | /** |
||
53 | * @param RpcRequest $request |
||
54 | * @return string|int|null |
||
55 | */ |
||
56 | public function getRequestIndex(RpcRequest $request) |
||
60 | |||
61 | /** |
||
62 | * @param $id |
||
63 | */ |
||
64 | public function remove($id) |
||
68 | |||
69 | /** |
||
70 | * @param RpcRequestCollection $collection |
||
71 | */ |
||
72 | public function addCollection(RpcRequestCollection $collection) |
||
79 | |||
80 | /** |
||
81 | * @param $priorityNumber |
||
82 | * @return string |
||
83 | */ |
||
84 | public function changePriority($priorityNumber) |
||
93 | |||
94 | |||
95 | public function clear() |
||
99 | |||
100 | /** |
||
101 | * Return octect priority |
||
102 | * |
||
103 | * @return int |
||
104 | */ |
||
105 | public function getPriority() |
||
109 | } |
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.