Passed
Branch 1.0 (db08ca)
by Vladimir
31:42
created

Str   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 0
cbo 0
dl 0
loc 52
ccs 14
cts 14
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A endsWith() 0 10 3
A random() 0 11 2
A lower() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Helpers;
6
7
class Str
8
{
9
    /**
10
     * Determine if a given string ends with a given substring.
11
     *
12
     * @param string       $haystack
13
     * @param string|array $needles
14
     *
15
     * @return bool
16
     */
17 1
    public static function endsWith(string $haystack, $needles): bool
18
    {
19 1
        foreach ((array) $needles as $needle) {
20 1
            if (substr($haystack, -strlen($needle)) === (string) $needle) {
21 1
                return true;
22
            }
23
        }
24
25 1
        return false;
26
    }
27
28
    /**
29
     * Generate a more truly "random" alpha-numeric string.
30
     *
31
     * @param int $length
32
     *
33
     * @return string
34
     */
35 2
    public static function random(int $length = 16): string
36
    {
37 2
        $string = '';
38 2
        while (($len = strlen($string)) < $length) {
39 2
            $size = $length - $len;
40 2
            $bytes = random_bytes($size);
41 2
            $string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size);
42
        }
43
44 2
        return $string;
45
    }
46
47
    /**
48
     * Convert the given string to lower-case.
49
     *
50
     * @param  string $value
51
     *
52
     * @return string
53
     */
54 3
    public static function lower($value)
55
    {
56 3
        return mb_strtolower($value, 'UTF-8');
57
    }
58
}
59