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