OS   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 6
c 4
b 0
f 2
lcom 0
cbo 1
dl 0
loc 41
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isWindows() 0 3 1
B hasBinary() 0 21 5
1
<?php
2
namespace nochso\Omni;
3
4
/**
5
 * OS.
6
 */
7
class OS {
8
	/**
9
	 * isWindows returns true if the current OS is Windows.
10
	 *
11
	 * @param string $phpOs Optional, defaults to the PHP_OS constant.
12
	 *
13
	 * @return bool
14
	 */
15
	public static function isWindows($phpOs = PHP_OS) {
16
		return strtolower(substr($phpOs, 0, 3)) === 'win';
17
	}
18
19
	/**
20
	 * hasBinary returns true if the binary is available in any of the PATHs.
21
	 *
22
	 * @param string $binaryName
23
	 *
24
	 * @return bool
25
	 */
26
	public static function hasBinary($binaryName) {
27
		$exec = Exec::create();
28
		if (self::isWindows()) {
29
			// 'where.exe' uses 0 for success, 1 for failure to find
30
			$exec->run('where.exe', '/q', $binaryName);
31
			return $exec->getStatus() === 0;
32
		}
33
		// 'which' uses 0 for success, 1 for failure to find
34
		$exec->run('which', $binaryName);
35
		if ($exec->getStatus() === 0) {
36
			return true;
37
		}
38
		// 'whereis' does not use status codes. Check for a matching line instead.
39
		$exec->run('whereis', '-b', $binaryName);
40
		foreach ($exec->getOutput() as $line) {
41
			if (preg_match('/^' . preg_quote($binaryName) . ': .*$/', $line) === 1) {
42
				return true;
43
			}
44
		}
45
		return false;
46
	}
47
}
48