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 |
||
| 9 | class Services { |
||
| 10 | |||
| 11 | /** |
||
| 12 | * Store the current instance |
||
| 13 | * |
||
| 14 | * @access private |
||
| 15 | * @var object Services |
||
| 16 | * @static |
||
| 17 | */ |
||
| 18 | private static $instance; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * The array of services |
||
| 22 | * |
||
| 23 | * Should be of the format array( __FILE__ => __CLASS__ ); |
||
| 24 | * |
||
| 25 | * @access private |
||
| 26 | * @var array |
||
| 27 | * @static |
||
| 28 | */ |
||
| 29 | private $services = array(); |
||
|
|
|||
| 30 | |||
| 31 | /** |
||
| 32 | * The current schedule object |
||
| 33 | * |
||
| 34 | * @access private |
||
| 35 | * @var object Scheduled_Backup |
||
| 36 | */ |
||
| 37 | private $schedule; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Get the current instance |
||
| 41 | * |
||
| 42 | * @static |
||
| 43 | */ |
||
| 44 | public static function instance() { |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Get the array of registered services |
||
| 55 | * |
||
| 56 | * @param Scheduled_Backup $schedule |
||
| 57 | * @return Service[] |
||
| 58 | */ |
||
| 59 | public static function get_services( Scheduled_Backup $schedule = null ) { |
||
| 70 | |||
| 71 | public function get_available_services() { |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Register a new service |
||
| 77 | * |
||
| 78 | * @param $filepath |
||
| 79 | * @param $classname |
||
| 80 | * @return bool|WP_Error |
||
| 81 | */ |
||
| 82 | View Code Duplication | public static function register( $filepath, $classname ) { |
|
| 91 | |||
| 92 | /** |
||
| 93 | * De-register an existing service |
||
| 94 | * @param string $filepath |
||
| 95 | * @return bool|WP_Error |
||
| 96 | */ |
||
| 97 | View Code Duplication | public static function unregister( $filepath ) { |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Instantiate the individual service classes |
||
| 110 | * |
||
| 111 | * @param string $classname |
||
| 112 | * |
||
| 113 | * @return array An array of instantiated classes |
||
| 114 | */ |
||
| 115 | View Code Duplication | private static function instantiate( $classname ) { |
|
| 129 | |||
| 130 | } |
||
| 131 |
This check marks private properties in classes that are never used. Those properties can be removed.