Test Failed
Push — master ( f3b9bd...3b5313 )
by Jean-Christophe
23:33
created

CodeUtils   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 44
eloc 94
c 0
b 0
f 0
dl 0
loc 143
rs 8.8798

7 Methods

Rating   Name   Duplication   Size   Complexity  
B cleanParameters() 0 27 8
A checkVar() 0 8 3
A indent() 0 4 1
A isValidCode() 0 19 5
A unCheckVar() 0 8 3
B getParametersForRoute() 0 20 7
C getPHPExecutable() 0 38 17

How to fix   Complexity   

Complex Class

Complex classes like CodeUtils often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use CodeUtils, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Ubiquity\utils\base;
4
5
/**
6
 * Ubiquity\utils\base$CodeUtils
7
 * This class is part of Ubiquity
8
 *
9
 * @author jcheron <[email protected]>
10
 * @version 1.0.0
11
 *
12
 */
13
class CodeUtils {
14
15
	public static function cleanParameters($parameters) {
16
		$optional = false;
17
		$tmpResult = [ ];
18
		$params = \explode ( ",", $parameters );
19
		foreach ( $params as $param ) {
20
			$param = \trim ( $param );
21
			$list = \explode ( "=", $param );
22
			if (isset ( $list [0] )) {
23
				$var = $list [0];
24
			}
25
			if (isset ( $list [1] )) {
26
				$value = $list [1];
27
			}
28
			if (isset ( $var ) && isset ( $value )) {
29
				$value = \trim ( $value );
30
				$var = self::checkVar ( $var );
31
				$tmpResult [] = $var . '=' . $value;
32
				$optional = true;
33
			} elseif (isset ( $var )) {
34
				$var = self::checkVar ( $var );
35
				if ($optional)
36
					$tmpResult [] = $var . "=''";
37
				else
38
					$tmpResult [] = $var;
39
			}
40
		}
41
		return \implode ( ',', $tmpResult );
42
	}
43
44
	public static function getParametersForRoute($parameters) {
45
		$tmpResult = [ ];
46
		$params = \explode ( ",", $parameters );
47
		foreach ( $params as $param ) {
48
			$param = \trim ( $param );
49
			$list = \explode ( "=", $param );
50
			if (isset ( $list [0] )) {
51
				$var = $list [0];
52
			}
53
			if (isset ( $list [1] )) {
54
				$value = $list [1];
55
			}
56
			if (isset ( $var ) && isset ( $value )) {
57
				break;
58
			} elseif (isset ( $var )) {
59
				$var = self::unCheckVar ( $var );
60
				$tmpResult [] = '{' . $var . '}';
61
			}
62
		}
63
		return $tmpResult;
64
	}
65
66
	public static function checkVar($var, $prefix = '$') {
67
		if (UString::isNull ( $var ))
68
			return "";
69
		$var = \trim ( $var );
70
		if (! UString::startswith ( $var, $prefix )) {
71
			$var = $prefix . $var;
72
		}
73
		return $var;
74
	}
75
76
	public static function unCheckVar($var, $prefix = '$') {
77
		if (UString::isNull ( $var ))
78
			return "";
79
		$var = \trim ( $var );
80
		if (UString::startswith ( $var, $prefix )) {
81
			$var = \substr ( $var, \sizeof ( $prefix ) );
82
		}
83
		return $var;
84
	}
85
86
	public static function indent($code, $count = 2) {
87
		$tab = \str_repeat ( "\t", $count );
88
		$lines = \explode ( "\n", $code );
89
		return $tab . \implode ( $tab, $lines );
90
	}
91
92
	public static function isValidCode($code) {
93
		$output = [ ];
94
		$result = 1;
95
		$temp_file = tempnam ( sys_get_temp_dir (), 'Tux' );
96
		$fp = fopen ( $temp_file, "w" );
97
		fwrite ( $fp, $code );
1 ignored issue
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

97
		fwrite ( /** @scrutinizer ignore-type */ $fp, $code );
Loading history...
98
		fclose ( $fp );
1 ignored issue
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

98
		fclose ( /** @scrutinizer ignore-type */ $fp );
Loading history...
99
		if (file_exists ( $temp_file )) {
100
			$phpExe = self::getPHPExecutable ();
101
			if (isset ( $phpExe )) {
102
				exec ( $phpExe . ' -l ' . $temp_file, $output, $result );
103
			}
104
			$output = implode ( "", $output );
105
			\unlink ( $temp_file );
106
			if (strpos ( $output, 'No syntax errors detected' ) === false && $result !== 1) {
107
				return false;
108
			}
109
		}
110
		return true;
111
	}
112
113
	/**
114
	 *
115
	 * @see https://stackoverflow.com/questions/2372624/get-current-php-executable-from-within-script
116
	 * @return string
117
	 */
118
	public static function getPHPExecutable() {
119
		if (defined ( 'PHP_BINARY' ) && PHP_BINARY && in_array ( PHP_SAPI, array ('cli','cli-server' ) ) && is_file ( PHP_BINARY )) {
120
			return PHP_BINARY;
121
		} else if (strtoupper ( substr ( PHP_OS, 0, 3 ) ) === 'WIN') {
122
			$paths = explode ( PATH_SEPARATOR, getenv ( 'PATH' ) );
123
			foreach ( $paths as $path ) {
124
				if (substr ( $path, strlen ( $path ) - 1 ) == \DS) {
125
					$path = substr ( $path, 0, strlen ( $path ) - 1 );
126
				}
127
				if (substr ( $path, strlen ( $path ) - strlen ( 'php' ) ) == 'php') {
128
					$response = $path . \DS . 'php.exe';
129
					if (is_file ( $response )) {
130
						return $response;
131
					}
132
				} else if (substr ( $path, strlen ( $path ) - strlen ( 'php.exe' ) ) == 'php.exe') {
133
					if (is_file ( $response )) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $response seems to be defined later in this foreach loop on line 128. Are you sure it is defined here?
Loading history...
134
						return $response;
135
					}
136
				}
137
			}
138
		} else {
139
			$paths = explode ( PATH_SEPARATOR, getenv ( 'PATH' ) );
140
			foreach ( $paths as $path ) {
141
				if (substr ( $path, strlen ( $path ) - 1 ) == \DS) {
142
					$path = substr ( $path, strlen ( $path ) - 1 );
143
				}
144
				if (substr ( $path, strlen ( $path ) - strlen ( 'php' ) ) == 'php') {
145
					if (is_file ( $path )) {
146
						return $path;
147
					}
148
					$response = $path . \DS . 'php';
149
					if (is_file ( $response )) {
150
						return $response;
151
					}
152
				}
153
			}
154
		}
155
		return null;
156
	}
157
}
158