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 |
||
| 19 | class Gamajo_Dashboard_Glancer { |
||
| 20 | /** |
||
| 21 | * Hold all of the items to show. |
||
| 22 | * |
||
| 23 | * @since 1.0.0 |
||
| 24 | * |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | protected $items; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Automatically show any registered items. |
||
| 31 | * |
||
| 32 | * With this, there's no need to explicitly call show() during the |
||
| 33 | * `dashboard_glance_items` hook, and items can be registered at any time |
||
| 34 | * before `dashboard_glance_items` priority 20 (including on earlier hooks). |
||
| 35 | * |
||
| 36 | * @since 1.0.0 |
||
| 37 | */ |
||
| 38 | public function __construct() { |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Register one or more post type items to be shown on the dashboard widget. |
||
| 45 | * |
||
| 46 | * @since 1.0.0 |
||
| 47 | * |
||
| 48 | * @param array|string $post_types Post type name, or array of post type names. |
||
| 49 | * @param array|string $statuses Optional. Post status or array of |
||
| 50 | * different post type statuses. Default is |
||
| 51 | * `publish`. |
||
| 52 | * @return null Return early if action hook has already passed, or no valid |
||
| 53 | * post types were given. |
||
| 54 | */ |
||
| 55 | public function add( $post_types, $statuses = 'publish' ) { |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Show the items on the dashboard widget. |
||
| 83 | * |
||
| 84 | * @since 1.0.0 |
||
| 85 | */ |
||
| 86 | public function show() { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Check one or more post types to see if they are valid. |
||
| 96 | * |
||
| 97 | * @since 1.0.0 |
||
| 98 | * |
||
| 99 | * @param array $post_types Each of the post types to check. |
||
| 100 | * @return array List of the given post types that are valid. |
||
| 101 | */ |
||
| 102 | protected function unset_invalid_post_types( array $post_types ) { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Build and return the data and markup for a single item. |
||
| 115 | * |
||
| 116 | * If the item count is zero, return an empty string, to avoid visual clutter. |
||
| 117 | * |
||
| 118 | * @since 1.0.0 |
||
| 119 | * |
||
| 120 | * @param array $item Registered item. |
||
| 121 | * @return string Markup, or empty string if item count is zero. |
||
| 122 | */ |
||
| 123 | View Code Duplication | protected function get_single_item( array $item ) { |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Get the singular or plural label for an item. |
||
| 140 | * |
||
| 141 | * @since 1.0.0 |
||
| 142 | * |
||
| 143 | * @param array $item Registered item. |
||
| 144 | * @param int $count Number of items present in WP. |
||
| 145 | * @return string |
||
| 146 | */ |
||
| 147 | protected function get_label( array $item, $count ) { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Build the URL that linked items use. |
||
| 162 | * |
||
| 163 | * @since 1.0.0 |
||
| 164 | * |
||
| 165 | * @param array $item Registered item. |
||
| 166 | * @return string Admin URL to view the entries of the given post type with the given status |
||
| 167 | */ |
||
| 168 | public function get_link_url( array $item ) { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Wrap a glance item in a link, if the current user can edit posts. |
||
| 174 | * |
||
| 175 | * @since 1.0.0 |
||
| 176 | * |
||
| 177 | * @param string $text Text to potentially wrap in a link. |
||
| 178 | * @param string $href Link target. |
||
| 179 | * @return string Text wrapped in a link if current user can edit posts, or original text otherwise. |
||
| 180 | */ |
||
| 181 | protected function maybe_link( $text, $href ) { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Wrap number and text within list item markup. |
||
| 191 | * |
||
| 192 | * The extra work for populating classes is to provide dashicons support. |
||
| 193 | * |
||
| 194 | * @since 1.0.0 |
||
| 195 | * |
||
| 196 | * @param string $text Text to display. May be wrapped in a link. |
||
| 197 | * @param string $post_type Post type. |
||
| 198 | * @return string Markup for list item. |
||
| 199 | */ |
||
| 200 | protected function get_markup( $text, $post_type ) { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Add post types icon styling to Dashboard page. |
||
| 218 | * |
||
| 219 | * To override the overly-specific core styles, we apply styles to ignore |
||
| 220 | * the default circle, and with the classes added to the list item (not |
||
| 221 | * anchor), get dashicons showing there instead. |
||
| 222 | * |
||
| 223 | * @since 1.1.0 |
||
| 224 | * |
||
| 225 | * @return null Return early if style already added, or the Dashboard page. |
||
| 226 | */ |
||
| 227 | public function dashboard_css() { |
||
| 245 | } |
||
| 246 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.