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 Base_Admin_Menu { |
||
| 16 | /** |
||
| 17 | * Holds class instances. |
||
| 18 | * |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | protected static $instances; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Whether the current request is a REST API request. |
||
| 25 | * |
||
| 26 | * @var bool |
||
| 27 | */ |
||
| 28 | protected $is_api_request = false; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Domain of the current site. |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $domain; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Base_Admin_Menu constructor. |
||
| 39 | */ |
||
| 40 | protected function __construct() { |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Determine if the current request is from API |
||
| 51 | */ |
||
| 52 | public function set_is_api_request() { |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Returns class instance. |
||
| 61 | * |
||
| 62 | * @return Admin_Menu |
||
| 63 | */ |
||
| 64 | public static function get_instance() { |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Sets up class properties for REST API requests. |
||
| 76 | * |
||
| 77 | * @param \WP_REST_Response $response Response from the endpoint. |
||
| 78 | */ |
||
| 79 | public function rest_api_init( $response ) { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Updates the menu data of the given menu slug. |
||
| 87 | * |
||
| 88 | * @param string $slug Slug of the menu to update. |
||
| 89 | * @param string $url New menu URL. |
||
|
|
|||
| 90 | * @param string $title New menu title. |
||
| 91 | * @param string $cap New menu capability. |
||
| 92 | * @param string $icon New menu icon. |
||
| 93 | * @param int $position New menu position. |
||
| 94 | * @return bool Whether the menu has been updated. |
||
| 95 | */ |
||
| 96 | public function update_menu( $slug, $url = null, $title = null, $cap = null, $icon = null, $position = null ) { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Updates the submenus of the given menu slug. |
||
| 154 | * |
||
| 155 | * @param string $slug Menu slug. |
||
| 156 | * @param array $submenus_to_update Array of new submenu slugs. |
||
| 157 | */ |
||
| 158 | public function update_submenus( $slug, $submenus_to_update ) { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Adds a menu separator. |
||
| 176 | * |
||
| 177 | * @param int $position The position in the menu order this item should appear. |
||
| 178 | * @param string $cap Optional. The capability required for this menu to be displayed to the user. |
||
| 179 | * Default: 'read'. |
||
| 180 | */ |
||
| 181 | public function add_admin_menu_separator( $position, $cap = 'read' ) { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Enqueues scripts and styles. |
||
| 197 | */ |
||
| 198 | public function enqueue_scripts() { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Remove submenu items from given menu slug. |
||
| 222 | * |
||
| 223 | * @param string $slug Menu slug. |
||
| 224 | */ |
||
| 225 | public function remove_submenus( $slug ) { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Whether to use wp-admin pages rather than Calypso. |
||
| 236 | * |
||
| 237 | * Options: |
||
| 238 | * false - Calypso (Default). |
||
| 239 | * true - wp-admin. |
||
| 240 | * |
||
| 241 | * @return bool |
||
| 242 | */ |
||
| 243 | abstract public function should_link_to_wp_admin(); |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Create the desired menu output. |
||
| 247 | */ |
||
| 248 | abstract public function reregister_menu_items(); |
||
| 249 | } |
||
| 250 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.