Complex classes like VariableParser 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 VariableParser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class VariableParser 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 symbol for the assign default value parameter expansion |
||
40 | * |
||
41 | * @var string SYMBOL_ASSIGN_DEFAULT_VALUE |
||
42 | */ |
||
43 | const SYMBOL_ASSIGN_DEFAULT_VALUE = '='; |
||
44 | |||
45 | /** |
||
46 | * The symbol for the default value parameter expansion |
||
47 | * |
||
48 | * @var string SYMBOL_DEFAULT_VALUE |
||
49 | */ |
||
50 | const SYMBOL_DEFAULT_VALUE = '-'; |
||
51 | |||
52 | /** |
||
53 | * Variables context |
||
54 | * |
||
55 | * @var array |
||
56 | */ |
||
57 | private $context; |
||
58 | |||
59 | /** |
||
60 | 36 | * {@inheritdoc} |
|
61 | * |
||
62 | 36 | * @param \M1\Env\Parser $parser The parent parser |
|
63 | * @param array $context Variables context |
||
64 | 36 | */ |
|
65 | 18 | public function __construct($parser, array $context = array()) |
|
71 | |||
72 | 30 | /** |
|
73 | * Parses a .env variable |
||
74 | * |
||
75 | * @param string $value The value to parse |
||
76 | * @param bool $quoted_string Is the value in a quoted string |
||
77 | * |
||
78 | * @return string The parsed value |
||
79 | */ |
||
80 | public function parse($value, $quoted_string = false) |
||
94 | |||
95 | /** |
||
96 | * Get variable matches inside a string |
||
97 | * |
||
98 | * @param string $value The value to parse |
||
99 | * |
||
100 | * @return array The variable matches |
||
101 | */ |
||
102 | private function fetchVariableMatches($value) |
||
112 | 12 | ||
113 | 6 | /** |
|
114 | 6 | * Parses a .env variable |
|
115 | * |
||
116 | 12 | * @param string $value The value to parse |
|
117 | * @param string $variable_name The variable name to get |
||
118 | * @param array $matches The matches of the variables |
||
119 | * @param bool $quoted_string Is the value in a quoted string |
||
120 | * |
||
121 | * @return string The parsed value |
||
122 | * @throws \M1\Env\Exception\ParseException If the variable can not be found |
||
123 | */ |
||
124 | private function fetchVariable($value, $variable_name, $matches, $quoted_string) |
||
144 | 9 | ||
145 | /** |
||
146 | 9 | * Checks to see if the variable has a parameter expansion |
|
147 | * |
||
148 | 9 | * @param string $variable The variable to check |
|
149 | 9 | * |
|
150 | * @return bool Does the variable have a parameter expansion |
||
151 | 6 | */ |
|
152 | 6 | private function hasParameterExpansion($variable) |
|
162 | |||
163 | /** |
||
164 | * Fetches and sets the parameter expansion |
||
165 | * |
||
166 | * @param string $variable_name The parameter expansion inside this to fetch |
||
167 | 9 | * |
|
168 | * @return string The parsed value |
||
169 | 9 | */ |
|
170 | 9 | private function fetchParameterExpansion($variable_name) |
|
185 | |||
186 | 9 | /** |
|
187 | 9 | * Fetches the parameter expansion type |
|
188 | 9 | * |
|
189 | * @param string $variable_name The parameter expansion type to get from this |
||
190 | 9 | * |
|
191 | * @return string The parameter expansion type |
||
192 | 9 | */ |
|
193 | 9 | private function fetchParameterExpansionType($variable_name) |
|
201 | |||
202 | /** |
||
203 | * Fetches the parameter type symbol |
||
204 | * |
||
205 | * @param string $variable_name The variable |
||
206 | * @param string $type The type of parameter expansion |
||
207 | * |
||
208 | * @return array The symbol and if there is a empty check |
||
209 | 9 | */ |
|
210 | private function fetchParameterExpansionSymbol($variable_name, $type) |
||
224 | |||
225 | /** |
||
226 | * Splits the parameter expansion into variable and default |
||
227 | * |
||
228 | * @param string $variable_name The variable name to split |
||
229 | * @param string $parameter_symbol The parameter expansion symbol |
||
230 | * |
||
231 | * @throws \M1\Env\Exception\ParseException If the parameter expansion if not valid syntax |
||
232 | * |
||
233 | * @return array The split variable and default value |
||
234 | */ |
||
235 | private function splitVariableDefault($variable_name, $parameter_symbol) |
||
249 | |||
250 | |||
251 | /** |
||
252 | * Parses and sets the variable and default if needed |
||
253 | * |
||
254 | * @param string $variable The variable to parse |
||
255 | 6 | * @param string $default The default value |
|
256 | * @param bool $exists Does the variable exist |
||
257 | 6 | * @param bool $empty Is there the variable empty if exists and the empty flag is set |
|
258 | * @param string $type The type of parameter expansion |
||
259 | 6 | * |
|
260 | 6 | * @return string The parsed value |
|
261 | 6 | */ |
|
262 | private function parseVariableParameter($variable, $default, $exists, $empty, $type) |
||
270 | |||
271 | /** |
||
272 | * Parses and sets the variable parameter to default |
||
273 | * |
||
274 | * @param string $variable The variable to parse |
||
275 | * @param string $default The default value |
||
276 | * @param bool $empty Is there the variable empty if exists and the empty flag is set |
||
277 | 15 | * @param string $type The type of parameter expansion |
|
278 | * |
||
279 | 15 | * @return string The parsed default value |
|
280 | 9 | */ |
|
281 | 6 | private function assignVariableParameterDefault($variable, $default, $empty, $type) |
|
291 | 12 | ||
292 | /** |
||
293 | * Checks to see if a variable exists or throw |
||
294 | * |
||
295 | * @param string $value The value to throw an error with if doesn't exist |
||
296 | * @param string $variable The variable name to get |
||
297 | * @param bool $variable_check Are you checking to only see if a variable exists and not to throw an exception |
||
298 | * |
||
299 | * @throws \M1\Env\Exception\ParseException If the variable can not be found and `$variable_check` is false |
||
300 | * |
||
301 | * @return bool Does the variable exist |
||
302 | */ |
||
303 | 9 | private function checkVariableExists($value, $variable, $variable_check = false) |
|
313 | 9 | ||
314 | 9 | /** |
|
315 | * Checks to see if a variable exists |
||
316 | 9 | * |
|
317 | * @param string $variable The variable name to get |
||
318 | * |
||
319 | * @return bool |
||
320 | */ |
||
321 | private function hasVariable($variable) |
||
333 | |||
334 | /** |
||
335 | * Get variable value |
||
336 | * |
||
337 | * @param string $variable |
||
338 | * |
||
339 | * @return mixed |
||
340 | */ |
||
341 | private function getVariable($variable) |
||
353 | |||
354 | /** |
||
355 | * Do the variable replacements |
||
356 | * |
||
357 | * @param string $value The value to throw an error with if doesn't exist |
||
358 | * @param array $matches The matches of the variables |
||
359 | * @param bool $quoted_string Is the value in a quoted string |
||
360 | * |
||
361 | * @return string The parsed value |
||
362 | */ |
||
363 | public function doReplacements($value, $matches, $quoted_string) |
||
378 | } |
||
379 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.