Passed
Push — master ( 32f5fd...bae4db )
by Stephen
26:48
created

LaravelHelpers::randomFloat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Sfneal\Helpers\Laravel;
4
5
use Illuminate\Support\Facades\Cache;
6
use ReflectionClass;
7
use ReflectionException;
8
9
class LaravelHelpers
10
{
11
    /**
12
     * Return the alphabet in array form.
13
     *
14
     * @return array
15
     */
16
    public static function alphabet(): array
17
    {
18
        return Cache::rememberForever('alphabet', function () {
19
            return range('A', 'Z');
20
        });
21
    }
22
23
    /**
24
     * Return the index of a letter in the alphabet.
25
     *
26
     * @param int $index
27
     * @return string
28
     */
29
    public static function alphabetIndex(int $index): string
30
    {
31
        return self::alphabet()[$index];
32
    }
33
34
    /**
35
     * Retrieve a class's short name (without namespace).
36
     *
37
     * @param $class
38
     * @param bool $short Full name or short name
39
     * @param string|null $default
40
     * @return string
41
     */
42
    public static function getClassName($class, $short = false, $default = null): string
43
    {
44
        // Attempt to resolve the $class's name
45
        try {
46
            return (new ReflectionClass($class))->{$short ? 'getShortName' : 'getName'}();
47
        }
48
49
        // Return $default
50
        catch (ReflectionException $e) {
51
            return $default;
52
        }
53
    }
54
55
    /**
56
     * Determine if the Application is running in a 'production' environment.
57
     *
58
     * @return bool
59
     */
60
    public static function isProductionEnvironment(): bool
61
    {
62
        return env('APP_ENV') == 'production';
63
    }
64
65
    /**
66
     * Determine if the Application is running in a 'development' environment.
67
     *
68
     * @return bool
69
     */
70
    public static function isDevelopmentEnvironment(): bool
71
    {
72
        return env('APP_ENV') == 'development';
73
    }
74
75
    /**
76
     * Serialize and simple hash a value to create a unique ID.
77
     *
78
     * @param $value
79
     * @return int
80
     */
81
    public static function serializeHash($value): int
82
    {
83
84
        return crc32(serialize($value));
85
    }
86
87
    /**
88
     * Retrieve a random float between two values with a specified number of decimals
89
     *
90
     * @param $min
91
     * @param $max
92
     * @param int $decimals
93
     * @return float
94
     */
95
    public static function randomFloat(int $min, int $max, int $decimals = 2): float
96
    {
97
        $decimal = str_pad("1", $decimals + 1, '0');
98
        return rand($min, $max) + (rand(1, $decimal) / $decimal);
0 ignored issues
show
Bug introduced by
$decimal of type string is incompatible with the type integer expected by parameter $max of rand(). ( Ignorable by Annotation )

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

98
        return rand($min, $max) + (rand(1, /** @scrutinizer ignore-type */ $decimal) / $decimal);
Loading history...
99
    }
100
}
101