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 | 37 | 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) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Create flyweight instance 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 MetaOptions|null $options |
||
| 257 | * @return static |
||
| 258 | */ |
||
| 259 | 44 | public static function create($model, MetaOptions $options = null) |
|
| 260 | { |
||
| 261 | // Reset local cache if dynamically added namespace |
||
| 262 | 44 | if (self::$addNs) |
|
| 263 | { |
||
| 264 | 5 | self::$c = []; |
|
| 265 | 5 | self::$addNs = false; |
|
| 266 | } |
||
| 267 | 44 | $class = is_object($model) ? get_class($model) : $model; |
|
| 268 | 44 | $key = static::class . $class; |
|
| 269 | 44 | if (isset(self::$c[$key])) |
|
| 270 | { |
||
| 271 | 7 | return self::$c[$key]; |
|
| 272 | } |
||
| 273 | 37 | $cache = FlyCache::instance(static::class, $model, $options); |
|
| 274 | |||
| 275 | 37 | $cached = $cache->get($class); |
|
| 276 | 37 | if ($cached !== false) |
|
| 277 | { |
||
| 278 | self::$c[$key] = $cached; |
||
| 279 | return $cached; |
||
| 280 | } |
||
| 281 | |||
| 282 | 37 | $instance = new static($model, $options); |
|
| 283 | 30 | self::$c[$key] = $instance; |
|
| 284 | 30 | $cache->set($class, $instance); |
|
| 285 | 30 | return $instance; |
|
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Get array of properties values for property field |
||
| 290 | * |
||
| 291 | * @param string $fieldName |
||
| 292 | * @param int $type type of entities to return Meta::Type|Meta::Field|Meta::Method |
||
| 293 | * @return array |
||
| 294 | */ |
||
| 295 | public function properties($fieldName, $type = Meta::Field) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Get fields annotations for selected field or for all fields |
||
| 325 | * @param string $fieldName |
||
| 326 | * @return mixed[] |
||
| 327 | * @todo Remove this |
||
| 328 | * @deprecated since version number |
||
| 329 | */ |
||
| 330 | public function annotations($fieldName = null) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Get class metadata |
||
| 341 | * @return MetaType |
||
| 342 | */ |
||
| 343 | 15 | public function type() |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Get all fields metadata with field name as key |
||
| 350 | * @return MetaProperty[] |
||
| 351 | */ |
||
| 352 | 2 | public function fields() |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Get field by name |
||
| 359 | * @param string $name |
||
| 360 | * @return MetaProperty |
||
| 361 | */ |
||
| 362 | 1 | public function field($name) |
|
| 363 | { |
||
| 364 | 1 | return $this->_fields[$name]; |
|
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Get all methods metadata |
||
| 369 | * @return MetaMethod[] |
||
| 370 | */ |
||
| 371 | public function methods() |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Get method metadata by name |
||
| 378 | * @param string $name |
||
| 379 | * @return MetaMethod|bool |
||
| 380 | */ |
||
| 381 | 7 | public function method($name) |
|
| 389 | |||
| 390 | /** |
||
| 391 | * Get fields directly-like |
||
| 392 | * @param string $name |
||
| 393 | * @return MetaProperty|boolean |
||
| 394 | */ |
||
| 395 | 18 | public function __get($name) |
|
| 406 | |||
| 407 | } |
||
| 408 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.