Passed
Push — master ( 766df6...286284 )
by Roman
55s
created

Relation.php ➔ export()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 35
nc 1
nop 0
dl 0
loc 47
rs 9.0303
c 0
b 0
f 0
1
<?php
2
3
namespace PeacefulBit\Slate\Core\Modules\Relation;
4
5
use function Nerd\Common\Arrays\toHeadTail;
6
use PeacefulBit\Slate\Exceptions\EvaluatorException;
7
use PeacefulBit\Slate\Parser\Nodes\NativeExpression;
8
9
function relationReduce($eval, $callback, array $arguments)
10
{
11
    if (empty($arguments)) {
12
        throw new EvaluatorException("Too few arguments");
13
    }
14 View Code Duplication
    $iter = function ($rest, $first) use (&$iter, $callback, $eval) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
15
        if (empty($rest)) {
16
            return true;
17
        }
18
        list ($head, $tail) = toHeadTail($rest);
19
        $visitedHead = $eval($head);
20
        if ($callback($first, $visitedHead)) {
21
            return $iter($tail, $visitedHead);
22
        }
23
        return false;
24
    };
25
    list ($head, $tail) = toHeadTail($arguments);
26
    return $iter($tail, $eval($head));
27
}
28
29
function export()
30
{
31
    return [
32
        '@' => [
33
            '>' => new NativeExpression(function ($eval, array $arguments) {
34
                return relationReduce($eval, function ($first, $second) {
35
                    return $first > $second;
36
                }, $arguments);
37
            }),
38
            '<' => new NativeExpression(function ($eval, array $arguments) {
39
                return relationReduce($eval, function ($first, $second) {
40
                    return $first < $second;
41
                }, $arguments);
42
            }),
43
            '>=' => new NativeExpression(function ($eval, array $arguments) {
44
                return relationReduce($eval, function ($first, $second) {
45
                    return $first >= $second;
46
                }, $arguments);
47
            }),
48
            '<=' => new NativeExpression(function ($eval, array $arguments) {
49
                return relationReduce($eval, function ($first, $second) {
50
                    return $first <= $second;
51
                }, $arguments);
52
            }),
53
            '=' => new NativeExpression(function ($eval, array $arguments) {
54
                return relationReduce($eval, function ($first, $second) {
55
                    return $first == $second;
56
                }, $arguments);
57
            }),
58
            '==' => new NativeExpression(function ($eval, array $arguments) {
59
                return relationReduce($eval, function ($first, $second) {
60
                    return $first === $second;
61
                }, $arguments);
62
            }),
63
            '!=' => new NativeExpression(function ($eval, array $arguments) {
64
                return relationReduce($eval, function ($first, $second) {
65
                    return $first != $second;
66
                }, $arguments);
67
            }),
68
            '!==' => new NativeExpression(function ($eval, array $arguments) {
69
                return relationReduce($eval, function ($first, $second) {
70
                    return $first !== $second;
71
                }, $arguments);
72
            })
73
        ]
74
    ];
75
}
76