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 |
||
35 | View Code Duplication | class StringMessage extends AbstractMessage |
|
|
|||
36 | { |
||
37 | |||
38 | /** |
||
39 | * The message id as hash value. |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $messageId = null; |
||
44 | |||
45 | /** |
||
46 | * The message itself. |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $message = null; |
||
51 | |||
52 | /** |
||
53 | * Initializes the message with the String |
||
54 | * to send to the queue. |
||
55 | * |
||
56 | * @param string $message The string with the data to send |
||
57 | * |
||
58 | * @throws \Exception Is thrown if the passed message is not a valid string value |
||
59 | */ |
||
60 | 1 | public function __construct($message) |
|
76 | |||
77 | /** |
||
78 | * Returns the unique message-ID. |
||
79 | * |
||
80 | * @return string The unique message-ID |
||
81 | */ |
||
82 | public function getMessageId() |
||
86 | |||
87 | /** |
||
88 | * The message itself. |
||
89 | * |
||
90 | * @return string The message itself |
||
91 | */ |
||
92 | public function getMessage() |
||
96 | |||
97 | /** |
||
98 | * Returns the message as string. |
||
99 | * |
||
100 | * @return string The message as string |
||
101 | */ |
||
102 | public function __toString() |
||
106 | } |
||
107 |
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.