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 | final 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|null $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 ) { |
||
| 113 | return static::resolve( $service_name, 'services' ); |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Check if a dependency is registered |
||
| 118 | * |
||
| 119 | * @param string $key |
||
| 120 | * @param string|null $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 $class Extension class name |
||
| 138 | * @param string $extender Extending callable |
||
| 139 | */ |
||
| 140 | public static function extend( $class, $extender ) { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Replace the ioc container for Carbon_Fields\Carbon_Fields |
||
| 167 | * |
||
| 168 | * @param PimpleContainer $ioc |
||
| 169 | */ |
||
| 170 | public function install( PimpleContainer $ioc ) { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Boot Carbon Fields with default IoC dependencies |
||
| 176 | */ |
||
| 177 | public static function boot() { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Check if Carbon Fields has booted |
||
| 194 | */ |
||
| 195 | public static function is_booted() { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Throw exception if Carbon Fields has not been booted |
||
| 201 | */ |
||
| 202 | public static function verify_boot() { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Resolve the public url of a directory inside WordPress |
||
| 210 | * |
||
| 211 | * @param string $directory |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | public static function directory_to_url( $directory ) { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Get the event emitter |
||
| 241 | * |
||
| 242 | * @return Emitter |
||
| 243 | */ |
||
| 244 | public function get_emitter() { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Add a listener to an event |
||
| 253 | * |
||
| 254 | * @param string $event |
||
| 255 | * @return Listener $listener |
||
| 256 | */ |
||
| 257 | public static function add_listener( $event, $listener ) { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Remove a listener from any event |
||
| 263 | * |
||
| 264 | * @param Listener $listener |
||
| 265 | */ |
||
| 266 | public static function remove_listener( $listener ) { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Add a persistent listener to an event |
||
| 272 | * |
||
| 273 | * @param string $event The event to listen for |
||
| 274 | * @param string $callable The callable to call when the event is broadcasted |
||
| 275 | * @return Listener |
||
| 276 | */ |
||
| 277 | public static function on( $event, $callable ) { |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Add a one-time listener to an event |
||
| 283 | * |
||
| 284 | * @param string $event The event to listen for |
||
| 285 | * @param string $callable The callable to call when the event is broadcasted |
||
| 286 | * @return Listener |
||
| 287 | */ |
||
| 288 | public static function once( $event, $callable ) { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Get default IoC container dependencies |
||
| 294 | * |
||
| 295 | * @return PimpleContainer |
||
| 296 | */ |
||
| 297 | protected static function get_default_ioc() { |
||
| 366 | } |