Passed
Push — master ( 2cd313...f3f6f1 )
by Amin
02:53
created

Helper   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 50
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A makeDir() 0 4 2
A directorySize() 0 11 4
A randomString() 0 4 1
A roundToEven() 0 3 2
1
<?php
2
3
/**
4
 * This file is part of the PHP-FFmpeg-video-streaming package.
5
 *
6
 * (c) Amin Yazdanpanah <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Streaming;
13
14
15
class Helper
16
{
17
    /**
18
     * round a number to nearest even number
19
     *
20
     * @param float $number
21
     * @return int
22
     */
23
    public static function roundToEven(float $number): int
24
    {
25
        return (($number = intval($number)) % 2 == 0) ? $number : $number + 1;
26
    }
27
28
29
    /**
30
     * @param $dirname
31
     */
32
    public static function makeDir($dirname): void
33
    {
34
        if (!is_dir($dirname)) {
35
            mkdir($dirname, 0777, true);
36
        }
37
    }
38
39
    /**
40
     * @param int $length
41
     * @return bool|string
42
     */
43
    public static function randomString($length = 10)
44
    {
45
        $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
46
        return substr(str_shuffle(str_repeat($chars, ceil($length / strlen($chars)))), 1, $length);
0 ignored issues
show
Bug introduced by
ceil($length / strlen($chars)) of type double is incompatible with the type integer expected by parameter $multiplier of str_repeat(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
        return substr(str_shuffle(str_repeat($chars, /** @scrutinizer ignore-type */ ceil($length / strlen($chars)))), 1, $length);
Loading history...
47
48
    }
49
50
    /**
51
     * @param $dir
52
     * @return int|null
53
     */
54
    public static function directorySize($dir)
55
    {
56
        if (is_dir($dir)) {
57
            $size = 0;
58
            foreach (glob(rtrim($dir, '/') . '/*', GLOB_NOSORT) as $each) {
59
                $size += is_file($each) ? filesize($each) : static::directorySize($each);
60
            }
61
            return $size;
62
        }
63
64
        return null;
65
    }
66
}