ExecDriver   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 5
Bugs 2 Features 0
Metric Value
wmc 14
c 5
b 2
f 0
lcom 1
cbo 3
dl 0
loc 85
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 4
A getFileSize() 0 9 4
A convertToInteger() 0 14 3
A getFileSizeWindows() 0 7 1
A getFileSizeLinux() 0 7 1
A getFileSizeMac() 0 7 1
1
<?php
2
3
namespace BigFileTools\Driver;
4
use Brick\Math\BigInteger;
5
use Brick\Math\Exception\ArithmeticException;
6
7
class ExecDriver implements ISizeDriver
8
{
9
	private $os;
10
	const OS_WINDOWS = "Windows";
11
	const OS_LINUX = "Linux";
12
	const OS_MAC = "Mac";
13
14
	public function __construct()
15
	{
16
		if (!function_exists("exec")) {
17
			throw new PrerequisiteException("Exec function is disabled");
18
		}
19
20
		if(strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
21
			$this->os = self::OS_WINDOWS;
22
		} elseif (strtoupper(PHP_OS) == "DARWIN") {
23
			$this->os = self::OS_MAC;
24
		} else {
25
			$this->os = self::OS_LINUX;
26
		}
27
	}
28
29
	/**
30
	 * Returns file size by using system shell/cmd commands
31
	 * @inheritdoc
32
	 * @link http://stackoverflow.com/questions/5501451/php-x86-how-to-get-filesize-of-2gb-file-without-external-program/5502328#5502328
33
	 * @return BigInteger
34
	 */
35
	public function getFileSize($path)
36
	{
37
		switch($this->os) {
38
			case self::OS_WINDOWS: return $this->getFileSizeWindows($path); break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
39
			case self::OS_LINUX:   return $this->getFileSizeLinux($path); break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
40
			case self::OS_MAC:     return $this->getFileSizeMac($path); break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
41
			default: throw new Exception("OS not detected");
0 ignored issues
show
Coding Style introduced by
The default body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a default statement must start on the line immediately following the statement.

switch ($expr) {
    default:
        doSomething(); //right
        break;
}


switch ($expr) {
    default:

        doSomething(); //wrong
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
42
		}
43
	}
44
45
	/**
46
	 * Convert string into integer
47
	 * Must be precise number, otherwise you will see and exception.
48
	 *
49
	 * @param $valueAsString
50
	 * @return BigInteger
51
	 * @throws Exception
52
	 */
53
	private function convertToInteger($valueAsString) {
54
		if(!is_string($valueAsString)) {
55
			throw new Exception("Cannot convert to integer. Expected string, but got " . gettype($valueAsString). ".");
56
		}
57
		$trimmedInput = trim($valueAsString);
58
59
		try {
60
			return BigInteger::of($trimmedInput);
61
62
		} catch (ArithmeticException $e) {
63
			throw new Exception("Returned value cannot be converted to an integer.",0, $e);
64
		}
65
66
	}
67
68
	private function getFileSizeWindows($path)
69
	{
70
		$escapedPath = escapeshellarg($path);
71
		return $this->convertToInteger(
72
			exec("for %F in ($escapedPath) do @echo %~zF")
73
		);
74
	}
75
76
	private function getFileSizeLinux($path)
77
	{
78
		$escapedPath = escapeshellarg($path);
79
		return $this->convertToInteger(
80
			exec("stat -Lc%s $escapedPath")
81
		);
82
	}
83
84
	private function getFileSizeMac($path)
85
	{
86
		$escapedPath = escapeshellarg($path);
87
		return $this->convertToInteger(
88
			exec("stat -f%z $escapedPath")
89
		);
90
	}
91
}