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 |
||
31 | class RemoteUser implements IUser { |
||
32 | |||
33 | /** @var string */ |
||
34 | private $userId; |
||
35 | |||
36 | /** |
||
37 | * RemoteUser constructor. |
||
38 | * |
||
39 | * @param string $userId |
||
40 | */ |
||
41 | public function __construct($userId) { |
||
44 | |||
45 | /** |
||
46 | * @inheritdoc |
||
47 | */ |
||
48 | public function getUID() { |
||
51 | |||
52 | /** |
||
53 | * @inheritdoc |
||
54 | */ |
||
55 | public function getDisplayName() { |
||
58 | |||
59 | /** |
||
60 | * @inheritdoc |
||
61 | */ |
||
62 | public function setDisplayName($displayName) { |
||
65 | |||
66 | /** |
||
67 | * @inheritdoc |
||
68 | */ |
||
69 | public function getLastLogin() { |
||
72 | |||
73 | /** |
||
74 | * @inheritdoc |
||
75 | */ |
||
76 | public function updateLastLoginTimestamp() { |
||
78 | |||
79 | /** |
||
80 | * @inheritdoc |
||
81 | */ |
||
82 | public function delete() { |
||
85 | |||
86 | /** |
||
87 | * @inheritdoc |
||
88 | */ |
||
89 | public function setPassword($password, $recoveryPassword = null) { |
||
92 | |||
93 | /** |
||
94 | * @inheritdoc |
||
95 | */ |
||
96 | public function getHome() { |
||
98 | |||
99 | /** |
||
100 | * @inheritdoc |
||
101 | */ |
||
102 | public function getBackendClassName() { |
||
105 | |||
106 | /** |
||
107 | * @inheritdoc |
||
108 | */ |
||
109 | public function canChangeAvatar() { |
||
112 | |||
113 | /** |
||
114 | * @inheritdoc |
||
115 | */ |
||
116 | public function canChangePassword() { |
||
119 | |||
120 | /** |
||
121 | * @inheritdoc |
||
122 | */ |
||
123 | public function canChangeDisplayName() { |
||
126 | |||
127 | /** |
||
128 | * @inheritdoc |
||
129 | */ |
||
130 | public function isEnabled() { |
||
133 | |||
134 | /** |
||
135 | * @inheritdoc |
||
136 | */ |
||
137 | public function setEnabled($enabled) { |
||
140 | |||
141 | /** |
||
142 | * @inheritdoc |
||
143 | */ |
||
144 | public function getEMailAddress() { |
||
147 | |||
148 | /** |
||
149 | * @inheritdoc |
||
150 | */ |
||
151 | public function getAvatarImage($size) { |
||
154 | |||
155 | /** |
||
156 | * @inheritdoc |
||
157 | */ |
||
158 | public function getCloudId() { |
||
163 | |||
164 | /** |
||
165 | * @param string $url |
||
166 | * @return string |
||
167 | */ |
||
168 | View Code Duplication | private function removeProtocolFromUrl($url) { |
|
177 | |||
178 | /** |
||
179 | * @inheritdoc |
||
180 | */ |
||
181 | public function setEMailAddress($mailAddress) { |
||
183 | |||
184 | /** |
||
185 | * @inheritdoc |
||
186 | */ |
||
187 | public function getQuota() { |
||
190 | |||
191 | /** |
||
192 | * @inheritdoc |
||
193 | */ |
||
194 | public function setQuota($quota) { |
||
196 | |||
197 | /** |
||
198 | * @inheritdoc |
||
199 | */ |
||
200 | public function getSearchTerms() { |
||
203 | |||
204 | /** |
||
205 | * @inheritdoc |
||
206 | */ |
||
207 | public function setSearchTerms(array $terms) { |
||
209 | |||
210 | /** |
||
211 | * @return integer |
||
212 | * @since 11.0.0 |
||
213 | */ |
||
214 | public function getAccountId() { |
||
217 | } |
||
218 |
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.