Complex classes like Generator 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.
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 Generator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class Generator extends Configurable |
||
24 | { |
||
25 | /** |
||
26 | * What version of source map does the generator generate? |
||
27 | */ |
||
28 | const VERSION = 3; |
||
29 | |||
30 | /** |
||
31 | * Array of default options. |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $defaultOptions = [ |
||
36 | // an optional source root, useful for relocating source files |
||
37 | // on a server or removing repeated values in the 'sources' entry. |
||
38 | // This value is prepended to the individual entries in the 'source' field. |
||
39 | 'sourceRoot' => '', |
||
40 | // an optional name of the generated code that this source map is associated with. |
||
41 | 'filename' => null, |
||
42 | // url of the map |
||
43 | 'url' => null, |
||
44 | // absolute path to a file to write the map to |
||
45 | 'write_to' => null, |
||
46 | // output source contents? |
||
47 | 'source_contents' => false, |
||
48 | // base path for filename normalization |
||
49 | 'base_path' => '', |
||
50 | // encode inline map using base64? |
||
51 | 'inline_encode_base64' => true, |
||
52 | ]; |
||
53 | |||
54 | /** |
||
55 | * The base64 VLQ encoder. |
||
56 | * |
||
57 | * @var Base64VLQ |
||
58 | */ |
||
59 | protected $encoder; |
||
60 | |||
61 | /** |
||
62 | * Array of mappings. |
||
63 | * |
||
64 | * @var array |
||
65 | */ |
||
66 | protected $mappings = []; |
||
67 | |||
68 | /** |
||
69 | * The root node. |
||
70 | * |
||
71 | * @var RulesetNode |
||
72 | */ |
||
73 | protected $root; |
||
74 | |||
75 | /** |
||
76 | * Array of contents map. |
||
77 | * |
||
78 | * @var array |
||
79 | */ |
||
80 | protected $contentsMap = []; |
||
81 | |||
82 | /** |
||
83 | * File to content map. |
||
84 | * |
||
85 | * @var array |
||
86 | */ |
||
87 | protected $sources = []; |
||
88 | |||
89 | /** |
||
90 | * Constructor. |
||
91 | * |
||
92 | * @param RulesetNode $root The root node |
||
93 | * @param array $contentsMap Array of file contents map |
||
94 | * @param array $options Array of options |
||
95 | * @param Base64VLQ $encoder The encoder |
||
96 | */ |
||
97 | public function __construct( |
||
108 | |||
109 | /** |
||
110 | * Setups the generator. |
||
111 | */ |
||
112 | protected function setup() |
||
119 | |||
120 | /** |
||
121 | * Generates the CSS. |
||
122 | * |
||
123 | * @param Context $context |
||
124 | * |
||
125 | * @return string |
||
126 | */ |
||
127 | public function generateCSS(Context $context) |
||
179 | |||
180 | /** |
||
181 | * Saves the source map to a file. |
||
182 | * |
||
183 | * @param string $file The absolute path to a file |
||
184 | * @param string $content The content to write |
||
185 | * |
||
186 | * @throws IOException If the file could not be saved |
||
187 | * @throws InvalidArgumentException If the directory to write the map to does not exist or is not writable |
||
188 | * |
||
189 | * @return true |
||
190 | */ |
||
191 | protected function saveMap($file, $content) |
||
206 | |||
207 | /** |
||
208 | * Normalizes the filename. |
||
209 | * |
||
210 | * @param string $filename |
||
211 | * |
||
212 | * @return string |
||
213 | */ |
||
214 | protected function normalizeFilename($filename) |
||
229 | |||
230 | /** |
||
231 | * Adds a mapping. |
||
232 | * |
||
233 | * @param int $generatedLine The line number in generated file |
||
234 | * @param int $generatedColumn The column number in generated file |
||
235 | * @param int $originalLine The line number in original file |
||
236 | * @param int $originalColumn The column number in original file |
||
237 | * @param string $sourceFile The original source file |
||
238 | * |
||
239 | * @return Generator |
||
240 | */ |
||
241 | public function addMapping( |
||
258 | |||
259 | /** |
||
260 | * Clear the mappings. |
||
261 | * |
||
262 | * @return Generator |
||
263 | */ |
||
264 | public function clear() |
||
270 | |||
271 | /** |
||
272 | * Sets the encoder. |
||
273 | * |
||
274 | * @param Base64VLQ $encoder |
||
275 | * |
||
276 | * @return Generator |
||
277 | */ |
||
278 | public function setEncoder(Base64VLQ $encoder) |
||
284 | |||
285 | /** |
||
286 | * Returns the encoder. |
||
287 | * |
||
288 | * @return Base64VLQ |
||
289 | */ |
||
290 | public function getEncoder() |
||
294 | |||
295 | /** |
||
296 | * Generates the JSON source map. |
||
297 | * |
||
298 | * @return string |
||
299 | * |
||
300 | * @see https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit# |
||
301 | */ |
||
302 | protected function generateJson() |
||
334 | |||
335 | /** |
||
336 | * Returns the sources contents. |
||
337 | * |
||
338 | * @return array|null |
||
339 | */ |
||
340 | protected function getSourcesContent() |
||
349 | |||
350 | /** |
||
351 | * Generates the mappings string. |
||
352 | * |
||
353 | * @return string |
||
354 | */ |
||
355 | public function generateMappings() |
||
405 | |||
406 | /** |
||
407 | * Finds the index for the filename. |
||
408 | * |
||
409 | * @param string $filename |
||
410 | * |
||
411 | * @return int|false |
||
412 | */ |
||
413 | protected function findFileIndex($filename) |
||
417 | } |
||
418 |