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 |
||
| 16 | class Namer |
||
| 17 | { |
||
| 18 | /** The template to use to calculate the table name. |
||
| 19 | * @var string */ |
||
| 20 | protected $tableNameTemplate = '%short%'; |
||
| 21 | |||
| 22 | /** The naming scheme to use for table names. |
||
| 23 | * @var string */ |
||
| 24 | protected $tableNameScheme = 'snake_lower'; |
||
| 25 | |||
| 26 | /** @var string[] */ |
||
| 27 | protected $tableNames = []; |
||
| 28 | |||
| 29 | /** @var string[][] */ |
||
| 30 | protected $columnNames = []; |
||
| 31 | |||
| 32 | /** The naming scheme to use for column names. |
||
| 33 | * @var string */ |
||
| 34 | protected $columnNameScheme = 'snake_lower'; |
||
| 35 | |||
| 36 | /** The naming scheme used for method names. |
||
| 37 | * @var string */ |
||
| 38 | 265 | protected $methodNameScheme = 'camelCase'; |
|
| 39 | |||
| 40 | 265 | /** |
|
| 41 | 4 | * Namer constructor. |
|
| 42 | * |
||
| 43 | 265 | * @param array $options |
|
| 44 | */ |
||
| 45 | public function __construct($options = []) |
||
| 51 | |||
| 52 | 4 | /** |
|
| 53 | * Set $option to $value |
||
| 54 | * |
||
| 55 | 4 | * @param string $option |
|
| 56 | 1 | * @param mixed $value |
|
| 57 | 1 | * @return self |
|
| 58 | */ |
||
| 59 | 3 | public function setOption($option, $value) |
|
| 81 | |||
| 82 | /** |
||
| 83 | 130 | * Get the table name for $reflection |
|
| 84 | * |
||
| 85 | 130 | * @param string $class |
|
| 86 | 2 | * @param string $template |
|
| 87 | * @param string $namingScheme |
||
| 88 | * @return string |
||
| 89 | 130 | * @throws InvalidName |
|
| 90 | 4 | */ |
|
| 91 | public function getTableName($class, $template = null, $namingScheme = null) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Get the column name with $namingScheme or default naming scheme |
||
| 122 | * |
||
| 123 | * @param $class |
||
| 124 | * @param string $field |
||
| 125 | 89 | * @param string $prefix |
|
| 126 | * @param string $namingScheme |
||
| 127 | 89 | * @return string |
|
| 128 | 2 | */ |
|
| 129 | public function getColumnName($class, $field, $prefix = null, $namingScheme = null) |
||
| 147 | 168 | ||
| 148 | /** |
||
| 149 | * Get the column name with $namingScheme or default naming scheme |
||
| 150 | 167 | * |
|
| 151 | 1 | * @param $name |
|
| 152 | * @param null $namingScheme |
||
| 153 | * @return string |
||
| 154 | 167 | */ |
|
| 155 | 168 | public function getMethodName($name, $namingScheme = null) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Substitute a $template with $values |
||
| 166 | * |
||
| 167 | * $values is a key value pair array. The value should be a string or an array o |
||
| 168 | * |
||
| 169 | * @param string $template |
||
| 170 | * @param array $values |
||
| 171 | 226 | * @param string $arrayGlue |
|
| 172 | * @return string |
||
| 173 | 226 | */ |
|
| 174 | 226 | public function substitute($template, $values = [], $arrayGlue = ', ') |
|
| 189 | |||
| 190 | 117 | /** |
|
| 191 | 4 | * Enforce $namingScheme to $name |
|
| 192 | 4 | * |
|
| 193 | * Supported naming schemes: snake_case, snake_lower, SNAKE_UPPER, Snake_Ucfirst, camelCase, StudlyCaps, lower |
||
| 194 | 113 | * and UPPER. |
|
| 195 | 4 | * |
|
| 196 | 4 | * @param string $name The name of the var / column |
|
| 197 | * @param string $namingScheme The naming scheme to use |
||
| 198 | 109 | * @return string |
|
| 199 | 91 | * @throws InvalidConfiguration |
|
| 200 | 91 | */ |
|
| 201 | public function forceNamingScheme($name, $namingScheme) |
||
| 250 | |||
| 251 | 9 | protected function getValue($var, $values, $arrayGlue) |
|
| 287 | } |
||
| 288 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: