Complex classes like Less_Cache 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 Less_Cache, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Less_Cache{ |
||
13 | |||
14 | // directory less.php can use for storing data |
||
15 | public static $cache_dir = false; |
||
16 | |||
17 | // prefix for the storing data |
||
18 | public static $prefix = 'lessphp_'; |
||
19 | |||
20 | // prefix for the storing vars |
||
21 | public static $prefix_vars = 'lessphpvars_'; |
||
22 | |||
23 | // specifies the number of seconds after which data created by less.php will be seen as 'garbage' and potentially cleaned up |
||
24 | public static $gc_lifetime = 604800; |
||
25 | |||
26 | |||
27 | /** |
||
28 | * Save and reuse the results of compiled less files. |
||
29 | * The first call to Get() will generate css and save it. |
||
30 | * Subsequent calls to Get() with the same arguments will return the same css filename |
||
31 | * |
||
32 | * @param array $less_files Array of .less files to compile |
||
33 | * @param array $parser_options Array of compiler options |
||
34 | * @param array $modify_vars Array of variables |
||
35 | * @return string Name of the css file |
||
36 | */ |
||
37 | public static function Get( $less_files, $parser_options = array(), $modify_vars = array() ){ |
||
132 | |||
133 | /** |
||
134 | * Force the compiler to regenerate the cached css file |
||
135 | * |
||
136 | * @param array $less_files Array of .less files to compile |
||
137 | * @param array $parser_options Array of compiler options |
||
138 | * @param array $modify_vars Array of variables |
||
139 | * @return string Name of the css file |
||
140 | */ |
||
141 | public static function Regen( $less_files, $parser_options = array(), $modify_vars = array() ){ |
||
142 | $parser_options['use_cache'] = false; |
||
143 | return self::Get( $less_files, $parser_options, $modify_vars ); |
||
144 | } |
||
145 | |||
146 | public static function Cache( &$less_files, $parser_options = array() ){ |
||
147 | |||
148 | |||
149 | // get less.php if it exists |
||
150 | $file = dirname(__FILE__) . '/Less.php'; |
||
151 | if( file_exists($file) && !class_exists('Less_Parser') ){ |
||
152 | require_once($file); |
||
153 | } |
||
154 | |||
155 | $parser_options['cache_dir'] = Less_Cache::$cache_dir; |
||
156 | $parser = new Less_Parser($parser_options); |
||
157 | |||
158 | |||
159 | // combine files |
||
160 | foreach($less_files as $file_path => $uri_or_less ){ |
||
161 | |||
162 | //treat as less markup if there are newline characters |
||
163 | if( strpos($uri_or_less,"\n") !== false ){ |
||
164 | $parser->Parse( $uri_or_less ); |
||
165 | continue; |
||
166 | } |
||
167 | |||
168 | $parser->ParseFile( $file_path, $uri_or_less ); |
||
169 | } |
||
170 | |||
171 | $compiled = $parser->getCss(); |
||
172 | |||
173 | |||
174 | $less_files = $parser->allParsedFiles(); |
||
175 | |||
176 | return $compiled; |
||
177 | } |
||
178 | |||
179 | |||
180 | private static function OutputFile( $compiled_name, $parser_options ){ |
||
195 | |||
196 | |||
197 | private static function CompiledName( $files ){ |
||
207 | |||
208 | |||
209 | public static function SetCacheDir( $dir ){ |
||
210 | Less_Cache::$cache_dir = $dir; |
||
211 | } |
||
212 | |||
213 | public static function CheckCacheDir(){ |
||
232 | |||
233 | |||
234 | /** |
||
235 | * Delete unused less.php files |
||
236 | * |
||
237 | */ |
||
238 | public static function CleanCache(){ |
||
296 | |||
297 | |||
298 | /** |
||
299 | * Get the list of less files and generated css file from a list file |
||
300 | * |
||
301 | */ |
||
302 | static function ListFiles($list_file, &$list, &$css_file_name ){ |
||
315 | |||
316 | } |
||
317 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.