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 |
||
| 4 | abstract class Jetpack_Admin_Page { |
||
| 5 | // Add page specific actions given the page hook |
||
| 6 | abstract function add_page_actions( $hook ); |
||
| 7 | |||
| 8 | // Create a menu item for the page and returns the hook |
||
| 9 | abstract function get_page_hook(); |
||
| 10 | |||
| 11 | // Enqueue and localize page specific scripts |
||
| 12 | abstract function page_admin_scripts(); |
||
| 13 | |||
| 14 | // Render page specific HTML |
||
| 15 | abstract function page_render(); |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Function called after admin_styles to load any additional needed styles. |
||
| 19 | * |
||
| 20 | * @since 4.3 |
||
| 21 | */ |
||
| 22 | function additional_styles() {} |
||
| 23 | |||
| 24 | function __construct() { |
||
| 27 | |||
| 28 | function add_actions() { |
||
| 51 | |||
| 52 | function admin_head() { |
||
| 67 | |||
| 68 | // Render the page with a common top and bottom part, and page specific content |
||
| 69 | function render() { |
||
| 72 | |||
| 73 | function admin_help() { |
||
| 76 | |||
| 77 | function admin_page_load() { |
||
| 81 | |||
| 82 | function admin_page_top() { |
||
| 85 | |||
| 86 | function admin_page_bottom() { |
||
| 89 | |||
| 90 | // Add page specific scripts and jetpack stats for all menu pages |
||
| 91 | function admin_scripts() { |
||
| 95 | |||
| 96 | // Enqueue the Jetpack admin stylesheet |
||
| 97 | View Code Duplication | function admin_styles() { |
|
| 106 | |||
| 107 | function is_wp_version_too_old() { |
||
| 111 | } |
||
| 112 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: