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:
Complex classes like Jetpack_Recipes often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Jetpack_Recipes, and based on these observations, apply Extract Interface, too.
| 1 | <?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
||
| 20 | class Jetpack_Recipes { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Have scripts and styles been enqueued already. |
||
| 24 | * |
||
| 25 | * @var bool |
||
| 26 | */ |
||
| 27 | private $scripts_and_style_included = false; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Constructor |
||
| 31 | */ |
||
| 32 | public function __construct() { |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Add Schema-specific attributes to our allowed tags in wp_kses, |
||
| 40 | * so we can have better Schema.org compliance. |
||
| 41 | * |
||
| 42 | * @param array $allowedtags Array of allowed HTML tags in recipes. |
||
| 43 | * @param array $context Context to judge allowed tags by. |
||
| 44 | */ |
||
| 45 | public function add_recipes_kses_rules( $allowedtags, $context ) { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Function to add a new property rule to our kses array. |
||
| 78 | * Used by add_recipe_kses_rules() above. |
||
| 79 | * |
||
| 80 | * @param array $all_tags Array of allowed HTML tags in recipes. |
||
| 81 | * @param string $tag New HTML tag to add to the array of allowed HTML. |
||
| 82 | * @param array $rules Array of allowed attributes for that HTML tag. |
||
| 83 | */ |
||
| 84 | private function add_kses_rule( $all_tags, $tag, $rules ) { |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Register our shortcode and enqueue necessary files. |
||
| 99 | */ |
||
| 100 | public function action_init() { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Enqueue scripts and styles |
||
| 113 | */ |
||
| 114 | public function add_scripts() { |
||
| 115 | if ( empty( $GLOBALS['posts'] ) || ! is_array( $GLOBALS['posts'] ) ) { |
||
| 116 | return; |
||
| 117 | } |
||
| 118 | |||
| 119 | View Code Duplication | foreach ( $GLOBALS['posts'] as $p ) { |
|
| 120 | if ( has_shortcode( $p->post_content, 'recipe' ) ) { |
||
| 121 | $this->scripts_and_style_included = true; |
||
| 122 | break; |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | if ( ! $this->scripts_and_style_included ) { |
||
| 127 | return; |
||
| 128 | } |
||
| 129 | |||
| 130 | wp_enqueue_style( 'jetpack-recipes-style', plugins_url( '/css/recipes.css', __FILE__ ), array(), '20130919' ); |
||
| 131 | wp_style_add_data( 'jetpack-recipes-style', 'rtl', 'replace' ); |
||
| 132 | |||
| 133 | // add $themecolors-defined styles. |
||
| 134 | wp_add_inline_style( 'jetpack-recipes-style', self::themecolor_styles() ); |
||
| 135 | wp_enqueue_script( |
||
| 136 | 'jetpack-recipes-printthis', |
||
| 137 | Assets::get_file_url_for_environment( '_inc/build/shortcodes/js/recipes-printthis.min.js', 'modules/shortcodes/js/recipes-printthis.js' ), |
||
| 138 | array( 'jquery' ), |
||
| 139 | '20170202', |
||
| 140 | false |
||
| 141 | ); |
||
| 142 | |||
| 143 | wp_enqueue_script( |
||
| 144 | 'jetpack-recipes-js', |
||
| 145 | Assets::get_file_url_for_environment( '_inc/build/shortcodes/js/recipes.min.js', 'modules/shortcodes/js/recipes.js' ), |
||
| 146 | array( 'jquery', 'jetpack-recipes-printthis' ), |
||
| 147 | '20131230', |
||
| 148 | false |
||
| 149 | ); |
||
| 150 | |||
| 151 | $title_var = wp_title( '|', false, 'right' ); |
||
| 152 | $rtl = is_rtl() ? '-rtl' : ''; |
||
| 153 | $print_css_var = plugins_url( "/css/recipes-print{$rtl}.css", __FILE__ ); |
||
| 154 | |||
| 155 | wp_localize_script( |
||
| 156 | 'jetpack-recipes-js', |
||
| 157 | 'jetpack_recipes_vars', |
||
| 158 | array( |
||
| 159 | 'pageTitle' => $title_var, |
||
| 160 | 'loadCSS' => $print_css_var, |
||
| 161 | ) |
||
| 162 | ); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Our [recipe] shortcode. |
||
| 167 | * Prints recipe data styled to look good on *any* theme. |
||
| 168 | * |
||
| 169 | * @param array $atts Array of shortcode attributes. |
||
| 170 | * @param string $content Post content. |
||
| 171 | * |
||
| 172 | * @return string HTML for recipe shortcode. |
||
| 173 | */ |
||
| 174 | public static function recipe_shortcode( $atts, $content = '' ) { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * The recipe output |
||
| 196 | * |
||
| 197 | * @param array $atts Array of shortcode attributes. |
||
| 198 | * @param string $content Post content. |
||
| 199 | * |
||
| 200 | * @return string HTML output |
||
| 201 | */ |
||
| 202 | private static function recipe_shortcode_html( $atts, $content = '' ) { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Our [recipe-notes] shortcode. |
||
| 324 | * Outputs ingredients, styled in a div. |
||
| 325 | * |
||
| 326 | * @param array $atts Array of shortcode attributes. |
||
| 327 | * @param string $content Post content. |
||
| 328 | * |
||
| 329 | * @return string HTML for recipe notes shortcode. |
||
| 330 | */ |
||
| 331 | View Code Duplication | public static function recipe_notes_shortcode( $atts, $content = '' ) { |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Our [recipe-ingredients] shortcode. |
||
| 363 | * Outputs notes, styled in a div. |
||
| 364 | * |
||
| 365 | * @param array $atts Array of shortcode attributes. |
||
| 366 | * @param string $content Post content. |
||
| 367 | * |
||
| 368 | * @return string HTML for recipe ingredients shortcode. |
||
| 369 | */ |
||
| 370 | View Code Duplication | public static function recipe_ingredients_shortcode( $atts, $content = '' ) { |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Reusable function to check for shortened formatting. |
||
| 400 | * Basically, users can create lists with the following shorthand: |
||
| 401 | * - item one |
||
| 402 | * - item two |
||
| 403 | * - item three |
||
| 404 | * And we'll magically convert it to a list. This has the added benefit |
||
| 405 | * of including itemprops for the recipe schema. |
||
| 406 | * |
||
| 407 | * @param string $content HTML content. |
||
| 408 | * @param string $type Type of list. |
||
| 409 | * |
||
| 410 | * @return string content formatted as a list item |
||
| 411 | */ |
||
| 412 | private static function output_list_content( $content, $type ) { |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Our [recipe-directions] shortcode. |
||
| 482 | * Outputs directions, styled in a div. |
||
| 483 | * |
||
| 484 | * @param array $atts Array of shortcode attributes. |
||
| 485 | * @param string $content Post content. |
||
| 486 | * |
||
| 487 | * @return string HTML for recipe directions shortcode. |
||
| 488 | */ |
||
| 489 | View Code Duplication | public static function recipe_directions_shortcode( $atts, $content = '' ) { |
|
| 516 | |||
| 517 | /** |
||
| 518 | * Use $themecolors array to style the Recipes shortcode |
||
| 519 | * |
||
| 520 | * @print style block |
||
| 521 | * @return string $style |
||
| 522 | */ |
||
| 523 | public function themecolor_styles() { |
||
| 534 | |||
| 535 | } |
||
| 536 | |||
| 538 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.