Completed
Pull Request — master (#13)
by Paweł
03:59 queued 01:37
created

papply()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace {
6
    require_once __DIR__.'/Conversion/implicit_conversion.php';
7
    require_once __DIR__.'/Reflection/functions.php';
8
    require_once __DIR__.'/Type/restrictions.php';
9
}
10
11
namespace Scalp {
12
    use function Scalp\Conversion\AnyToString;
13
14
    function println($x): void
15
    {
16
        echo AnyToString($x)."\n";
17
    }
18
19
    function None(): None
20
    {
21
        return new None();
22
    }
23
24
    function Some($x): Some
25
    {
26
        return new Some($x);
27
    }
28
29
    function Option($x): Option
30
    {
31
        return ($x === null) ? None() : Some($x);
32
    }
33
34
    const __ = '$argument$';
35
36
    function papply(callable $f, ...$args): callable
37
    {
38
        return new PartialApplication($f, $args);
0 ignored issues
show
Bug Best Practice introduced by
The expression return new Scalp\PartialApplication($f, $args) returns the type Scalp\PartialApplication which is incompatible with the type-hinted return callable.
Loading history...
39
    }
40
}
41
42
namespace Scalp\Conversion {
43
    const AnyToString = __NAMESPACE__.'\AnyToString';
44
45
    function AnyToString($any): string
46
    {
47
        static $anyToString = null;
48
49
        if ($anyToString === null) {
50
            $anyToString = new AnyToString();
51
        }
52
53
        return $anyToString($any);
54
    }
55
}
56
57
namespace Scalp\Utils {
58
    function Delayed(callable $functionOrCodeBlock, ...$args): Delayed
59
    {
60
        return new Delayed($functionOrCodeBlock, ...$args);
61
    }
62
63
    function Failure(\Throwable $error): Failure
64
    {
65
        return new Failure($error);
66
    }
67
68
    function Success($value): Success
69
    {
70
        return new Success($value);
71
    }
72
73
    function TryCatch(callable $delayed): TryCatch
74
    {
75
        try {
76
            return new Success($delayed());
77
        } catch (\Throwable $e) {
78
            return new Failure($e);
79
        }
80
    }
81
82
    function type($x): string
83
    {
84
        return is_object($x)
85
                ? get_class($x)
86
                : gettype($x)
87
            ;
88
    }
89
90
    function checkType(string $actual, string $expected): bool
91
    {
92
        return $actual === $expected
93
                || is_subclass_of($actual, $expected, true)
94
            ;
95
    }
96
}
97