Formatter::formatSize()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 16

Duplication

Lines 9
Ratio 56.25 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
dl 9
loc 16
ccs 10
cts 10
cp 1
rs 9.4222
c 0
b 0
f 0
cc 5
nc 5
nop 1
crap 5
1
<?php
2
3
namespace UniMan\Core\Helper;
4
5
use Nette\Localization\ITranslator;
6
7
class Formatter
8
{
9
    private $translator;
10
11 6
    public function __construct(ITranslator $translator)
12
    {
13 6
        $this->translator = $translator;
14 6
    }
15
16 4
    public function formatNumber($number, $decimals = 0)
17
    {
18 4
        return number_format($number, $decimals, $this->translator->translate('core.formatter.number_format.decimal_point'), $this->translator->translate('core.formatter.number_format.thousands_separator'));
19
    }
20
21 2
    public function formatSize($number)
22
    {
23 2
        if ($number < 1024) {
24 2
            return $this->formatNumber($number) . ' B';
25
        }
26 2 View Code Duplication
        if ($number < pow(1024, 2)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27 2
            return $this->formatNumber($number / 1024, 1) . ' kB';
28
        }
29 2 View Code Duplication
        if ($number < pow(1024, 3)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30 2
            return $this->formatNumber($number / pow(1024, 2), 1) . ' MB';
31
        }
32 2 View Code Duplication
        if ($number < pow(1024, 4)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33 2
            return $this->formatNumber($number / pow(1024, 3), 1) . ' GB';
34
        }
35 2
        return $this->formatNumber($number / pow(1024, 4), 1) . ' TB';
36
    }
37
38 2
    public function formatTime($number)
39
    {
40 2
        if ($number < 60) {
41 2
            return $number . ' s';
42
        }
43 2
        if ($number < 3600) {
44 2
            return date('i:s', $number);
45
        }
46 2
        return floor($number / 3600) . date('\h i\m s\s', $number);
47
    }
48
}
49