GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — develop ( 755201...07ce67 )
by gyeong-won
12:25
created

minify.php ➔ execute()   F

Complexity

Conditions 15
Paths 576

Size

Total Lines 75
Code Lines 45

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 15
eloc 45
nc 576
nop 1
dl 0
loc 75
rs 2.8266

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/* Copyright (C) NAVER <http://www.navercorp.com> */
3
/**
4
 * Minify
5
 * This script comnbines multiple JavaScript or CSS files with minifying.
6
 * PHP 5.3.0 or above version is required.
7
 *
8
 * Usage : php minify.php [TARGET_DIR ...]
9
 * TARGET_DIR use the current working directory as a default path.
10
 *
11
 * @author NAVER([email protected])
12
 */
13
14
if(version_compare(PHP_VERSION, '5.3.0', '<')) {
15
	echo "PHP 5.3.0 or above version is required.";
16
	exit(1);
17
}
18
19
function main() {
20
	$argv = $_SERVER['argv'];
21
	$argc = $_SERVER['argc'];
22
23
	// get target directories
24
	if($argc < 1) exit;
25
	elseif($argc < 2) $dirs = array($_SERVER['PWD']);
26
	else $dirs = array_slice($argv, 1);
27
28
	$dirs = array_map('realpath', $dirs);
29
	$dirs = array_map('add_dirsep', $dirs);
30
31
	array_walk($dirs, 'execute');
32
}
33
34
// add directory separator
35
function add_dirsep($path) {
36
	if(substr($path,-1) != DIRECTORY_SEPARATOR) $path .= DIRECTORY_SEPARATOR;
37
	return $path;
38
}
39
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);
0 ignored issues
show
Bug introduced by
The variable $copyright does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
111
		echo '.';
112
	}
113
	echo " Done\n";
114
}
115
116
function read_config($dir) {
117
	$default = array('option'=>array(), 'skip'=>array(), 'merge'=>array());
118
	$file    = $dir.'minify.ini';
119
120
	if(!is_readable($file)) return $default;
121
122
	$config_str = file_get_contents($file);
123
	$config_str = preg_replace_callback('/(\[(?:skip|merge *>> *.+?)\])([\s\S]+?)(?=\[|$)/', 'transform_config_str', $config_str);
124
125
	$config = parse_ini_string($config_str, 1);
126
	if($config === false) return $default;
127
128
	if(is_array($config['skip'])) $config['skip'] = array_keys($config['skip']);
129
130
	foreach($config as $section=>$value) {
131
		if(preg_match('/merge *>> *(.+)/', $section, $match)) {
132
			if(!is_array($config['merge'])) $config['merge'] = array();
133
			$config['merge'][trim($match[1])] = array_keys($value);
134
135
			unset($config[$section]);
136
		}
137
	}
138
139
	if(is_array($config['option'])) $config = array_merge($config['option'], $config);
140
	$config = array_merge($default, $config);
141
142
	return $config;
143
}
144
145
function transform_config_str($matches) {
146
	if(!$matches[2]) return $matches[0];
147
	$values = preg_replace('/$/m', '=', trim($matches[2]));
148
149
	return "{$matches[1]}\n{$values}\n\n";
150
}
151
152
function merge($files, $target, $base_dir) {
153
	if(!is_array($files)) return false;
154
155
	$body   = '';
156
	$is_css = !!preg_match('/\.css$/', $target);
157
158
	foreach($files as $file) {
159
		$file = $base_dir.trim($file);
160
		if(!is_readable($file)) continue;
161
162
		$content = trim(file_get_contents($file));
163
		if($is_css && $body) $content = preg_replace('/^@.+?;/m', '', $content);
164
		if($content) $body .= $content."\n";
165
	}
166
167
	if ($body) {
168
		$file_count = count($files);
169
		echo "  Merging {$file_count} files to create {$target} file...";
170
		file_put_contents($base_dir.$target, $body, LOCK_EX);
171
		echo " Done\n";
172
	}
173
}
174
175
function get_target_files($ext, $dir, $files_to_skip) {
176
	$files = glob("{$dir}*.{$ext}");
177
	$skips = glob("{$dir}*.min.{$ext}");
178
	$skips = array_merge($skips, $files_to_skip);
179
	$files = array_diff($files, $skips);
180
181
	return $files;
182
}
183
184
function closure_compile($content) {
185
	require_once dirname(__FILE__).'/../../classes/httprequest/XEHttpRequest.class.php';
186
187
	$req = new XEHttpRequest('closure-compiler.appspot.com', 80);
0 ignored issues
show
Deprecated Code introduced by
The class XEHttpRequest has been deprecated.

This class, trait or interface has been deprecated.

Loading history...
188
	$ret = $req->send('/compile', 'POST', 5, array(
189
		'output_info'   => 'compiled_code',
190
		'output_format' => 'text',
191
		'compilation_level' => 'SIMPLE_OPTIMIZATIONS',
192
		'js_code' => $content
193
	));
194
195
	return $ret->body;
196
}
197
198
// run main function
199
error_reporting(E_ALL & ~E_NOTICE);
200
main();
201