Total Complexity | 7 |
Total Lines | 74 |
Duplicated Lines | 9.46 % |
Coverage | 100% |
Changes | 0 |
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 |
||
5 | class UpdateReplyMessage implements Message { |
||
6 | |||
7 | /** |
||
8 | * Notification message data. |
||
9 | * |
||
10 | * @var array |
||
11 | */ |
||
12 | protected $data; |
||
13 | |||
14 | /** |
||
15 | * Create a new message instance. |
||
16 | * |
||
17 | * @param array $data |
||
18 | * @return void |
||
19 | */ |
||
20 | 1 | public function __construct( array $data = [] ) { |
|
21 | 1 | $this->data = $data; |
|
22 | 1 | } |
|
23 | |||
24 | /** |
||
25 | * Get message text. |
||
26 | * |
||
27 | * @return string |
||
28 | */ |
||
29 | 2 | View Code Duplication | public function message() { |
|
|||
30 | 2 | if ( $this->data['total'] > 1 ) { |
|
31 | 1 | return sprintf( 'You have %d new replies', (int) $this->data['total'] ); |
|
32 | } |
||
33 | |||
34 | 1 | return sprintf( '%s commented on one of your updates', bp_core_get_user_displayname( $this->data['user_id' ] ) ); |
|
35 | } |
||
36 | |||
37 | /** |
||
38 | * Get message URL. |
||
39 | * |
||
40 | * @return string |
||
41 | */ |
||
42 | 2 | public function url() { |
|
43 | 2 | if ( $this->data['total'] > 1 ) { |
|
44 | 1 | return add_query_arg( |
|
45 | 1 | 'type', |
|
46 | 1 | $this->data['action'], |
|
47 | 1 | bp_get_notifications_permalink( $this->data['user_id'] ) ); |
|
48 | } |
||
49 | |||
50 | 1 | return add_query_arg( |
|
51 | 1 | 'nid', |
|
52 | 1 | $this->data['id'], |
|
53 | 1 | bp_activity_get_permalink( $this->data['activity_id'] ) |
|
54 | ); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Get the array representation of the message. |
||
59 | * |
||
60 | * @return array |
||
61 | */ |
||
62 | 1 | public function toArray() { |
|
63 | return [ |
||
64 | 1 | 'text' => $this->message(), |
|
65 | 1 | 'link' => $this->url(), |
|
66 | ]; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Get the HTML representation of the message. |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | 1 | public function toHtml() { |
|
79 | ); |
||
80 | } |
||
81 | } |
||
82 |
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.