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 ) { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Resolve a service through IoC |
||
| 109 | * |
||
| 110 | * @param string $service_name |
||
| 111 | * @return mixed |
||
| 112 | */ |
||
| 113 | public static function service( $service_name ) { |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Check if a dependency is registered |
||
| 119 | * |
||
| 120 | * @param string $key |
||
| 121 | * @param string|null $subcontainer Subcontainer to look into |
||
| 122 | * @return bool |
||
| 123 | */ |
||
| 124 | View Code Duplication | public static function has( $key, $subcontainer = null ) { |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Extend Carbon Fields by adding a new entity (container condition etc.) |
||
| 137 | * |
||
| 138 | * @param string $class Extension class name |
||
| 139 | * @param string $extender Extending callable |
||
| 140 | */ |
||
| 141 | public static function extend( $class, $extender ) { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Replace the ioc container for Carbon_Fields\Carbon_Fields |
||
| 169 | * |
||
| 170 | * @param PimpleContainer $ioc |
||
| 171 | */ |
||
| 172 | public function install( PimpleContainer $ioc ) { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Boot Carbon Fields with default IoC dependencies |
||
| 178 | */ |
||
| 179 | public static function boot() { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Check if Carbon Fields has booted |
||
| 196 | */ |
||
| 197 | public static function is_booted() { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Throw exception if Carbon Fields has not been booted |
||
| 203 | */ |
||
| 204 | public static function verify_boot() { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Throw exception if fields have not been registered yet |
||
| 212 | */ |
||
| 213 | public static function verify_fields_registered() { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Resolve the public url of a directory inside WordPress |
||
| 223 | * |
||
| 224 | * @param string $directory |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | public static function directory_to_url( $directory ) { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Get the event emitter |
||
| 254 | * |
||
| 255 | * @return Emitter |
||
| 256 | */ |
||
| 257 | public function get_emitter() { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Add a listener to an event |
||
| 266 | * |
||
| 267 | * @param string $event |
||
| 268 | * @return Listener $listener |
||
| 269 | */ |
||
| 270 | public static function add_listener( $event, $listener ) { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Remove a listener from any event |
||
| 276 | * |
||
| 277 | * @param Listener $listener |
||
| 278 | */ |
||
| 279 | public static function remove_listener( $listener ) { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Add a persistent listener to an event |
||
| 285 | * |
||
| 286 | * @param string $event The event to listen for |
||
| 287 | * @param string $callable The callable to call when the event is broadcasted |
||
| 288 | * @return Listener |
||
| 289 | */ |
||
| 290 | public static function on( $event, $callable ) { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Add a one-time listener to an event |
||
| 296 | * |
||
| 297 | * @param string $event The event to listen for |
||
| 298 | * @param string $callable The callable to call when the event is broadcasted |
||
| 299 | * @return Listener |
||
| 300 | */ |
||
| 301 | public static function once( $event, $callable ) { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Get default IoC container dependencies |
||
| 307 | * |
||
| 308 | * @return PimpleContainer |
||
| 309 | */ |
||
| 310 | protected static function get_default_ioc() { |
||
| 383 | } |
||
| 384 |