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 |
||
| 10 | class WP_Update_Php { |
||
|
|
|||
| 11 | /** |
||
| 12 | * @access private |
||
| 13 | * @var string |
||
| 14 | */ |
||
| 15 | private $plugin_name; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @access private |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | private $textdomain; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @access private |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | private $minimum_version; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @access private |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | private $recommended_version; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @access private |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | private $wpupdatephp_site = 'http://www.wpupdatephp.com/update/'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Constructor |
||
| 43 | * |
||
| 44 | * @access public |
||
| 45 | * @param array $plugin Plugin Name and Text Domain |
||
| 46 | * @param array $requirements Minimum and Recommended version of PHP. |
||
| 47 | */ |
||
| 48 | public function __construct($plugin = array(), $requirements = array()) { |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Check given PHP version against minimum required version. |
||
| 76 | * |
||
| 77 | * @access public |
||
| 78 | * @param string $version Optional. PHP version to check against. |
||
| 79 | * Default is the current PHP version as a string in |
||
| 80 | * "major.minor.release[extra]" notation. |
||
| 81 | * @return bool True if supplied PHP version meets minimum required version. |
||
| 82 | */ |
||
| 83 | View Code Duplication | public function does_it_meet_required_php_version($version = PHP_VERSION) { |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Check given PHP version against recommended version. |
||
| 94 | * |
||
| 95 | * @access public |
||
| 96 | * @param string $version Optional. PHP version to check against. |
||
| 97 | * Default is the current PHP version as a string in |
||
| 98 | * "major.minor.release[extra]" notation. |
||
| 99 | * @return bool True if supplied PHP version meets recommended version. |
||
| 100 | */ |
||
| 101 | View Code Duplication | public function does_it_meet_recommended_php_version($version = PHP_VERSION) { |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Check that one PHP version is less than or equal to another. |
||
| 112 | * |
||
| 113 | * @access private |
||
| 114 | * @param string $recommended The baseline version of PHP. |
||
| 115 | * @param string $version The given version of PHP. |
||
| 116 | * @return bool True if the requirement is less than or equal to given version. |
||
| 117 | */ |
||
| 118 | private function version_passes_requirement($recommended, $version) { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Conditionally hook in an admin notice. |
||
| 124 | * |
||
| 125 | * @access private |
||
| 126 | * @param callable $callback Callable that displays admin notice. |
||
| 127 | */ |
||
| 128 | private function load_version_notice($callback) { |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Return the string to be shown in the admin notice. |
||
| 137 | * |
||
| 138 | * This is based on the level (`recommended` or default `minimum`) of the |
||
| 139 | * notice. This will also add the plugin name to the notice string, if set. |
||
| 140 | * |
||
| 141 | * @access public |
||
| 142 | * @param string $level Optional. Admin notice level, `recommended` or `minimum`. |
||
| 143 | * Default is `minimum`. |
||
| 144 | * @return string |
||
| 145 | */ |
||
| 146 | public function get_admin_notice($level = 'minimum') { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Method hooked into admin_notices when minimum required PHP version is not |
||
| 170 | * available to show this in a notice. |
||
| 171 | * |
||
| 172 | * @access public |
||
| 173 | * @hook admin_notices |
||
| 174 | */ |
||
| 175 | public function minimum_admin_notice() { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Method hooked into admin_notices when recommended PHP version is not |
||
| 181 | * available to show this in a notice. |
||
| 182 | * |
||
| 183 | * @access public |
||
| 184 | * @hook admin_notices |
||
| 185 | */ |
||
| 186 | public function recommended_admin_notice() { |
||
| 189 | |||
| 190 | } // END Class |
||
| 191 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.