| Conditions | 15 |
| Paths | 576 |
| Total Lines | 75 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 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 |
||
| 40 | function execute($dir) { |
||
| 41 | echo "Processing : {$dir}\n"; |
||
| 42 | |||
| 43 | // parse config file if it exists |
||
| 44 | echo " Finding predefined configuration file..."; |
||
| 45 | $config = read_config($dir); |
||
| 46 | echo " Done\n"; |
||
| 47 | |||
| 48 | // merge |
||
| 49 | foreach($config['merge'] as $target=>$files) { |
||
| 50 | merge($files, $target, $dir); |
||
| 51 | } |
||
| 52 | |||
| 53 | // files to skip |
||
| 54 | $files_to_skip = $config['skip']; |
||
| 55 | foreach($files_to_skip as $idx=>$file) { |
||
| 56 | if($file) $files_to_skip[$idx] = realpath($dir.trim($file)); |
||
| 57 | } |
||
| 58 | |||
| 59 | echo " Minifying JavaScript files..."; |
||
| 60 | $js_files = get_target_files('js', $dir, $files_to_skip); |
||
| 61 | |||
| 62 | if(count($js_files) && !class_exists('JSMinPlus')) { |
||
| 63 | require dirname(__FILE__).'/jsminplus/jsminplus.php'; |
||
| 64 | } |
||
| 65 | foreach($js_files as $file) { |
||
| 66 | if(!is_readable($file)) continue; |
||
| 67 | |||
| 68 | $target = preg_replace('@\.js$@', '.min.js', $file); |
||
| 69 | $content = file_get_contents($file); |
||
| 70 | |||
| 71 | // save copyright to preserve it |
||
| 72 | if(preg_match('@^[ \t]*(/\*\*.+?\*/)@s', $content, $matches)) { |
||
| 73 | $copyright = $matches[1]."\n"; |
||
| 74 | } else { |
||
| 75 | $copyright = ''; |
||
| 76 | } |
||
| 77 | |||
| 78 | if($config['use_closure_compiler']) { |
||
| 79 | $content = closure_compile($content); |
||
| 80 | if(!$content) { |
||
| 81 | echo " CANNOT compile the js file with closure compiler.\n"; |
||
| 82 | echo " Trying again with JSMinPlus.\n"; |
||
| 83 | $content = JSMinPlus::minify($content); |
||
| 84 | } |
||
| 85 | } else { |
||
| 86 | $content = JSMinPlus::minify($content); |
||
| 87 | } |
||
| 88 | |||
| 89 | file_put_contents($target, $copyright.$content, LOCK_EX); |
||
| 90 | |||
| 91 | echo '.'; |
||
| 92 | } |
||
| 93 | echo " Done\n"; |
||
| 94 | |||
| 95 | echo " Minifying CSS files..."; |
||
| 96 | $css_files = get_target_files('css', $dir, $files_to_skip); |
||
| 97 | |||
| 98 | if(count($css_files) && !class_exists('CSSmin')) { |
||
| 99 | require dirname(__FILE__).'/cssmin/CSSmin.php'; |
||
| 100 | } |
||
| 101 | |||
| 102 | $oCSSmin = new CSSmin(); |
||
| 103 | |||
| 104 | foreach($css_files as $file) { |
||
| 105 | if(!is_readable($file)) continue; |
||
| 106 | |||
| 107 | $target = preg_replace('@\.css$@', '.min.css', $file); |
||
| 108 | $content = file_get_contents($file); |
||
| 109 | |||
| 110 | file_put_contents($target, $copyright.$oCSSmin->run($content), LOCK_EX); |
||
|
|
|||
| 111 | echo '.'; |
||
| 112 | } |
||
| 113 | echo " Done\n"; |
||
| 114 | } |
||
| 115 | |||
| 201 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: