Completed
Push — master ( eb0184...954492 )
by Jean-Christophe
01:46
created

Minify::minifyJavascript()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 16
nc 2
nop 1
1
<?php
2
3
namespace Ubiquity\js;
4
5
class Minify {
6
	public static function minifyJavascript($input){
7
		if(trim($input) === "") return $input;
8
		$input= preg_replace(
9
				array(
10
						// Remove comment(s)
11
						'#\s*("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')\s*|\s*\/\*(?!\!|@cc_on)(?>[\s\S]*?\*\/)\s*|\s*(?<![\:\=])\/\/.*(?=[\n\r]|$)|^\s*|\s*$#',
12
						// Remove white-space(s) outside the string and regex
13
						'#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\'|\/\*(?>.*?\*\/)|\/(?!\/)[^\n\r]*?\/(?=[\s.,;]|[gimuy]|$))|\s*([!%&*\(\)\-=+\[\]\{\}|;:,.<>?\/])\s*#s',
14
						// Remove the last semicolon
15
						//'#;+\}#',
16
						// Minify object attribute(s) except JSON attribute(s). From `{'foo':'bar'}` to `{foo:'bar'}`
17
						'#([\{,])([\'])(\d+|[a-z_][a-z0-9_]*)\2(?=\:)#i',
18
						// --ibid. From `foo['bar']` to `foo.bar`
19
						'#([a-z0-9_\)\]])\[([\'"])([a-z_][a-z0-9_]*)\2\]#i'
20
				),
21
				array(
22
						'$1',
23
						'$1$2',
24
						//'}',
25
						'$1$3',
26
						'$1.$3'
27
				),
28
				$input);
29
		$input=str_replace("}$", "};$", $input);
30
		return $input;
31
	}
32
}
33
34