Completed
Push — master ( 8c9cd6...ef07c2 )
by Adam
08:13
created

Byte::getUnit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace BestServedCold\PhalueObjects\Format;
4
5
use BestServedCold\PhalueObjects\ValueObject;
6
7
abstract class Byte extends ValueObject
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...
8
{
9
    private $units = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
10
    protected $power;
11
12
    public static function fromBytes($bytes)
13
    {
14
        return new static($bytes);
15
    }
16
17
    /**
18
     * @return float
19
     */
20
    private function base()
21
    {
22
        return log($this->getValue(), $this->power);
23
    }
24
25
    public function getUnit()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
26
    {
27
        return $this->units[ (int) floor($this->base()) ];
28
    }
29
30
    public function power($precision = 2)
31
    {
32
        return round(pow(1024, $this->base() - floor($this->base())), $precision);
33
    }
34
35
    public function __toString()
36
    {
37
        return $this->getValue() ? $this->power() . ' ' . $this->getUnit() : '0 bytes';
38
    }      
39
}
40