Complex classes like ValueParser 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 ValueParser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class ValueParser extends AbstractParser |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * The regex to get variables '$(VARIABLE)' in .env |
||
| 32 | * Unescaped: ${(.*?)} |
||
| 33 | * |
||
| 34 | * @var string REGEX_ENV_VARIABLE |
||
| 35 | */ |
||
| 36 | const REGEX_ENV_VARIABLE = '\\${(.*?)}'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The regex to get the content between double quote (") strings, ignoring escaped quotes. |
||
| 40 | * Unescaped: "(?:[^"\\]*(?:\\.)?)*" |
||
| 41 | * |
||
| 42 | * @var string REGEX_QUOTE_DOUBLE_STRING |
||
| 43 | */ |
||
| 44 | const REGEX_QUOTE_DOUBLE_STRING = '"(?:[^\"\\\\]*(?:\\\\.)?)*\"'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The regex to get the content between single quote (') strings, ignoring escaped quotes |
||
| 48 | * Unescaped: '(?:[^'\\]*(?:\\.)?)*' |
||
| 49 | * |
||
| 50 | * @var string REGEX_QUOTE_SINGLE_STRING |
||
| 51 | */ |
||
| 52 | const REGEX_QUOTE_SINGLE_STRING = "'(?:[^'\\\\]*(?:\\\\.)?)*'"; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The bool variants available in .env |
||
| 56 | * |
||
| 57 | * @var array $bool_variants |
||
| 58 | */ |
||
| 59 | private static $bool_variants = array( |
||
| 60 | 'true', 'false', 'yes', 'no' |
||
| 61 | ); |
||
| 62 | |||
| 63 | /** |
||
| 64 | * The map to convert escaped characters into real characters |
||
| 65 | * |
||
| 66 | * @var array $character_map |
||
| 67 | */ |
||
| 68 | private static $character_map = array( |
||
| 69 | "\\n" => "\n", |
||
| 70 | "\\\"" => "\"", |
||
| 71 | '\\\'' => "'", |
||
| 72 | '\\t' => "\t" |
||
| 73 | ); |
||
| 74 | |||
| 75 | /** |
||
| 76 | * The line num of the current value |
||
| 77 | * |
||
| 78 | * @var int $line_num |
||
| 79 | */ |
||
| 80 | private $line_num; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * The current parsed values/lines |
||
| 84 | * |
||
| 85 | * @var array $lines |
||
| 86 | */ |
||
| 87 | private $lines; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Parses a .env value |
||
| 91 | * |
||
| 92 | * @param string $value The value to parse |
||
| 93 | * @param array $lines The array of already parsed lines |
||
| 94 | * @param int $line_num The line num of the value |
||
| 95 | * |
||
| 96 | * @return string|null The parsed key, or null if the key is a comment |
||
| 97 | */ |
||
| 98 | 30 | public function parse($value, $lines, $line_num) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Parses a .env variable |
||
| 137 | * |
||
| 138 | * @param string $value The value to parse |
||
| 139 | * @param bool $quoted_string Is the value in a quoted string |
||
| 140 | * |
||
| 141 | * @return string The parsed value |
||
| 142 | */ |
||
| 143 | 24 | private function parseVariables($value, $quoted_string = false) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Get variable matches inside a string |
||
| 169 | * |
||
| 170 | * @param string $value The value to parse |
||
| 171 | * |
||
| 172 | * @return array The variable matches |
||
| 173 | */ |
||
| 174 | 24 | private function fetchVariableMatches($value) |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Parses a .env variable |
||
| 187 | * |
||
| 188 | * @param string $value The value to parse |
||
| 189 | * @param string $variable_name The variable name to get |
||
| 190 | * @param array $matches The matches of the variables |
||
| 191 | * @param bool $quoted_string Is the value in a quoted string |
||
| 192 | * |
||
| 193 | * @throws \M1\Env\Exception\ParseException If the variable can not be found |
||
| 194 | * |
||
| 195 | * @return string The parsed value |
||
| 196 | */ |
||
| 197 | 6 | private function fetchVariable($value, $variable_name, $matches, $quoted_string) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Parses a .env string |
||
| 222 | * |
||
| 223 | * @param string $value The value to parse |
||
| 224 | * |
||
| 225 | * @throws \M1\Env\Exception\ParseException If the string has a missing end quote |
||
| 226 | * |
||
| 227 | * @return string The parsed string |
||
| 228 | */ |
||
| 229 | 12 | private function parseString($value) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Parses a .env unquoted string |
||
| 257 | * |
||
| 258 | * @param string $value The value to parse |
||
| 259 | * |
||
| 260 | * @return string The parsed string |
||
| 261 | */ |
||
| 262 | 24 | private function parseUnquotedString($value) |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Parses a .env bool |
||
| 273 | * |
||
| 274 | * @param string $value The value to parse |
||
| 275 | * |
||
| 276 | * @return bool The parsed bool |
||
| 277 | */ |
||
| 278 | 6 | private function parseBool($value) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Parses a .env number |
||
| 297 | * |
||
| 298 | * @param string $value The value to parse |
||
| 299 | * |
||
| 300 | * @return int|float The parsed bool |
||
| 301 | */ |
||
| 302 | 6 | private function parseNumber($value) |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Strips comments from a value |
||
| 313 | * |
||
| 314 | * @param string $value The value to strip comments from |
||
| 315 | * |
||
| 316 | * @return string value |
||
| 317 | */ |
||
| 318 | 27 | private function stripComments($value) |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Returns if value is a string |
||
| 325 | * |
||
| 326 | * @param string $value The value to test |
||
| 327 | * |
||
| 328 | * @return bool Is a value a string |
||
| 329 | */ |
||
| 330 | 30 | private function isString($value) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Returns if value is a bool |
||
| 337 | * |
||
| 338 | * @param string $value The value to test |
||
| 339 | * |
||
| 340 | * @return bool Is a value a bool |
||
| 341 | */ |
||
| 342 | 27 | private function isBool($value) |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Returns if value is number |
||
| 349 | * |
||
| 350 | * @param string $value The value to test |
||
| 351 | * |
||
| 352 | * @return bool Is a value a number |
||
| 353 | */ |
||
| 354 | 24 | private function isNumber($value) |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Returns if value is null |
||
| 361 | * |
||
| 362 | * @param string $value The value to test |
||
| 363 | * |
||
| 364 | * @return bool Is a value null |
||
| 365 | */ |
||
| 366 | 24 | private function isNull($value) |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Returns if variable value is a clone, e.g. BOOL = $(BOOL_1) |
||
| 373 | * |
||
| 374 | * @param string $value The value to test |
||
| 375 | * @param array $matches The matches of the variables |
||
| 376 | * @param bool $quoted_string If the value is in a quoted string |
||
| 377 | * |
||
| 378 | * @return bool Is a value null |
||
| 379 | */ |
||
| 380 | 6 | private function isVariableClone($value, $matches, $quoted_string) |
|
| 384 | } |