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) |
|
187 | 9 | ||
188 | 9 | /** |
|
189 | * Fetches the parameter expansion type |
||
190 | 9 | * |
|
191 | * @param string $variable_name The parameter expansion type to get from this |
||
192 | 9 | * |
|
193 | 9 | * @return string The parameter expansion type |
|
194 | 9 | */ |
|
195 | private function fetchParameterExpansionType($variable_name) |
||
203 | |||
204 | /** |
||
205 | * Fetches the parameter type symbol |
||
206 | * |
||
207 | * @param string $variable_name The variable |
||
208 | * @param string $type The type of parameter expansion |
||
209 | 9 | * |
|
210 | * @return array The symbol and if there is a empty check |
||
211 | 9 | */ |
|
212 | private function fetchParameterExpansionSymbol($variable_name, $type) |
||
226 | |||
227 | /** |
||
228 | * Splits the parameter expansion into variable and default |
||
229 | * |
||
230 | * @param string $variable_name The variable name to split |
||
231 | * @param string $parameter_symbol The parameter expansion symbol |
||
232 | * |
||
233 | * @throws \M1\Env\Exception\ParseException If the parameter expansion if not valid syntax |
||
234 | * |
||
235 | * @return array The split variable and default value |
||
236 | 6 | */ |
|
237 | private function splitVariableDefault($variable_name, $parameter_symbol) |
||
251 | |||
252 | |||
253 | /** |
||
254 | * Parses and sets the variable and default if needed |
||
255 | 6 | * |
|
256 | * @param string $variable The variable to parse |
||
257 | 6 | * @param string $default The default value |
|
258 | * @param bool $exists Does the variable exist |
||
259 | 6 | * @param bool $empty Is there the variable empty if exists and the empty flag is set |
|
260 | 6 | * @param string $type The type of parameter expansion |
|
261 | 6 | * |
|
262 | * @return string The parsed value |
||
263 | 6 | */ |
|
264 | private function parseVariableParameter($variable, $default, $exists, $empty, $type) |
||
272 | |||
273 | /** |
||
274 | * Parses and sets the variable parameter to default |
||
275 | * |
||
276 | * @param string $variable The variable to parse |
||
277 | 15 | * @param string $default The default value |
|
278 | * @param bool $empty Is there the variable empty if exists and the empty flag is set |
||
279 | 15 | * @param string $type The type of parameter expansion |
|
280 | 9 | * |
|
281 | 6 | * @return string The parsed default value |
|
282 | */ |
||
283 | private function assignVariableParameterDefault($variable, $default, $empty, $type) |
||
293 | |||
294 | /** |
||
295 | * Checks to see if a variable exists |
||
296 | * |
||
297 | * @param string $variable The variable name to get |
||
298 | * |
||
299 | * @return bool |
||
300 | */ |
||
301 | private function hasVariable($variable) |
||
313 | 9 | ||
314 | 9 | /** |
|
315 | * Get variable value |
||
316 | 9 | * |
|
317 | * @param string $variable |
||
318 | * |
||
319 | * @return mixed |
||
320 | */ |
||
321 | private function getVariable($variable) |
||
333 | |||
334 | /** |
||
335 | * Do the variable replacements |
||
336 | * |
||
337 | * @param string $value The value to throw an error with if doesn't exist |
||
338 | * @param array $matches The matches of the variables |
||
339 | * @param bool $quoted_string Is the value in a quoted string |
||
340 | * |
||
341 | * @return string The parsed value |
||
342 | */ |
||
343 | public function doReplacements($value, $matches, $quoted_string) |
||
358 | } |
||
359 |