Complex classes like OrmAnnotation 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 OrmAnnotation, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | trait OrmAnnotation |
||
| 12 | { |
||
| 13 | use OrmDataTypeConverter; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Retrieve the annotated table name |
||
| 17 | * |
||
| 18 | * @param string $class The name of class |
||
| 19 | * @param string $fallback As fallback if nothing was found |
||
| 20 | * |
||
| 21 | * @return string The name of table |
||
| 22 | * |
||
| 23 | * @throws OrmException |
||
| 24 | */ |
||
| 25 | 32 | private static function getAnnotatedTableName($class, $fallback) |
|
| 42 | |||
| 43 | /** |
||
| 44 | * Get the annotated primary key property name |
||
| 45 | * |
||
| 46 | * The property is annotated with the @id annotation |
||
| 47 | * |
||
| 48 | * @param string $class The name of class to retrieve the primary key property |
||
| 49 | * |
||
| 50 | * @return string The name of property which represents the primary key |
||
| 51 | * |
||
| 52 | * @throws OrmException |
||
| 53 | */ |
||
| 54 | 10 | private static function getAnnotatedPrimaryKeyProperty($class) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Get the annotated primary key |
||
| 74 | * |
||
| 75 | * The property is annotated with the @id annotation |
||
| 76 | * The propery may have a @column annotation to modify the database column name |
||
| 77 | * |
||
| 78 | * @param string $class The name of class to retrieve the primary key column of |
||
| 79 | * |
||
| 80 | * @return string|null The name of primary key column |
||
| 81 | * |
||
| 82 | * @throws OrmException |
||
| 83 | */ |
||
| 84 | 20 | private static function getAnnotatedPrimaryKeyColumn($class) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Get the property type via annotation |
||
| 106 | * |
||
| 107 | * @param string $class The name of class to retrieve a particular property type |
||
| 108 | * @param string $propertyName The name of property to retrieve the type of |
||
| 109 | * @param string $namespace The namespace |
||
| 110 | * |
||
| 111 | * @return string|null The property type either as primitive type or full qualified class |
||
| 112 | */ |
||
| 113 | 26 | private static function getAnnotatedPropertyType($class, $propertyName, $namespace) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Get the value from property |
||
| 129 | * |
||
| 130 | * @param object $from The source object |
||
| 131 | * @param string $toClass The type of destination class |
||
| 132 | * @param \ReflectionProperty $property The property to get value of |
||
| 133 | * @param string $namespace The namespace of destination class |
||
| 134 | * |
||
| 135 | * @return array The type and value from property |
||
| 136 | */ |
||
| 137 | 26 | private static function getAnnotatedPropertyValue($from, $toClass, \ReflectionProperty $property, $namespace) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Retrieve list of columns and its corresponding pairs |
||
| 166 | * |
||
| 167 | * @param string $class The name of class to retrieve all column-value pairs of |
||
| 168 | * @param \Nkey\Caribu\Model\AbstractModel $object The entity to get the column-value pairs of |
||
| 169 | * |
||
| 170 | * @return array List of column => value pairs |
||
| 171 | * |
||
| 172 | * @throws OrmException |
||
| 173 | */ |
||
| 174 | 12 | private static function getAnnotatedColumnValuePairs($class, $object) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Retrieve the primary key name and value using annotation |
||
| 207 | * |
||
| 208 | * @param string $class The name of class to retrieve the primary key name and value |
||
| 209 | * @param \Nkey\Caribu\Model\AbstractModel $object The entity to retrieve the pimary key value |
||
| 210 | * @param boolean $onlyValue Whether to retrieve only the value instead of name and value |
||
| 211 | * |
||
| 212 | * @return array The "name" => "value" of primary key or only the value (depending on $onlyValue) |
||
| 213 | * |
||
| 214 | * @throws OrmException |
||
| 215 | */ |
||
| 216 | 16 | private static function getAnnotatedPrimaryKey($class, $object, $onlyValue = false) |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Get the annotated type |
||
| 251 | * |
||
| 252 | * @param string $comment The document comment string which may contain the @var annotation |
||
| 253 | * @param string $namespace Optional namespace where class is part of |
||
| 254 | * |
||
| 255 | * @return string The parsed type |
||
| 256 | * |
||
| 257 | * @throws OrmException |
||
| 258 | */ |
||
| 259 | 31 | private static function getAnnotatedType($comment, $namespace = null) |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Get the annotated column name |
||
| 285 | * |
||
| 286 | * @param string $class The name of class to retrieve te annotated column name |
||
| 287 | * @param string $property The property which is annotated by column name |
||
| 288 | * |
||
| 289 | * @return string The column name |
||
| 290 | */ |
||
| 291 | 1 | private static function getAnnotatedColumnFromProperty($class, $property) |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Get the annotated column name from document comment string |
||
| 299 | * |
||
| 300 | * @param string $comment The document comment which may contain the @column annotation |
||
| 301 | * |
||
| 302 | * @return string|null The parsed column name |
||
| 303 | */ |
||
| 304 | 30 | private static function getAnnotatedColumn($comment) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Check whether property is annotated using @id |
||
| 317 | * |
||
| 318 | * @param string $comment The document comment which may contain the @id annotation |
||
| 319 | * |
||
| 320 | * @return boolean true in case of it is annotated, false otherwise |
||
| 321 | */ |
||
| 322 | 22 | private static function isIdAnnotated($comment) |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Check whether property is annotated using @cascade |
||
| 329 | * |
||
| 330 | * @param string $comment The document comment which may contain the @cascade annotation |
||
| 331 | * |
||
| 332 | * @return boolean true in case of it is annotated, false otherwise |
||
| 333 | */ |
||
| 334 | 13 | private static function isCascadeAnnotated($comment) |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Get the mappedBy parameters from documentation comment |
||
| 341 | * |
||
| 342 | * @param string $comment The documentation comment to parse |
||
| 343 | * |
||
| 344 | * @return string The parsed parameters or null |
||
| 345 | */ |
||
| 346 | 22 | private static function getAnnotatedMappedByParameters($comment) |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Checks whether an entity has eager fetch type |
||
| 359 | * |
||
| 360 | * @param string $class Name of class of entity |
||
| 361 | * |
||
| 362 | * @return boolean true if fetch type is eager, false otherwise |
||
| 363 | */ |
||
| 364 | 26 | private static function isEager($class) |
|
| 369 | } |
||
| 370 |