Completed
Branch master (dadb56)
by Michał
02:12
created

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php namespace nyx\utils;
2
3
// External dependencies
4
use nyx\core;
5
6
/**
7
 * Unit
8
 *
9
 * Utilities related to unit conversions within and between types.
10
 *
11
 * @package     Nyx\Utils\Units
12
 * @version     0.0.1
13
 * @author      Michal Chojnacki <[email protected]>
14
 * @copyright   2012-2016 Nyx Dev Team
15
 * @link        http://docs.muyo.io/nyx/utils/units.html
16
 */
17
class Unit
18
{
19
    /**
20
     * The traits of the Unit class.
21
     */
22
    use traits\StaticallyExtendable;
23
24
    /**
25
     * Converts a string that may contain size units (e.g. "64m", "128k") into an integer of bytes.
26
     *
27
     * @param   string  $string     The string to convert to an integer of bytes.
28
     * @return  int                 The number of bytes.
29
     */
30
    public static function sizeStringToBytes($string)
31
    {
32
        // '-1' happens more often than not to denote an "unlimited" or "not defined" size, so let's reduce some
33
        // overhead of doing the conversion if possible.
34
        if ('-1' === $string) {
35
            return -1;
36
        }
37
38
        $max = strtolower(ltrim($string, '+'));
39
40
        if (0 === strpos($max, '0x')) {
41
            $max = intval($max, 16);
42
        } elseif (0 === strpos($max, '0')) {
43
            $max = intval($max, 8);
44
        } else {
45
            $max = intval($max);
46
        }
47
48
        switch (substr($string, -1)) {
49
            case 't': $max *= 1024;
0 ignored issues
show
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...
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
50
            case 'g': $max *= 1024;
0 ignored issues
show
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...
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
51
            case 'm': $max *= 1024;
0 ignored issues
show
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...
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
52
            case 'k': $max *= 1024;
0 ignored issues
show
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...
53
        }
54
55
        return $max;
56
    }
57
}
58