CodeUtils::getPHPExecutable()   C
last analyzed

Complexity

Conditions 17
Paths 21

Size

Total Lines 38
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 97.7667

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 38
ccs 9
cts 26
cp 0.3462
rs 5.2166
c 0
b 0
f 0
cc 17
nc 21
nop 0
crap 97.7667

How to fix   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
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 1
	public static function cleanParameters($parameters) {
16 1
		$optional = false;
17 1
		$tmpResult = [ ];
18 1
		$params = \explode ( ",", $parameters );
19 1
		foreach ( $params as $param ) {
20 1
			$param = \trim ( $param );
21 1
			$list = \explode ( "=", $param );
22 1
			if (isset ( $list [0] )) {
23 1
				$var = $list [0];
24
			}
25 1
			if (isset ( $list [1] )) {
26 1
				$value = $list [1];
27
			}
28 1
			if (isset ( $var ) && isset ( $value )) {
29 1
				$value = \trim ( $value );
30 1
				$var = self::checkVar ( $var );
31 1
				$tmpResult [] = $var . '=' . $value;
32 1
				$optional = true;
33
			} elseif (isset ( $var )) {
34 1
				$var = self::checkVar ( $var );
35 1
				if ($optional)
36
					$tmpResult [] = $var . "=''";
37
				else
38 1
					$tmpResult [] = $var;
39
			}
40
		}
41 1
		return \implode ( ',', $tmpResult );
42
	}
43
44 1
	public static function getParametersForRoute($parameters) {
45 1
		$tmpResult = [ ];
46 1
		$params = \explode ( ",", $parameters );
47 1
		foreach ( $params as $param ) {
48 1
			$param = \trim ( $param );
49 1
			$list = \explode ( "=", $param );
50 1
			if (isset ( $list [0] )) {
51 1
				$var = $list [0];
52
			}
53 1
			if (isset ( $list [1] )) {
54 1
				$value = $list [1];
55
			}
56 1
			if (isset ( $var ) && isset ( $value )) {
57 1
				break;
58
			} elseif (isset ( $var )) {
59 1
				$var = self::unCheckVar ( $var );
60 1
				$tmpResult [] = '{' . $var . '}';
61
			}
62
		}
63 1
		return $tmpResult;
64
	}
65
66 1
	public static function checkVar($var, $prefix = '$') {
67 1
		if (UString::isNull ( $var ))
68
			return "";
69 1
		$var = \trim ( $var );
70 1
		if (! UString::startswith ( $var, $prefix )) {
71 1
			$var = $prefix . $var;
72
		}
73 1
		return $var;
74
	}
75
76 1
	public static function unCheckVar($var, $prefix = '$') {
77 1
		if (UString::isNull ( $var ))
78
			return '';
79 1
		$var = \trim ( $var );
80 1
		if (UString::startswith ( $var, $prefix )) {
81
			$var = \substr ( $var, \count ( $prefix ) );
82
		}
83 1
		return $var;
84
	}
85
86 1
	public static function indent($code, $count = 2) {
87 1
		$tab = \str_repeat ( "\t", $count );
88 1
		$lines = \explode ( "\n", $code );
89 1
		return $tab . \implode ( $tab, $lines );
90
	}
91
92 9
	public static function isValidCode($code) {
93 9
		$output = [ ];
94 9
		$result = 1;
95 9
		$temp_file = tempnam ( sys_get_temp_dir (), 'Tux' );
96 9
		$fp = \fopen ( $temp_file, 'w' );
97 9
		\fwrite ( $fp, $code );
98 9
		\fclose ( $fp );
99 9
		if (\file_exists ( $temp_file )) {
100 9
			$phpExe = self::getPHPExecutable ();
101 9
			if (isset ( $phpExe )) {
102 2
				exec ( $phpExe . ' -l ' . $temp_file, $output, $result );
103
			}
104 9
			$output = \implode ( '', $output );
105 9
			\unlink ( $temp_file );
106 9
			if (strpos ( $output, 'No syntax errors detected' ) === false && $result !== 1) {
107
				return false;
108
			}
109
		}
110 9
		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 9
	public static function getPHPExecutable() {
119 9
		if (defined ( 'PHP_BINARY' ) && PHP_BINARY && in_array ( PHP_SAPI, array ('cli','cli-server' ) ) && is_file ( PHP_BINARY )) {
120 2
			return PHP_BINARY;
121 7
		} 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 ( $path )) {
134
						return $path;
135
					}
136
				}
137
			}
138
		} else {
139 7
			$paths = \explode ( PATH_SEPARATOR, getenv ( 'PATH' ) );
140 7
			foreach ( $paths as $path ) {
141 7
				if (\substr ( $path, \strlen ( $path ) - 1 ) == \DS) {
142
					$path = substr ( $path, strlen ( $path ) - 1 );
143
				}
144 7
				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 7
		return null;
156
	}
157
}
158