Conditions | 17 |
Paths | 230 |
Total Lines | 95 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
37 | public static function Get( $less_files, $parser_options = array(), $modify_vars = array() ){ |
||
38 | |||
39 | |||
40 | //check $cache_dir |
||
41 | if( isset($parser_options['cache_dir']) ){ |
||
42 | Less_Cache::$cache_dir = $parser_options['cache_dir']; |
||
43 | } |
||
44 | |||
45 | if( empty(Less_Cache::$cache_dir) ){ |
||
46 | throw new Exception('cache_dir not set'); |
||
47 | } |
||
48 | |||
49 | if( isset($parser_options['prefix']) ){ |
||
50 | Less_Cache::$prefix = $parser_options['prefix']; |
||
51 | } |
||
52 | |||
53 | if( empty(Less_Cache::$prefix) ){ |
||
54 | throw new Exception('prefix not set'); |
||
55 | } |
||
56 | |||
57 | if( isset($parser_options['prefix_vars']) ){ |
||
58 | Less_Cache::$prefix_vars = $parser_options['prefix_vars']; |
||
59 | } |
||
60 | |||
61 | if( empty(Less_Cache::$prefix_vars) ){ |
||
62 | throw new Exception('prefix_vars not set'); |
||
63 | } |
||
64 | |||
65 | self::CheckCacheDir(); |
||
66 | $less_files = (array)$less_files; |
||
67 | |||
68 | |||
69 | //create a file for variables |
||
70 | if( !empty($modify_vars) ){ |
||
71 | $lessvars = Less_Parser::serializeVars($modify_vars); |
||
72 | $vars_file = Less_Cache::$cache_dir . Less_Cache::$prefix_vars . sha1($lessvars) . '.less'; |
||
73 | |||
74 | if( !file_exists($vars_file) ){ |
||
75 | file_put_contents($vars_file, $lessvars); |
||
76 | } |
||
77 | |||
78 | $less_files += array($vars_file => '/'); |
||
79 | } |
||
80 | |||
81 | |||
82 | // generate name for compiled css file |
||
83 | $hash = md5(json_encode($less_files)); |
||
84 | $list_file = Less_Cache::$cache_dir . Less_Cache::$prefix . $hash . '.list'; |
||
85 | |||
86 | |||
87 | // check cached content |
||
88 | if( !isset($parser_options['use_cache']) || $parser_options['use_cache'] === true ){ |
||
89 | if( file_exists($list_file) ){ |
||
90 | |||
91 | self::ListFiles($list_file, $list, $cached_name); |
||
92 | $compiled_name = self::CompiledName($list); |
||
93 | |||
94 | // if $cached_name is the same as the $compiled name, don't regenerate |
||
95 | if( !$cached_name || $cached_name === $compiled_name ){ |
||
96 | |||
97 | $output_file = self::OutputFile($compiled_name, $parser_options ); |
||
98 | |||
99 | if( $output_file && file_exists($output_file) ){ |
||
100 | @touch($list_file); |
||
101 | return basename($output_file); // for backwards compatibility, we just return the name of the file |
||
102 | } |
||
103 | } |
||
104 | } |
||
105 | } |
||
106 | |||
107 | $compiled = self::Cache( $less_files, $parser_options ); |
||
108 | if( !$compiled ){ |
||
109 | return false; |
||
110 | } |
||
111 | |||
112 | $compiled_name = self::CompiledName( $less_files ); |
||
113 | $output_file = self::OutputFile($compiled_name, $parser_options ); |
||
114 | |||
115 | |||
116 | //save the file list |
||
117 | $list = $less_files; |
||
118 | $list[] = $compiled_name; |
||
119 | $cache = implode("\n",$list); |
||
120 | file_put_contents( $list_file, $cache ); |
||
121 | |||
122 | |||
123 | //save the css |
||
124 | file_put_contents( $output_file, $compiled ); |
||
125 | |||
126 | |||
127 | //clean up |
||
128 | self::CleanCache(); |
||
129 | |||
130 | return basename($output_file); |
||
131 | } |
||
132 | |||
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.