Complex classes like Meta often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Meta, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class Meta |
||
| 36 | { |
||
| 37 | |||
| 38 | const Type = 1; |
||
| 39 | const Field = 2; |
||
| 40 | const Method = 3; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Container for type metadata |
||
| 44 | * @var MetaType |
||
| 45 | */ |
||
| 46 | protected $_type = null; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Array of containers for property metadata |
||
| 50 | * @var MetaProperty[] |
||
| 51 | */ |
||
| 52 | protected $_fields = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Array of containers for method metadata |
||
| 56 | * @var MetaMethod[] |
||
| 57 | */ |
||
| 58 | protected $_methods = []; |
||
| 59 | private $_annotations = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * On-hand cache |
||
| 63 | * @var Meta[] |
||
| 64 | */ |
||
| 65 | private static $c = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Flag to clear local cache if namespace was dynamically added |
||
| 69 | * @var bool |
||
| 70 | */ |
||
| 71 | public static $addNs = false; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param string|object|AnnotatedInterface $model |
||
| 75 | * @param MetaOptions $options |
||
| 76 | * @throws Exception |
||
| 77 | */ |
||
| 78 | 32 | protected function __construct($model = null, MetaOptions $options = null) |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Set state implementation |
||
| 238 | * @param mixed $data |
||
| 239 | * @return static |
||
| 240 | */ |
||
| 241 | public static function __set_state($data) |
||
| 242 | { |
||
| 243 | $obj = new static(null); |
||
| 244 | foreach ($data as $field => $value) |
||
| 245 | { |
||
| 246 | $obj->$field = $value; |
||
| 247 | } |
||
| 248 | return $obj; |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Create flyghtweight instace of `Meta`. |
||
| 253 | * Calling this function will create new instance only if it's not stored in cache. |
||
| 254 | * This allows very effective retrieving of `Meta` container's meta data, without need of parsing annotations. |
||
| 255 | * @param string|object|AnnotatedInterface $model |
||
| 256 | * @param MetaOption|null $options |
||
| 257 | * @return static |
||
| 258 | */ |
||
| 259 | 39 | public static function create($model, MetaOptions $options = null) |
|
| 260 | { |
||
| 261 | // Reset local cache if dynamically added namespace |
||
| 262 | 39 | if (self::$addNs) |
|
| 263 | { |
||
| 264 | 5 | self::$c = []; |
|
| 265 | 5 | self::$addNs = false; |
|
| 266 | } |
||
| 267 | 39 | $class = is_object($model) ? get_class($model) : $model; |
|
| 268 | 39 | $key = static::class . $class; |
|
| 269 | 39 | if (isset(self::$c[$key])) |
|
| 270 | { |
||
| 271 | 7 | return self::$c[$key]; |
|
| 272 | } |
||
| 273 | 32 | $cache = FlyCache::instance(static::class, $model, $options); |
|
| 274 | |||
| 275 | 32 | $cached = $cache->get(); |
|
| 276 | 32 | if ($cached) |
|
| 277 | { |
||
| 278 | self::$c[$key] = $cached; |
||
| 279 | return $cached; |
||
| 280 | } |
||
| 281 | |||
| 282 | 32 | $instance = new static($model, $options); |
|
| 283 | 25 | self::$c[$key] = $instance; |
|
| 284 | 25 | return $cache->set($instance); |
|
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Get array of properties values for property field |
||
| 289 | * |
||
| 290 | * @param string $fieldName |
||
| 291 | * @param enum $type type of entities to return Meta::Type|Meta::Field|Meta::Method |
||
| 292 | * @return type |
||
| 293 | */ |
||
| 294 | public function properties($fieldName, $type = Meta::Field) |
||
|
|
|||
| 295 | { |
||
| 296 | $result = []; |
||
| 297 | switch ($type) |
||
| 298 | { |
||
| 299 | case self::Type: |
||
| 300 | $from = $this->_type; |
||
| 301 | break; |
||
| 302 | case self::Field: |
||
| 303 | $from = $this->_fields; |
||
| 304 | break; |
||
| 305 | case self::Method: |
||
| 306 | $from = $this->_methods; |
||
| 307 | break; |
||
| 308 | default: |
||
| 309 | $from = $this->_fields; |
||
| 310 | break; |
||
| 311 | } |
||
| 312 | foreach ($from as $name => $field) |
||
| 313 | { |
||
| 314 | if (isset($field->$fieldName)) |
||
| 315 | { |
||
| 316 | $result[$name] = $field->$fieldName; |
||
| 317 | } |
||
| 318 | } |
||
| 319 | return $result; |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Get fields annotations for selected field or for all fields |
||
| 324 | * @param string $fieldName |
||
| 325 | * @return mixed[] |
||
| 326 | * @todo Remove this |
||
| 327 | * @deprecated since version number |
||
| 328 | */ |
||
| 329 | public function annotations($fieldName = null) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Get class metadata |
||
| 340 | * @return MetaType |
||
| 341 | */ |
||
| 342 | 10 | public function type() |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Get all fields metadata with field name as key |
||
| 349 | * @return MetaProperty[] |
||
| 350 | */ |
||
| 351 | 1 | public function fields() |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Get field by name |
||
| 358 | * @param string $name |
||
| 359 | * @return MetaProperty |
||
| 360 | */ |
||
| 361 | 1 | public function field($name) |
|
| 362 | { |
||
| 363 | 1 | return $this->_fields[$name]; |
|
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Get all methods metadata |
||
| 368 | * @return MetaMethod[] |
||
| 369 | */ |
||
| 370 | public function methods() |
||
| 371 | { |
||
| 372 | return $this->_methods; |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Get method metadata by name |
||
| 377 | * @param string $name |
||
| 378 | * @return MetaMethod|bool |
||
| 379 | */ |
||
| 380 | 7 | public function method($name) |
|
| 388 | |||
| 389 | /** |
||
| 390 | * Get fields directly-like |
||
| 391 | * @param string $name |
||
| 392 | * @return MetaProperty|boolean |
||
| 393 | */ |
||
| 394 | 19 | public function __get($name) |
|
| 405 | |||
| 406 | } |
||
| 407 |
This check looks for accesses to local static members using the fully qualified name instead of
self::.While this is perfectly valid, the fully qualified name of
Certificate::TRIPLEDES_CBCcould just as well be replaced byself::TRIPLEDES_CBC. Referencing local members withself::assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.