FileSize::addSize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php namespace Arcanedev\Units\Measures;
2
3
use Arcanedev\Units\Bases\UnitMeasure;
4
use Arcanedev\Units\Contracts\Measures\FileSize as FileSizeContract;
5
use Arcanedev\Units\Traits\Calculatable;
6
7
/**
8
 * Class     FileSize
9
 *
10
 * @package  Arcanedev\Units\Measures
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class FileSize extends UnitMeasure implements FileSizeContract
14
{
15
    /* -----------------------------------------------------------------
16
     |  Traits
17
     | -----------------------------------------------------------------
18
     */
19
20
    use Calculatable;
21
22
    /* -----------------------------------------------------------------
23
     |  Getters & Setters
24
     | -----------------------------------------------------------------
25
     */
26
27
    /**
28
     * Get the default names.
29
     *
30
     * @return array
31
     */
32 63
    public function defaultNames()
33
    {
34 63
        return array_combine(static::units(), [
35 63
            'yotta',
36
            'zetta',
37
            'exa',
38
            'peta',
39
            'tera',
40
            'gigabyte',
41
            'megabyte',
42
            'kilobyte',
43
            'byte',
44
        ]);
45
    }
46
47
    /* -----------------------------------------------------------------
48
     |  Constructor
49
     | -----------------------------------------------------------------
50
     */
51
52
    /**
53
     * Distance constructor.
54
     *
55
     * @param  float|int  $value
56
     * @param  string     $unit
57
     * @param  array      $options
58
     */
59 66
    public function __construct($value = 0, $unit = self::B, array $options = [])
60
    {
61 66
        parent::__construct($value, $unit, $options);
62 66
    }
63
64
    /* -----------------------------------------------------------------
65
     |  Main Methods
66
     | -----------------------------------------------------------------
67
     */
68
69
    /**
70
     * Make a distance instance.
71
     *
72
     * @param  float|int  $value
73
     * @param  string     $unit
74
     * @param  array      $options
75
     *
76
     * @return \Arcanedev\Units\Contracts\Measures\FileSize|\Arcanedev\Units\Contracts\UnitMeasure
77
     */
78 27
    public static function make($value = 0, $unit = self::B, array $options = [])
79
    {
80 27
        return parent::make($value, $unit, $options);
81
    }
82
83
    /* -----------------------------------------------------------------
84
     |  Calculation Methods
85
     | -----------------------------------------------------------------
86
     */
87
88
    /**
89
     * Add the file size.
90
     *
91
     * @param  float|int  $value
92
     * @param  string     $unit
93
     *
94
     * @return \Arcanedev\Units\Contracts\Measures\FileSize|\Arcanedev\Units\Contracts\UnitMeasure
95
     */
96 6
    public function addSize($value, $unit = self::B)
97
    {
98 6
        return $this->add(static::make($value, $unit));
0 ignored issues
show
Bug introduced by
It seems like static::make($value, $unit) targeting Arcanedev\Units\Measures\FileSize::make() can also be of type object<Arcanedev\Units\C...acts\Measures\FileSize>; however, Arcanedev\Units\Traits\Calculatable::add() does only seem to accept object<Arcanedev\Units\Contracts\UnitMeasure>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug Best Practice introduced by
The return type of return $this->add(static::make($value, $unit)); (Arcanedev\Units\Contracts\UnitMeasure) is incompatible with the return type declared by the interface Arcanedev\Units\Contract...sures\FileSize::addSize of type Arcanedev\Units\Contracts\Measures\FileSize.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
99
    }
100
101
    /**
102
     * Sub the file size.
103
     *
104
     * @param  float|int  $value
105
     * @param  string     $unit
106
     *
107
     * @return \Arcanedev\Units\Contracts\Measures\FileSize|\Arcanedev\Units\Contracts\UnitMeasure
108
     */
109 6
    public function subSize($value, $unit = self::B)
110
    {
111 6
        return $this->sub(static::make($value, $unit));
0 ignored issues
show
Bug introduced by
It seems like static::make($value, $unit) targeting Arcanedev\Units\Measures\FileSize::make() can also be of type object<Arcanedev\Units\C...acts\Measures\FileSize>; however, Arcanedev\Units\Traits\Calculatable::sub() does only seem to accept object<Arcanedev\Units\Contracts\UnitMeasure>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug Best Practice introduced by
The return type of return $this->sub(static::make($value, $unit)); (Arcanedev\Units\Contracts\UnitMeasure) is incompatible with the return type declared by the interface Arcanedev\Units\Contract...sures\FileSize::subSize of type Arcanedev\Units\Contracts\Measures\FileSize.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
112
    }
113
114
    /* -----------------------------------------------------------------
115
     |  Other Methods
116
     | -----------------------------------------------------------------
117
     */
118
119
    /**
120
     * Get all the distance ratios.
121
     *
122
     * @return array
123
     */
124 9
    protected static function getRatios()
125
    {
126 9
        $rate   = 1024;
127
        $ratios = [
128 9
            FileSize::YB => 0,
129 9
            FileSize::ZB => 1,
130 9
            FileSize::EB => 2,
131 9
            FileSize::PB => 3,
132 9
            FileSize::TB => 4,
133 9
            FileSize::GB => 5,
134 9
            FileSize::MB => 6,
135 9
            FileSize::KB => 7,
136 9
            FileSize::B  => 8,
137
        ];
138
139 9
        return array_map(function ($ratio) use ($rate) {
140 9
            return static::calculate($rate, '^', $ratio);
141 9
        }, $ratios);
142
    }
143
}
144