Byte   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 7
dl 0
loc 90
ccs 23
cts 23
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 4 1
A fromString() 0 12 1
A toString() 0 4 2
A getPower() 0 4 1
A base() 0 4 1
A getUnitKeyList() 0 4 1
A getUnit() 0 6 2
1
<?php
2
3
namespace BestServedCold\PhalueObjects\Format;
4
5
use BestServedCold\PhalueObjects\Contract\VOStringable;
6
use BestServedCold\PhalueObjects\VOArray\Find;
7
use BestServedCold\PhalueObjects\VOArray\Map;
8
use BestServedCold\PhalueObjects\VOClosure\Value;
9
use BestServedCold\PhalueObjects\VOFloat;
10
use BestServedCold\PhalueObjects\VOString;
11
use BestServedCold\PhalueObjects\VOString\Word;
12
use BestServedCold\PhalueObjects\VOString\Mixin as VOStringTrait;
13
14
/**
15
 * Class Byte
16
 *
17
 * @package BestServedCold\PhalueObjects\Format
18
 */
19
abstract class Byte extends VOFloat implements VOStringable
0 ignored issues
show
Coding Style introduced by
Byte does not seem to conform to the naming convention (^Abstract|Factory$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
20
{
21
    use VOStringTrait;
22
23
    const UNITS = [
24
        [ 'Byte' => 'Byte' ],
25
        [ 'KB'   => 'Kilobyte' ],
26
        [ 'MB'   => 'Megabyte' ],
27
        [ 'GB'   => 'Gigabyte' ],
28
        [ 'TB'   => 'Terabyte' ],
29
        [ 'PB'   => 'Pecabyte' ],
30
        [ 'EB'   => 'Exabyte' ],
31
        [ 'ZB'   => 'Zettabyte' ],
32
        [ 'YB'   => 'Yottabyte' ]
33
    ];
34
35
    /**
36
     * @var integer $power
37
     */
38
    protected static $power;
39
40
    /**
41
     * @return string
42
     */
43 3
    public function __toString()
44
    {
45 3
        return $this->toString();
46
    }
47
48
    /**
49
     * @param  string $string
50
     * @return static
51
     */
52 2
    public static function fromString($string)
53
    {
54 2
        $voString = VOString::fromString($string);
55
56 2
        return new static(
57 2
            $voString->getNumbers() *
58 2
            pow(
59 2
                static::$power,
60 2
                Find::fromArray(self::getUnitKeyList())->keyFromArrayValue($voString->getLetters())
61 2
            )
62 2
        );
63
    }
64
65
    /**
66
     * @return string
67
     */
68 3
    public function toString()
69
    {
70 3
        return $this->getValue() ? $this->getPower().' '.$this->getUnit() : '0 Bytes';
71
    }
72
73
    /**
74
     * @param  null   $power
75
     * @return string
76
     */
77 4
    public function getUnit($power = null)
78
    {
79 4
        $power = $power ?: $this->getPower();
80 4
        $unit  = self::UNITS[ (int) floor($this->base()) ];
81 4
        return Word::fromString(reset($unit))->getPluralised($power);
82
    }
83
84
    /**
85
     * @param  int $precision
86
     * @return float
87
     */
88 4
    public function getPower($precision = 2)
89
    {
90 4
        return round(pow(static::$power, $this->base() - floor($this->base())), $precision);
91
    }
92
93
    /**
94
     * @return float
95
     */
96 4
    private function base()
97
    {
98 4
        return log($this->getValue(), static::$power);
99
    }
100
101
    /**
102
     * @return mixed
103
     */
104 2
    private static function getUnitKeyList()
105
    {
106 2
        return Map::fromVariadic(Value::toArrayWithPlural(true), self::UNITS)->getValue();
107
    }
108
}
109