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 |
||
| 28 | class Carbon_Fields { |
||
|
|
|||
| 29 | |||
| 30 | /** |
||
| 31 | * An event emitter to facilitate events before the WordPress environment is guaranteed to be loaded |
||
| 32 | * |
||
| 33 | * @var Emitter |
||
| 34 | */ |
||
| 35 | protected $emitter; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Flag if Carbon Fields has been booted |
||
| 39 | * |
||
| 40 | * @var bool |
||
| 41 | */ |
||
| 42 | public $booted = false; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Inversion of Control container instance |
||
| 46 | * |
||
| 47 | * @var PimpleContainer |
||
| 48 | */ |
||
| 49 | public $ioc = null; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Singleton implementation |
||
| 53 | * |
||
| 54 | * @return Carbon_Fields\Carbon_Fields |
||
| 55 | */ |
||
| 56 | public static function instance() { |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Constructor |
||
| 66 | */ |
||
| 67 | private function __construct() { |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Resolve a dependency through IoC |
||
| 73 | * |
||
| 74 | * @param string $key |
||
| 75 | * @param string $subcontainer Subcontainer to look into |
||
| 76 | * @return mixed |
||
| 77 | */ |
||
| 78 | View Code Duplication | public static function resolve( $key, $subcontainer = null ) { |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Resolve a dependency through IoC with arguments |
||
| 91 | * |
||
| 92 | * @param string $identifier Key to resolve from the container |
||
| 93 | * @param array $arguments Key-value array of arguments |
||
| 94 | * @param string $subcontainer The container to resolve from |
||
| 95 | * @return mixed |
||
| 96 | */ |
||
| 97 | public static function resolve_with_arguments( $identifier, $arguments, $subcontainer = null ) { |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Resolve a service through IoC |
||
| 108 | * |
||
| 109 | * @param string $service_name |
||
| 110 | * @return mixed |
||
| 111 | */ |
||
| 112 | public static function service( $service_name ) { |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Check if a dependency is registered |
||
| 118 | * |
||
| 119 | * @param string $key |
||
| 120 | * @param string $subcontainer Subcontainer to look into |
||
| 121 | * @return bool |
||
| 122 | */ |
||
| 123 | View Code Duplication | public static function has( $key, $subcontainer = null ) { |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Extend Carbon Fields by adding a new entity (container condition etc.) |
||
| 136 | * |
||
| 137 | * @param string $type Type of extension - 'container_condition' |
||
| 138 | * @param string $class Extension class name |
||
| 139 | * @param string $extender Extending callable |
||
| 140 | */ |
||
| 141 | public static function extend( $class, $extender ) { |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Replace the ioc container for Carbon_Fields\Carbon_Fields |
||
| 168 | * |
||
| 169 | * @param PimpleContainer $ioc |
||
| 170 | */ |
||
| 171 | public function install( PimpleContainer $ioc ) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Boot Carbon Fields with default IoC dependencies |
||
| 177 | */ |
||
| 178 | public static function boot() { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Check if Carbon Fields has booted |
||
| 195 | */ |
||
| 196 | public static function is_booted() { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Throw exception if Carbon Fields has not been booted |
||
| 202 | */ |
||
| 203 | public static function verify_boot() { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Get the event emitter |
||
| 211 | * |
||
| 212 | * @return Emitter |
||
| 213 | */ |
||
| 214 | public function get_emitter() { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Add a listener to an event |
||
| 223 | * |
||
| 224 | * @param string $event |
||
| 225 | * @return Listener $listener |
||
| 226 | */ |
||
| 227 | public static function add_listener( $event, $listener ) { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Remove a listener from any event |
||
| 233 | * |
||
| 234 | * @param Listener $removed_listener |
||
| 235 | */ |
||
| 236 | public static function remove_listener( $listener ) { |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Add a persistent listener to an event |
||
| 242 | * |
||
| 243 | * @param string $event The event to listen for |
||
| 244 | * @param string $callable The callable to call when the event is broadcasted |
||
| 245 | * @return Listener |
||
| 246 | */ |
||
| 247 | public static function on( $event, $callable ) { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Add a one-time listener to an event |
||
| 253 | * |
||
| 254 | * @param string $event The event to listen for |
||
| 255 | * @param string $callable The callable to call when the event is broadcasted |
||
| 256 | * @return Listener |
||
| 257 | */ |
||
| 258 | public static function once( $event, $callable ) { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Get default IoC container dependencies |
||
| 264 | * |
||
| 265 | * @return PimpleContainer |
||
| 266 | */ |
||
| 267 | protected static function get_default_ioc() { |
||
| 336 | } |