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 |
||
8 | class SimpleHtmlDomBlank extends AbstractSimpleHtmlDom implements \IteratorAggregate, SimpleHtmlDomInterface |
||
9 | { |
||
10 | /** |
||
11 | * @var null |
||
12 | */ |
||
13 | protected $node; |
||
14 | |||
15 | /** |
||
16 | * Retrieve an external iterator. |
||
17 | * |
||
18 | * @see http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
19 | * |
||
20 | * @return SimpleHtmlDomNodeInterface |
||
21 | * <p> |
||
22 | * An instance of an object implementing <b>Iterator</b> or |
||
23 | * <b>Traversable</b> |
||
24 | * </p> |
||
25 | */ |
||
26 | 1 | public function getIterator(): SimpleHtmlDomNodeInterface |
|
30 | |||
31 | /** |
||
32 | * Returns children of node. |
||
33 | * |
||
34 | * @param int $idx |
||
35 | * |
||
36 | * @return null |
||
37 | */ |
||
38 | public function childNodes(int $idx = -1) |
||
42 | |||
43 | /** |
||
44 | * Find list of nodes with a CSS selector. |
||
45 | * |
||
46 | * @param string $selector |
||
47 | * @param int|null $idx |
||
48 | * |
||
49 | * @return SimpleHtmlDomNodeInterface |
||
50 | */ |
||
51 | public function find(string $selector, $idx = null) |
||
55 | |||
56 | /** |
||
57 | * Find one node with a CSS selector. |
||
58 | * |
||
59 | * @param string $selector |
||
60 | * |
||
61 | * @return SimpleHtmlDomInterface |
||
62 | */ |
||
63 | public function findOne(string $selector): SimpleHtmlDomInterface |
||
67 | |||
68 | /** |
||
69 | * Find nodes with a CSS selector. |
||
70 | * |
||
71 | * @param string $selector |
||
72 | * |
||
73 | * @return SimpleHtmlDomNodeInterface |
||
74 | */ |
||
75 | public function findMulti(string $selector): SimpleHtmlDomNodeInterface |
||
79 | |||
80 | /** |
||
81 | * Returns the first child of node. |
||
82 | * |
||
83 | * @return null |
||
84 | */ |
||
85 | public function firstChild() |
||
89 | |||
90 | /** |
||
91 | * Returns an array of attributes. |
||
92 | * |
||
93 | * @return null |
||
94 | */ |
||
95 | public function getAllAttributes() |
||
99 | |||
100 | /** |
||
101 | * Return attribute value. |
||
102 | * |
||
103 | * @param string $name |
||
104 | * |
||
105 | * @return string |
||
106 | */ |
||
107 | 2 | public function getAttribute(string $name): string |
|
111 | |||
112 | /** |
||
113 | * Return element by #id. |
||
114 | * |
||
115 | * @param string $id |
||
116 | * |
||
117 | * @return SimpleHtmlDomInterface |
||
118 | */ |
||
119 | public function getElementById(string $id): SimpleHtmlDomInterface |
||
123 | |||
124 | /** |
||
125 | * Returns elements by #id. |
||
126 | * |
||
127 | * @param string $id |
||
128 | * @param int|null $idx |
||
129 | * |
||
130 | * @return SimpleHtmlDomNodeInterface |
||
131 | */ |
||
132 | public function getElementsById(string $id, $idx = null) |
||
136 | |||
137 | /** |
||
138 | * Return elements by .class. |
||
139 | * |
||
140 | * @param string $class |
||
141 | * |
||
142 | * @return SimpleHtmlDomNodeInterface |
||
143 | */ |
||
144 | public function getElementByClass(string $class): SimpleHtmlDomNodeInterface |
||
148 | |||
149 | /** |
||
150 | * Return element by tag name. |
||
151 | * |
||
152 | * @param string $name |
||
153 | * |
||
154 | * @return SimpleHtmlDomInterface |
||
155 | */ |
||
156 | public function getElementByTagName(string $name): SimpleHtmlDomInterface |
||
160 | |||
161 | /** |
||
162 | * Returns elements by tag name. |
||
163 | * |
||
164 | * @param string $name |
||
165 | * @param int|null $idx |
||
166 | * |
||
167 | * @return SimpleHtmlDomNodeInterface |
||
168 | */ |
||
169 | public function getElementsByTagName(string $name, $idx = null) |
||
173 | |||
174 | /** |
||
175 | * Create a new "HtmlDomParser"-object from the current context. |
||
176 | * |
||
177 | * @return HtmlDomParser |
||
178 | */ |
||
179 | public function getHtmlDomParser(): HtmlDomParser |
||
183 | |||
184 | /** |
||
185 | * @return \DOMNode |
||
186 | */ |
||
187 | public function getNode(): \DOMNode |
||
191 | |||
192 | /** |
||
193 | * Determine if an attribute exists on the element. |
||
194 | * |
||
195 | * @param string $name |
||
196 | * |
||
197 | * @return bool |
||
198 | */ |
||
199 | public function hasAttribute(string $name): bool |
||
203 | |||
204 | /** |
||
205 | * Get dom node's outer html. |
||
206 | * |
||
207 | * @param bool $multiDecodeNewHtmlEntity |
||
208 | * |
||
209 | * @return string |
||
210 | */ |
||
211 | 1 | public function html(bool $multiDecodeNewHtmlEntity = false): string |
|
215 | |||
216 | /** |
||
217 | * Get dom node's inner html. |
||
218 | * |
||
219 | * @param bool $multiDecodeNewHtmlEntity |
||
220 | * |
||
221 | * @return string |
||
222 | */ |
||
223 | 1 | public function innerHtml(bool $multiDecodeNewHtmlEntity = false): string |
|
224 | { |
||
225 | 1 | return ''; |
|
226 | } |
||
227 | |||
228 | /** |
||
229 | * Returns the last child of node. |
||
230 | * |
||
231 | * @return null |
||
232 | */ |
||
233 | public function lastChild() |
||
237 | |||
238 | /** |
||
239 | * Returns the next sibling of node. |
||
240 | * |
||
241 | * @return null |
||
242 | */ |
||
243 | public function nextSibling() |
||
247 | |||
248 | /** |
||
249 | * Returns the parent of node. |
||
250 | * |
||
251 | * @return SimpleHtmlDomInterface |
||
252 | */ |
||
253 | public function parentNode(): SimpleHtmlDomInterface |
||
257 | |||
258 | /** |
||
259 | * Nodes can get partially destroyed in which they're still an |
||
260 | * actual DOM node (such as \DOMElement) but almost their entire |
||
261 | * body is gone, including the `nodeType` attribute. |
||
262 | * |
||
263 | * @return bool true if node has been destroyed |
||
264 | */ |
||
265 | public function isRemoved(): bool |
||
269 | |||
270 | /** |
||
271 | * Returns the previous sibling of node. |
||
272 | * |
||
273 | * @return null |
||
274 | */ |
||
275 | public function previousSibling() |
||
279 | |||
280 | /** |
||
281 | * Set attribute value. |
||
282 | * |
||
283 | * @param string $name <p>The name of the html-attribute.</p> |
||
284 | * @param string|null $value <p>Set to NULL or empty string, to remove the attribute.</p> |
||
285 | * @param bool $strict </p> |
||
286 | * $value must be NULL, to remove the attribute, |
||
287 | * so that you can set an empty string as attribute-value e.g. autofocus="" |
||
288 | * </p> |
||
289 | * |
||
290 | * @return SimpleHtmlDomInterface |
||
291 | */ |
||
292 | 1 | public function setAttribute(string $name, $value = null, bool $strict = false): SimpleHtmlDomInterface |
|
296 | |||
297 | /** |
||
298 | * @param string|string[]|null $value <p> |
||
299 | * null === get the current input value |
||
300 | * text === set a new input value |
||
301 | * </p> |
||
302 | * |
||
303 | * @return string|string[]|null |
||
304 | */ |
||
305 | public function val($value = null) |
||
309 | |||
310 | /** |
||
311 | * Remove attribute. |
||
312 | * |
||
313 | * @param string $name <p>The name of the html-attribute.</p> |
||
314 | * |
||
315 | * @return SimpleHtmlDomInterface |
||
316 | */ |
||
317 | public function removeAttribute(string $name): SimpleHtmlDomInterface |
||
321 | |||
322 | /** |
||
323 | * Get dom node's plain text. |
||
324 | * |
||
325 | * @return string |
||
326 | */ |
||
327 | 3 | public function text(): string |
|
331 | |||
332 | /** |
||
333 | * @param string $name |
||
334 | * @param array $arguments |
||
335 | * |
||
336 | * @throws \BadMethodCallException |
||
337 | * |
||
338 | * @return SimpleHtmlDomInterface|string|null |
||
339 | */ |
||
340 | 1 | View Code Duplication | public function __call($name, $arguments) |
341 | { |
||
342 | 1 | $name = \strtolower($name); |
|
343 | |||
344 | 1 | if (isset(self::$functionAliases[$name])) { |
|
345 | 1 | return \call_user_func_array([$this, self::$functionAliases[$name]], $arguments); |
|
346 | } |
||
347 | |||
348 | throw new \BadMethodCallException('Method does not exist'); |
||
349 | } |
||
350 | |||
351 | protected function replaceNodeWithString(string $string): SimpleHtmlDomInterface |
||
355 | |||
356 | 2 | protected function replaceChildWithString(string $string): SimpleHtmlDomInterface |
|
360 | |||
361 | protected function replaceTextWithString($string): SimpleHtmlDomInterface |
||
365 | } |
||
366 |
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.