Complex classes like Namer 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 Namer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Namer |
||
| 18 | { |
||
| 19 | /** The template to use to calculate the table name. |
||
| 20 | * @var string */ |
||
| 21 | protected $tableNameTemplate = '%short%'; |
||
| 22 | |||
| 23 | /** The naming scheme to use for table names. |
||
| 24 | * @var string */ |
||
| 25 | protected $tableNameScheme = 'snake_lower'; |
||
| 26 | |||
| 27 | /** @var string[] */ |
||
| 28 | protected $tableNames = []; |
||
| 29 | |||
| 30 | /** @var string[][] */ |
||
| 31 | protected $columnNames = []; |
||
| 32 | |||
| 33 | /** The naming scheme to use for column names. |
||
| 34 | * @var string */ |
||
| 35 | protected $columnNameScheme = 'snake_lower'; |
||
| 36 | |||
| 37 | /** The naming scheme used for method names. |
||
| 38 | * @var string */ |
||
| 39 | protected $methodNameScheme = 'camelCase'; |
||
| 40 | |||
| 41 | /** The naming scheme used for attributes. |
||
| 42 | * @var string */ |
||
| 43 | protected $attributeNameScheme = 'camelCase'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | 297 | * Namer constructor. |
|
| 47 | * |
||
| 48 | 297 | * @param array $options |
|
| 49 | 4 | */ |
|
| 50 | public function __construct($options = []) |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Set $option to $value |
||
| 59 | * |
||
| 60 | 4 | * @param string $option |
|
| 61 | * @param mixed $value |
||
| 62 | * @return static |
||
| 63 | 4 | */ |
|
| 64 | 1 | public function setOption($option, $value) |
|
| 90 | |||
| 91 | /** |
||
| 92 | 147 | * Get the table name for $reflection |
|
| 93 | * |
||
| 94 | 147 | * @param string $class |
|
| 95 | 147 | * @param string $template |
|
| 96 | 2 | * @param string $namingScheme |
|
| 97 | * @return string |
||
| 98 | * @throws InvalidName |
||
| 99 | 147 | */ |
|
| 100 | 4 | public function getTableName($class, $template = null, $namingScheme = null) |
|
| 132 | |||
| 133 | /** |
||
| 134 | 166 | * Get the column name with $namingScheme or default naming scheme |
|
| 135 | * |
||
| 136 | 166 | * @param string $class |
|
| 137 | 166 | * @param string $attribute |
|
| 138 | 2 | * @param string $prefix |
|
| 139 | * @param string $namingScheme |
||
| 140 | * @return string |
||
| 141 | 166 | */ |
|
| 142 | public function getColumnName($class, $attribute, $prefix = null, $namingScheme = null) |
||
| 160 | 121 | ||
| 161 | /** |
||
| 162 | 121 | * Get the method name with $namingScheme or default naming scheme |
|
| 163 | 2 | * |
|
| 164 | * @param string $name |
||
| 165 | * @param string $namingScheme |
||
| 166 | 121 | * @return string |
|
| 167 | */ |
||
| 168 | public function getMethodName($name, $namingScheme = null) |
||
| 176 | |||
| 177 | |||
| 178 | /** |
||
| 179 | 186 | * Get the attribute name with $namingScheme or default naming scheme |
|
| 180 | * |
||
| 181 | 186 | * @param string $name |
|
| 182 | 186 | * @param string $namingScheme |
|
| 183 | 186 | * @return string |
|
| 184 | */ |
||
| 185 | 185 | public function getAttributeName($name, $prefix = null, $namingScheme = null) |
|
| 197 | |||
| 198 | /** |
||
| 199 | * Substitute a $template with $values |
||
| 200 | * |
||
| 201 | * $values is a key value pair array. The value should be a string or an array o |
||
| 202 | * |
||
| 203 | * @param string $template |
||
| 204 | * @param array $values |
||
| 205 | * @param string $arrayGlue |
||
| 206 | 255 | * @return string |
|
| 207 | */ |
||
| 208 | 255 | public function substitute($template, $values = [], $arrayGlue = ', ') |
|
| 223 | 198 | ||
| 224 | /** |
||
| 225 | 149 | * Enforce $namingScheme to $name |
|
| 226 | 4 | * |
|
| 227 | 4 | * Supported naming schemes: snake_case, snake_lower, SNAKE_UPPER, Snake_Ucfirst, camelCase, StudlyCaps, lower |
|
| 228 | * and UPPER. |
||
| 229 | 145 | * |
|
| 230 | 4 | * @param string $name The name of the var / column |
|
| 231 | 4 | * @param string $namingScheme The naming scheme to use |
|
| 232 | * @return string |
||
| 233 | 141 | * @throws InvalidConfiguration |
|
| 234 | 123 | */ |
|
| 235 | 123 | public function forceNamingScheme($name, $namingScheme) |
|
| 284 | |||
| 285 | /** |
||
| 286 | 16 | * Get the value for $attribute from $values using $arrayGlue |
|
| 287 | * |
||
| 288 | * @param string $attribute The key for $values |
||
| 289 | * @param array $values |
||
| 290 | * @param string $arrayGlue |
||
| 291 | * @return string |
||
| 292 | * @throws InvalidConfiguration |
||
| 293 | */ |
||
| 294 | protected function getValue($attribute, $values, $arrayGlue) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Convert array to string using indexes defined by $accessor |
||
| 320 | * |
||
| 321 | * @param array $array |
||
| 322 | * @param string $accessor |
||
| 323 | * @param string $glue |
||
| 324 | * @return string |
||
| 325 | */ |
||
| 326 | protected function arrayToString(array $array, $accessor, $glue) |
||
| 342 | } |
||
| 343 |