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 |
||
| 29 | final class Carbon_Fields { |
||
| 30 | |||
| 31 | /** |
||
| 32 | * An event emitter to facilitate events before the WordPress environment is guaranteed to be loaded |
||
| 33 | * |
||
| 34 | * @var Emitter |
||
| 35 | */ |
||
| 36 | protected $emitter; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Flag if Carbon Fields has been booted |
||
| 40 | * |
||
| 41 | * @var bool |
||
| 42 | */ |
||
| 43 | public $booted = false; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Inversion of Control container instance |
||
| 47 | * |
||
| 48 | * @var PimpleContainer |
||
| 49 | */ |
||
| 50 | public $ioc = null; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Singleton implementation |
||
| 54 | * |
||
| 55 | * @return Carbon_Fields |
||
| 56 | */ |
||
| 57 | public static function instance() { |
||
| 58 | static $instance = null; |
||
| 59 | if ( $instance === null ) { |
||
| 60 | $instance = new static(); |
||
| 61 | } |
||
| 62 | return $instance; |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Constructor |
||
| 67 | */ |
||
| 68 | private function __construct() { |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Resolve a dependency through IoC |
||
| 74 | * |
||
| 75 | * @param string $key |
||
| 76 | * @param string|null $subcontainer Subcontainer to look into |
||
| 77 | * @return mixed |
||
| 78 | */ |
||
| 79 | View Code Duplication | public static function resolve( $key, $subcontainer = null ) { |
|
| 80 | $ioc = static::instance()->ioc; |
||
| 81 | if ( $subcontainer !== null ) { |
||
| 82 | if ( ! isset( $ioc[ $subcontainer ] ) ) { |
||
| 83 | return null; |
||
| 84 | } |
||
| 85 | $ioc = $ioc[ $subcontainer ]; |
||
| 86 | } |
||
| 87 | return $ioc[ $key ]; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Resolve a dependency through IoC with arguments |
||
| 92 | * |
||
| 93 | * @param string $identifier Key to resolve from the container |
||
| 94 | * @param array $arguments Key-value array of arguments |
||
| 95 | * @param string $subcontainer The container to resolve from |
||
| 96 | * @return mixed |
||
| 97 | */ |
||
| 98 | public static function resolve_with_arguments( $identifier, $arguments, $subcontainer = null ) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Resolve a service through IoC |
||
| 110 | * |
||
| 111 | * @param string $service_name |
||
| 112 | * @return mixed |
||
| 113 | */ |
||
| 114 | public static function service( $service_name ) { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Check if a dependency is registered |
||
| 120 | * |
||
| 121 | * @param string $key |
||
| 122 | * @param string|null $subcontainer Subcontainer to look into |
||
| 123 | * @return bool |
||
| 124 | */ |
||
| 125 | View Code Duplication | public static function has( $key, $subcontainer = null ) { |
|
| 126 | $ioc = static::instance()->ioc; |
||
| 127 | if ( $subcontainer !== null ) { |
||
| 128 | if ( ! isset( $ioc[ $subcontainer ] ) ) { |
||
| 129 | return false; |
||
| 130 | } |
||
| 131 | $ioc = $ioc[ $subcontainer ]; |
||
| 132 | } |
||
| 133 | return isset( $ioc[ $key ] ); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Extend Carbon Fields by adding a new entity (container condition etc.) |
||
| 138 | * |
||
| 139 | * @param string $class Extension class name |
||
| 140 | * @param string $extender Extending callable |
||
| 141 | */ |
||
| 142 | public static function extend( $class, $extender ) { |
||
| 143 | $type_dictionary = array( |
||
| 144 | '_Container' => 'containers', |
||
| 145 | '_Field' => 'fields', |
||
| 146 | '_Condition' => 'container_conditions', |
||
| 147 | ); |
||
| 148 | |||
| 149 | $extension_suffix = ''; |
||
| 150 | $extension_subcontainer = ''; |
||
| 151 | foreach ( $type_dictionary as $suffix => $subcontainer ) { |
||
| 152 | if ( substr( $class, -strlen( $suffix ) ) === $suffix ) { |
||
| 153 | $extension_suffix = $suffix; |
||
| 154 | $extension_subcontainer = $subcontainer; |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | if ( empty( $extension_suffix ) ) { |
||
| 159 | Incorrect_Syntax_Exception::raise( 'Could not determine "' . $class . '" extension type. Extension classes must have one of the following suffixes: ' . implode( ', ', array_keys( $type_dictionary ) ) ); |
||
| 160 | return; |
||
| 161 | } |
||
| 162 | |||
| 163 | $identifier = Helper::class_to_type( $class, $extension_suffix ); |
||
| 164 | $ioc = static::instance()->ioc[ $extension_subcontainer ]; |
||
| 165 | $ioc[ $identifier ] = $ioc->factory( $extender ); |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Replace the ioc container for Carbon_Fields\Carbon_Fields |
||
| 170 | * |
||
| 171 | * @param PimpleContainer $ioc |
||
| 172 | */ |
||
| 173 | public function install( PimpleContainer $ioc ) { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Boot Carbon Fields with default IoC dependencies |
||
| 179 | */ |
||
| 180 | public static function boot() { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Check if Carbon Fields has booted |
||
| 197 | */ |
||
| 198 | public static function is_booted() { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Throw exception if Carbon Fields has not been booted |
||
| 204 | */ |
||
| 205 | public static function verify_boot() { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Throw exception if fields have not been registered yet |
||
| 213 | */ |
||
| 214 | public static function verify_fields_registered() { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Resolve the public url of a directory inside WordPress |
||
| 224 | * |
||
| 225 | * @param string $directory |
||
| 226 | * @return string |
||
| 227 | */ |
||
| 228 | public static function directory_to_url( $directory ) { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Get the event emitter |
||
| 255 | * |
||
| 256 | * @return Emitter |
||
| 257 | */ |
||
| 258 | public function get_emitter() { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Add a listener to an event |
||
| 267 | * |
||
| 268 | * @param string $event |
||
| 269 | * @param Event\Listener $listener |
||
| 270 | * @return Event\Listener $listener |
||
| 271 | */ |
||
| 272 | public static function add_listener( $event, $listener ) { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Remove a listener from any event |
||
| 278 | * |
||
| 279 | * @param Event\Listener $listener |
||
| 280 | */ |
||
| 281 | public static function remove_listener( $listener ) { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Add a persistent listener to an event |
||
| 287 | * |
||
| 288 | * @param string $event The event to listen for |
||
| 289 | * @param string $callable The callable to call when the event is broadcasted |
||
| 290 | * @return Event\Listener |
||
| 291 | */ |
||
| 292 | public static function on( $event, $callable ) { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Add a one-time listener to an event |
||
| 298 | * |
||
| 299 | * @param string $event The event to listen for |
||
| 300 | * @param string $callable The callable to call when the event is broadcasted |
||
| 301 | * @return Event\Listener |
||
| 302 | */ |
||
| 303 | public static function once( $event, $callable ) { |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Get default IoC container dependencies |
||
| 309 | * |
||
| 310 | * @return PimpleContainer |
||
| 311 | */ |
||
| 312 | protected static function get_default_ioc() { |
||
| 389 | } |
||
| 390 |