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 namespace Cornford\Notifier; |
||
6 | class Notifier extends NotifierAbstract implements NotifierInterface { |
||
7 | |||
8 | /** |
||
9 | * Get display notification messages. |
||
10 | * |
||
11 | * @return array |
||
12 | */ |
||
13 | public function getDisplayNotifications() |
||
28 | |||
29 | /** |
||
30 | * Update displayed status for passed notification messages. |
||
31 | * |
||
32 | * @param array $notifications |
||
33 | * |
||
34 | * @return self |
||
35 | */ |
||
36 | public function displayNotifications(array $notifications = []) |
||
44 | |||
45 | /** |
||
46 | * Update displayed status for displayable notification messages. |
||
47 | * |
||
48 | * @return self |
||
49 | */ |
||
50 | public function displayedDisplayableNotifications() |
||
58 | |||
59 | /** |
||
60 | * Update displayed status for all notification messages. |
||
61 | * |
||
62 | * @return self |
||
63 | */ |
||
64 | public function displayedAllNotifications() |
||
72 | |||
73 | /** |
||
74 | * Expire passed notification messages. |
||
75 | * |
||
76 | * @param array $notifications |
||
77 | * |
||
78 | * @return self |
||
79 | */ |
||
80 | public function expireNotifications(array $notifications = []) |
||
88 | |||
89 | /** |
||
90 | * Expire displayed notification messages. |
||
91 | * |
||
92 | * @return self |
||
93 | */ |
||
94 | public function expireDisplayedNotifications() |
||
105 | |||
106 | /** |
||
107 | * Expire all notification messages. |
||
108 | * |
||
109 | * @return self |
||
110 | */ |
||
111 | public function expireAllNotifications() |
||
117 | |||
118 | /** |
||
119 | * Fetch notifications from Session. |
||
120 | * |
||
121 | * @param array $notifications |
||
122 | * |
||
123 | * @return self |
||
124 | */ |
||
125 | View Code Duplication | public function fetchNotifications(array $notifications = []) |
|
141 | |||
142 | /** |
||
143 | * Store notifications in Session. |
||
144 | * |
||
145 | * @param array $notifications |
||
146 | * |
||
147 | * @return self |
||
148 | */ |
||
149 | View Code Duplication | public function storeNotifications(array $notifications = []) |
|
165 | |||
166 | /** |
||
167 | * Convert an array of Notification objects to an array of arrays. |
||
168 | * |
||
169 | * @param array $notifications |
||
170 | * |
||
171 | * @return array |
||
172 | */ |
||
173 | public function toArray(array $notifications = []) |
||
183 | |||
184 | } |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.