type()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Scalp\Utils {
6
    function delay(callable $functionOrCodeBlock, ...$args): Delayed
7
    {
8
        return new Delayed($functionOrCodeBlock, ...$args);
9
    }
10
11
    const Failure = __NAMESPACE__.'\Failure';
12
13
    function Failure(\Throwable $error): Failure
14
    {
15
        return new Failure($error);
16
    }
17
18
    const Success = __NAMESPACE__.'\Success';
19
20
    function Success($value): Success
21
    {
22
        return new Success($value);
23
    }
24
25
    function TryCatch(callable $delayed): TryCatch
26
    {
27
        try {
28
            return new Success($delayed());
29
        } catch (\Throwable $e) {
30
            return new Failure($e);
31
        }
32
    }
33
34
    const isInstanceOfType = __NAMESPACE__.'\isInstanceOfType';
35
36
    function isInstanceOfType($value, string $type): bool
37
    {
38
        return checkType(type($value), $type);
39
    }
40
41
    function type($x): string
42
    {
43
        return is_object($x)
44
            ? get_class($x)
45
            : gettype($x)
46
            ;
47
    }
48
49
    const checkType = __NAMESPACE__.'\checkType';
50
51
    function checkType(string $actual, string $expected): bool
52
    {
53
        return $actual === $expected
54
            || is_subclass_of($actual, $expected, true)
55
            ;
56
    }
57
}
58