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 |
||
19 | class UserUnreadNotificationModel extends Model |
||
20 | { |
||
21 | /** |
||
22 | * SQL table name. |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | const TABLE = 'user_has_unread_notifications'; |
||
27 | |||
28 | /** |
||
29 | * Add unread notification to someone. |
||
30 | * |
||
31 | * @param int $user_id |
||
32 | * @param string $event_name |
||
33 | * @param array $event_data |
||
34 | */ |
||
35 | public function create($user_id, $event_name, array $event_data) |
||
44 | |||
45 | /** |
||
46 | * Get one notification. |
||
47 | * |
||
48 | * @param int $notification_id |
||
49 | * |
||
50 | * @return array|null |
||
51 | */ |
||
52 | public function getById($notification_id) |
||
62 | |||
63 | /** |
||
64 | * Get all notifications for a user. |
||
65 | * |
||
66 | * @param int $user_id |
||
67 | * |
||
68 | * @return array |
||
69 | */ |
||
70 | View Code Duplication | public function getAll($user_id) |
|
80 | |||
81 | /** |
||
82 | * Mark a notification as read. |
||
83 | * |
||
84 | * @param int $user_id |
||
85 | * @param int $notification_id |
||
86 | * |
||
87 | * @return bool |
||
88 | */ |
||
89 | public function markAsRead($user_id, $notification_id) |
||
93 | |||
94 | /** |
||
95 | * Mark all notifications as read for a user. |
||
96 | * |
||
97 | * @param int $user_id |
||
98 | * |
||
99 | * @return bool |
||
100 | */ |
||
101 | public function markAllAsRead($user_id) |
||
105 | |||
106 | /** |
||
107 | * Return true if the user as unread notifications. |
||
108 | * |
||
109 | * @param int $user_id |
||
110 | * |
||
111 | * @return bool |
||
112 | */ |
||
113 | public function hasNotifications($user_id) |
||
117 | |||
118 | /** |
||
119 | * Unserialize the event. |
||
120 | * |
||
121 | * @param GenericEvent $event |
||
122 | */ |
||
123 | private function unserialize(&$event) |
||
128 | } |
||
129 |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.