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 |
||
15 | abstract class AbstractQuery |
||
16 | { |
||
17 | /** |
||
18 | * Create query object from array. |
||
19 | * |
||
20 | * @param array $array |
||
21 | * |
||
22 | * @return static |
||
23 | */ |
||
24 | 2 | public static function fromArray(array $array = []) |
|
33 | |||
34 | /** |
||
35 | * Create query object from string. |
||
36 | * |
||
37 | * @param string $string |
||
38 | * |
||
39 | * @return static |
||
40 | */ |
||
41 | 2 | public static function fromString($string) |
|
48 | |||
49 | /** |
||
50 | * Set a query argument. |
||
51 | * |
||
52 | * @param string $argument |
||
53 | * @param mixed $value |
||
54 | */ |
||
55 | 4 | View Code Duplication | public function __set($argument, $value) |
62 | |||
63 | /** |
||
64 | * Get a query argument. |
||
65 | * |
||
66 | * @param string $argument |
||
67 | * |
||
68 | * @return mixed |
||
69 | */ |
||
70 | 8 | View Code Duplication | public function __get($argument) |
79 | |||
80 | /** |
||
81 | * Test if a query argument is NULL. |
||
82 | * |
||
83 | * @param string $argument |
||
84 | * |
||
85 | * @return bool |
||
86 | */ |
||
87 | 4 | public function __isset($argument) |
|
96 | |||
97 | /** |
||
98 | * Set a query argument to NULL. |
||
99 | * |
||
100 | * @param string $argument |
||
101 | */ |
||
102 | 2 | public function __unset($argument) |
|
106 | |||
107 | /** |
||
108 | * Return string representation. |
||
109 | * |
||
110 | * @return string |
||
111 | */ |
||
112 | 4 | public function __toString() |
|
116 | |||
117 | /** |
||
118 | * Convert query into a string representation. |
||
119 | * |
||
120 | * @return string |
||
121 | */ |
||
122 | 4 | public function toString() |
|
126 | |||
127 | /** |
||
128 | * Convert query into a array representation. |
||
129 | * |
||
130 | * @return array |
||
131 | */ |
||
132 | 4 | public function toArray() |
|
147 | } |
||
148 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.