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 | class Messages |
||
17 | { |
||
18 | /** |
||
19 | * @var ClientInterface |
||
20 | */ |
||
21 | private $client; |
||
22 | |||
23 | /** |
||
24 | * @var MessageFactory |
||
25 | */ |
||
26 | private $factory; |
||
27 | |||
28 | /** |
||
29 | * @var int |
||
30 | */ |
||
31 | private $roomId; |
||
32 | |||
33 | /** |
||
34 | * Messages constructor. |
||
35 | * |
||
36 | * @param ClientInterface $client |
||
37 | * @param MessageFactory $factory |
||
38 | * @param int $roomId |
||
39 | */ |
||
40 | public function __construct(ClientInterface $client, MessageFactory $factory, int $roomId) |
||
46 | |||
47 | /** |
||
48 | * @param bool $force |
||
49 | * |
||
50 | * @return CollectionInterface |
||
51 | */ |
||
52 | View Code Duplication | public function show($force = false) |
|
63 | |||
64 | /** |
||
65 | * @param $id |
||
66 | * @param bool $force |
||
67 | * |
||
68 | * @return Message|EntityInterface |
||
69 | */ |
||
70 | View Code Duplication | public function detail($id, $force = false) |
|
81 | |||
82 | /** |
||
83 | * @param Message $message |
||
84 | */ |
||
85 | public function create(Message $message) |
||
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.