Completed
Push — master ( b27ddf...df6e21 )
by Roman
28s
created

Logic.php ➔ export()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 45
Code Lines 32

Duplication

Lines 33
Ratio 73.33 %

Importance

Changes 0
Metric Value
cc 5
eloc 32
nc 1
nop 0
dl 33
loc 45
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace PeacefulBit\Packet\Modules\Logic;
4
5
use function Nerd\Common\Arrays\all;
6
use function Nerd\Common\Arrays\any;
7
use PeacefulBit\Packet\Exception\RuntimeException;
8
use PeacefulBit\Packet\Nodes\NativeNode;
9
use PeacefulBit\Packet\Visitors\NodeCalculatorVisitor;
10
11
function export()
12
{
13
    return [
14
        'or' => new NativeNode('or', function (NodeCalculatorVisitor $visitor, array $arguments) {
15
            $predicate = function ($item) use ($visitor) {
16
                $visit = $visitor->getVisitor($item);
17
                return !!$visit($item);
18
            };
19
            return any($arguments, $predicate);
20
        }),
21 View Code Duplication
        'and' => new NativeNode('and', function (NodeCalculatorVisitor $visitor, array $arguments) {
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...
22
            $predicate = function ($item) use ($visitor) {
23
                $visit = $visitor->getVisitor($item);
24
                return !!$visit($item);
25
            };
26
            return all($arguments, $predicate);
27
        }),
28 View Code Duplication
        'not' => new NativeNode('not', function (NodeCalculatorVisitor $visitor, array $arguments) {
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...
29
            return all($arguments, function ($argument) use ($visitor) {
30
                $visit = $visitor->getVisitor($argument);
31
                return !$visit($argument);
32
            });
33
        }),
34 View Code Duplication
        'if' => new NativeNode('if', function (NodeCalculatorVisitor $visitor, array $arguments) {
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...
35
            if (sizeof($arguments) != 3) {
36
                throw new RuntimeException("Function 'if' accepts only three arguments");
37
            }
38
            list ($exp, $cons, $alt) = $arguments;
39
            $visitExp = $visitor->getVisitor($exp);
40
            $visitCons = $visitor->getVisitor($cons);
41
            $visitAlt = $visitor->getVisitor($alt);
42
            return $visitExp($exp) ? $visitCons($cons) : $visitAlt($alt);
43
        }),
44 View Code Duplication
        'unless' => new NativeNode('unless', function (NodeCalculatorVisitor $visitor, array $arguments) {
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...
45
            if (sizeof($arguments) != 3) {
46
                throw new RuntimeException("Function 'unless' accepts only three arguments");
47
            }
48
            list ($exp, $cons, $alt) = $arguments;
49
            $visitExp = $visitor->getVisitor($exp);
50
            $visitCons = $visitor->getVisitor($cons);
51
            $visitAlt = $visitor->getVisitor($alt);
52
            return $visitExp($exp) ? $visitAlt($alt) : $visitCons($cons);
53
        })
54
    ];
55
}
56