MiscHelper::getReadableBytes()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
3
/*
4
 * rmarchiv.tk
5
 * (c) 2016-2017 by Marcel 'ryg' Hering
6
 */
7
8
namespace App\Helpers;
9
10
use NotificationChannels\Discord\Discord;
11
12
class MiscHelper
13
{
14
    /**
15
     * Get the hash of the current git HEAD.
16
     *
17
     * @param string $branch The git branch to check
18
     *
19
     * @return mixed Either the hash or a boolean false
20
     */
21
    public static function get_current_git_commit($branch = 'master')
0 ignored issues
show
Unused Code introduced by
The parameter $branch is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
22
    {
23
        $rev = exec('git rev-parse --short HEAD');
24
25
        return $rev;
26
    }
27
28
    public static function sendDiscord($content)
0 ignored issues
show
Unused Code introduced by
The parameter $content is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
29
    {
30
        $dc = Discord::class;
0 ignored issues
show
Unused Code introduced by
$dc is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
31
    }
32
33
    public static function sendTelegram($content)
34
    {
35
        \Telegram::sendMessage([
36
            'chat_id'                  => 51419661,
37
            'text'                     => $content,
38
            'parse_mode'               => 'markdown',
39
            'disable_web_page_preview' => true,
40
        ]);
41
    }
42
43
    public static function array_diff_assoc_recursive($array1, $array2)
44
    {
45
        $difference = [];
46
        foreach ($array1 as $key => $value) {
47
            if (is_array($value)) {
48
                if (! isset($array2[$key]) || ! is_array($array2[$key])) {
49
                    $difference[$key] = $value;
50
                } else {
51
                    $new_diff = self::array_diff_assoc_recursive($value, $array2[$key]);
52
                    if (! empty($new_diff)) {
53
                        $difference[$key] = $new_diff;
54
                    }
55
                }
56
            } elseif (! array_key_exists($key, $array2) || $array2[$key] !== $value) {
57
                $difference[$key] = $value;
58
            }
59
        }
60
61
        return $difference;
62
    }
63
64
    public static function getPopularity($views, $max)
65
    {
66
        if ($max == 0 || $views == 0) {
67
            $ret = 0;
68
        } else {
69
            $ret = ($views / $max) * 100;
70
        }
71
72
        return $ret;
73
    }
74
75
    /**
76
     * @param int $size
77
     *
78
     * @return string
79
     */
80
    public static function getReadableBytes($size)
81
    {
82
        $bytes = $size;
83
84
        if ($bytes == 0) {
85
            return '0.00 B';
86
        }
87
88
        $s = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
89
        $e = floor(log($bytes, 1024));
90
91
        return round($bytes / pow(1024, $e), 2).' '.$s[$e];
92
    }
93
}
94