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 | class Quiz_Shortcode { |
||
16 | |||
17 | /** |
||
18 | * Parameters admitted by [quiz] shortcode. |
||
19 | * |
||
20 | * @since 4.5.0 |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | private static $quiz_params = array(); |
||
25 | |||
26 | /** |
||
27 | * Whether the scripts were enqueued. |
||
28 | * |
||
29 | * @since 4.5.0 |
||
30 | * |
||
31 | * @var bool |
||
32 | */ |
||
33 | private static $scripts_enqueued = false; |
||
34 | |||
35 | /** |
||
36 | * In a8c training, store user currently logged in. |
||
37 | * |
||
38 | * @since 4.5.0 |
||
39 | * |
||
40 | * @var null |
||
41 | */ |
||
42 | private static $username = null; |
||
43 | |||
44 | /** |
||
45 | * Whether the noscript tag was already printed. |
||
46 | * |
||
47 | * @since 4.5.0 |
||
48 | * |
||
49 | * @var bool |
||
50 | */ |
||
51 | private static $noscript_info_printed = false; |
||
52 | |||
53 | /** |
||
54 | * Whether JavaScript is available. |
||
55 | * |
||
56 | * @since 4.5.0 |
||
57 | * |
||
58 | * @var null |
||
59 | */ |
||
60 | private static $javascript_unavailable = null; |
||
61 | |||
62 | /** |
||
63 | * Register all shortcodes. |
||
64 | * |
||
65 | * @since 4.5.0 |
||
66 | */ |
||
67 | public static function init() { |
||
74 | |||
75 | /** |
||
76 | * Enqueue assets needed by the quiz, |
||
77 | * |
||
78 | * @since 4.5.0 |
||
79 | */ |
||
80 | private static function enqueue_scripts() { |
||
84 | |||
85 | /** |
||
86 | * Check if this is a feed and thus JS is unavailable. |
||
87 | * |
||
88 | * @since 4.5.0 |
||
89 | * |
||
90 | * @return bool|null |
||
91 | */ |
||
92 | private static function is_javascript_unavailable() { |
||
103 | |||
104 | /** |
||
105 | * Display message when JS is not available. |
||
106 | * |
||
107 | * @since 4.5.0 |
||
108 | * |
||
109 | * @return string |
||
110 | */ |
||
111 | private static function noscript_info() { |
||
118 | |||
119 | /** |
||
120 | * Check if we're in WordPress.com. |
||
121 | * |
||
122 | * @since 4.5.0 |
||
123 | * |
||
124 | * @return bool |
||
125 | */ |
||
126 | public static function is_wpcom() { |
||
129 | |||
130 | /** |
||
131 | * Parse shortcode arguments and render its output. |
||
132 | * |
||
133 | * @since 4.5.0 |
||
134 | * |
||
135 | * @param array $atts Shortcode parameters. |
||
136 | * @param string $content Content enclosed by shortcode tags. |
||
137 | * |
||
138 | * @return string |
||
139 | */ |
||
140 | public static function shortcode( $atts, $content = null ) { |
||
186 | |||
187 | /** |
||
188 | * Strip line breaks, restrict allowed HTML to a few whitelisted tags and execute nested shortcodes. |
||
189 | * |
||
190 | * @since 4.5.0 |
||
191 | * |
||
192 | * @param string $content |
||
193 | * |
||
194 | * @return mixed|string |
||
195 | */ |
||
196 | private static function do_shortcode( $content ) { |
||
214 | |||
215 | /** |
||
216 | * Render question. |
||
217 | * |
||
218 | * @since 4.5.0 |
||
219 | * |
||
220 | * @param array $atts |
||
221 | * @param null $content |
||
222 | * |
||
223 | * @return string |
||
224 | */ |
||
225 | public static function question_shortcode( $atts, $content = null ) { |
||
230 | |||
231 | /** |
||
232 | * Render correct answer. |
||
233 | * |
||
234 | * @since 4.5.0 |
||
235 | * |
||
236 | * @param array $atts |
||
237 | * @param null $content |
||
238 | * |
||
239 | * @return string |
||
240 | */ |
||
241 | View Code Duplication | public static function answer_shortcode( $atts, $content = null ) { |
|
250 | |||
251 | /** |
||
252 | * Render wrong response. |
||
253 | * |
||
254 | * @since 4.5.0 |
||
255 | * |
||
256 | * @param array $atts |
||
257 | * @param null $content |
||
258 | * |
||
259 | * @return string |
||
260 | */ |
||
261 | View Code Duplication | public static function wrong_shortcode( $atts, $content = null ) { |
|
270 | |||
271 | /** |
||
272 | * Render explanation for wrong or right answer. |
||
273 | * |
||
274 | * @since 4.5.0 |
||
275 | * |
||
276 | * @param array $atts |
||
277 | * @param null $content |
||
278 | * |
||
279 | * @return string |
||
280 | */ |
||
281 | View Code Duplication | public static function explanation_shortcode( $atts, $content = null ) { |
|
290 | } |
||
291 | |||
293 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..