Passed
Push — master ( 17b30e...88ad78 )
by Amin
03:09
created

Helper::randomString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
16
use Streaming\Exception\InvalidArgumentException;
17
18
class Helper
19
{
20
    /**
21
     * round a number to nearest even number
22
     *
23
     * @param float $number
24
     * @return int
25
     */
26
    public static function roundToEven(float $number): int
27
    {
28
        return (($number = intval($number)) % 2 == 0) ? $number : $number + 1;
29
    }
30
31
    /**
32
     * @param int $length
33
     * @return bool|string
34
     */
35
    public static function randomString($length = 10)
36
    {
37
        $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
38
        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

38
        return substr(str_shuffle(str_repeat($chars, /** @scrutinizer ignore-type */ ceil($length / strlen($chars)))), 1, $length);
Loading history...
39
    }
40
41
    /**
42
     * @param $word
43
     * @return bool|string
44
     */
45
    public static function appendSlash(string $word)
46
    {
47
        if ($word) {
48
            return rtrim($word, '/') . '/';
49
        }
50
        return $word;
51
    }
52
53
    /**
54
     * @param $url
55
     * @return bool
56
     */
57
    public static function isURL(string $url)
58
    {
59
        if (!filter_var($url, FILTER_VALIDATE_URL)) {
60
            throw new InvalidArgumentException("Your URL($url) is not valid! Your URL should start with (http://) or (https://).");
61
        }
62
63
        return true;
64
    }
65
}