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

Minify   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 0
dl 0
loc 28
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B minifyJavascript() 0 26 2
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