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() { |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Enqueue assets needed by the quiz, |
||
| 80 | * |
||
| 81 | * @since 4.5.0 |
||
| 82 | */ |
||
| 83 | public static function enqueue_scripts() { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Enqueue editor assets like JS and CSS. |
||
| 92 | * |
||
| 93 | * @since 5.4 |
||
| 94 | */ |
||
| 95 | function block_editor_assets() { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Check if this is a feed and thus JS is unavailable. |
||
| 106 | * |
||
| 107 | * @since 4.5.0 |
||
| 108 | * |
||
| 109 | * @return bool|null |
||
| 110 | */ |
||
| 111 | private static function is_javascript_unavailable() { |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Display message when JS is not available. |
||
| 125 | * |
||
| 126 | * @since 4.5.0 |
||
| 127 | * |
||
| 128 | * @return string |
||
| 129 | */ |
||
| 130 | private static function noscript_info() { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Check if we're in WordPress.com. |
||
| 140 | * |
||
| 141 | * @since 4.5.0 |
||
| 142 | * |
||
| 143 | * @return bool |
||
| 144 | */ |
||
| 145 | public static function is_wpcom() { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Parse shortcode arguments and render its output. |
||
| 151 | * |
||
| 152 | * @since 4.5.0 |
||
| 153 | * |
||
| 154 | * @param array $atts Shortcode parameters. |
||
| 155 | * @param string $content Content enclosed by shortcode tags. |
||
| 156 | * |
||
| 157 | * @return string |
||
| 158 | */ |
||
| 159 | public static function shortcode( $atts, $content = null ) { |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Strip line breaks, restrict allowed HTML to a few whitelisted tags and execute nested shortcodes. |
||
| 208 | * |
||
| 209 | * @since 4.5.0 |
||
| 210 | * |
||
| 211 | * @param string $content |
||
| 212 | * |
||
| 213 | * @return mixed|string |
||
| 214 | */ |
||
| 215 | private static function do_shortcode( $content ) { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Render question. |
||
| 236 | * |
||
| 237 | * @since 4.5.0 |
||
| 238 | * |
||
| 239 | * @param array $atts |
||
| 240 | * @param null $content |
||
| 241 | * |
||
| 242 | * @return string |
||
| 243 | */ |
||
| 244 | public static function question_shortcode( $atts, $content = null ) { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Render correct answer. |
||
| 252 | * |
||
| 253 | * @since 4.5.0 |
||
| 254 | * |
||
| 255 | * @param array $atts |
||
| 256 | * @param null $content |
||
| 257 | * |
||
| 258 | * @return string |
||
| 259 | */ |
||
| 260 | View Code Duplication | public static function answer_shortcode( $atts, $content = null ) { |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Render wrong response. |
||
| 272 | * |
||
| 273 | * @since 4.5.0 |
||
| 274 | * |
||
| 275 | * @param array $atts |
||
| 276 | * @param null $content |
||
| 277 | * |
||
| 278 | * @return string |
||
| 279 | */ |
||
| 280 | View Code Duplication | public static function wrong_shortcode( $atts, $content = null ) { |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Render explanation for wrong or right answer. |
||
| 292 | * |
||
| 293 | * @since 4.5.0 |
||
| 294 | * |
||
| 295 | * @param array $atts |
||
| 296 | * @param null $content |
||
| 297 | * |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | View Code Duplication | public static function explanation_shortcode( $atts, $content = null ) { |
|
| 309 | } |
||
| 310 | |||
| 312 |
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..