Complex classes like AssetMacro 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 AssetMacro, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | 1 | class AssetMacro extends MacroSet |
|
19 | { |
||
20 | |||
21 | const CONFIG_PROVIDER = 'assetMacroConfig'; |
||
22 | |||
23 | |||
24 | /** |
||
25 | * @param Latte\Compiler $compiler |
||
26 | */ |
||
27 | 1 | public static function install(Latte\Compiler $compiler) |
|
32 | |||
33 | |||
34 | /** |
||
35 | * @param Latte\MacroNode $node |
||
36 | * @param Latte\PhpWriter $writer |
||
37 | * @return string |
||
38 | * @throws Latte\CompileException |
||
39 | */ |
||
40 | 1 | public function macroAsset(Latte\MacroNode $node, Latte\PhpWriter $writer) |
|
57 | |||
58 | /** |
||
59 | * @param string $asset Asset relative path |
||
60 | * @param array $args Other macro arguments |
||
61 | * @param string $basePath Base path |
||
62 | * @param array $config Macro configuration |
||
63 | * @return string |
||
64 | */ |
||
65 | 1 | public static function resolveAssetPath($asset, array $args, $basePath, array $config) { |
|
88 | |||
89 | /** |
||
90 | * @param string $format Output format |
||
91 | * @param string $basePath Base path |
||
92 | * @param string $path Asset relative path |
||
93 | * @param string|null $revision Asset revision (version or path to file) |
||
94 | * @param bool $isVersion Is revision only version or full path? |
||
95 | * @return string |
||
96 | */ |
||
97 | private static function formatOutput($format, $basePath, $path, $revision, $isVersion) |
||
127 | |||
128 | /** |
||
129 | * @param string $asset Asset path specified in macro |
||
130 | * @param array $args Macro arguments |
||
131 | * @return array |
||
132 | */ |
||
133 | 1 | private static function processArguments($asset, array $args) |
|
146 | |||
147 | /** |
||
148 | * @param string $asset Asset path specified in macro |
||
149 | * @param bool $need Fail if manifest doesn't exist? |
||
150 | * @param string $wwwDir Public www dir |
||
151 | * @param array $config Macro configuration |
||
152 | * @return null|array |
||
153 | */ |
||
154 | 1 | private static function resolveManifest($asset, $need, $wwwDir, array $config) { |
|
175 | |||
176 | /** |
||
177 | * @param string $asset Asset path specified in macro |
||
178 | * @param string $wwwDir Public www dir |
||
179 | * @param bool $need Fail if asset/manifest doesn't exist? |
||
180 | * @param array $config Macro configuration |
||
181 | * @return null|array |
||
182 | */ |
||
183 | 1 | private static function autodetectManifest($asset, $wwwDir, $need, array $config) { |
|
209 | |||
210 | /** |
||
211 | * @param null|array $manifest Array of revisions |
||
212 | * @param string $path Asset path |
||
213 | * @param bool $need Fail if revision doesn't exist? |
||
214 | * @param array $config Macro configuration |
||
215 | * @return null|string |
||
216 | */ |
||
217 | 1 | private static function resolveRevision($manifest, $path, $need, array $config) { |
|
227 | |||
228 | |||
229 | /** |
||
230 | * @param \Exception $e |
||
231 | * @param string $action |
||
232 | * @param bool $need |
||
233 | * @throws \Exception |
||
234 | */ |
||
235 | 1 | private static function throwError(\Exception $e, $action = 'exception', $need = TRUE) |
|
246 | |||
247 | 1 | private static function normalizePath($path, $separator = '\\/') |
|
270 | |||
271 | } |
||
272 |