Completed
Pull Request — master (#17)
by Roman
06:01 queued 02:35
created

Math.php ➔ export()   C

Complexity

Conditions 8
Paths 1

Size

Total Lines 62
Code Lines 48

Duplication

Lines 38
Ratio 61.29 %

Importance

Changes 0
Metric Value
cc 8
eloc 48
nc 1
nop 0
dl 38
loc 62
rs 6.943
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace PeacefulBit\Slate\Core\Modules\Math;
4
5
use function Nerd\Common\Arrays\toHeadTail;
6
7
use PeacefulBit\Slate\Exceptions\EvaluatorException;
8
use PeacefulBit\Slate\Parser\Nodes\NativeExpression;
9
10
function export()
11
{
12
    return [
13
        'math' => [
14
            '+' => new NativeExpression(function ($eval, array $arguments) {
15
                return array_reduce($arguments, function ($result, $argument) use ($eval) {
16
                    return $result + $eval($argument);
17
                }, 0);
18
            }),
19
            '*' => new NativeExpression(function ($eval, array $arguments) {
20
                return array_reduce($arguments, function ($result, $argument) use ($eval) {
21
                    return $result * $eval($argument);
22
                }, 1);
23
            }),
24 View Code Duplication
            '-' => new NativeExpression(function ($eval, 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...
25
                switch (sizeof($arguments)) {
26
                    case 0:
27
                        throw new EvaluatorException("Too few arguments");
28
                    case 1:
29
                        return $eval($arguments[0]);
30
                    default:
31
                        list ($first, $rest) = toHeadTail($arguments);
32
                        return array_reduce($rest, function ($result, $argument) use ($eval) {
33
                            return $result - $eval($argument);
34
                        }, $eval($first));
35
                }
36
            }),
37 View Code Duplication
            '/' => new NativeExpression(function ($eval, 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...
38
                switch (sizeof($arguments)) {
39
                    case 0:
40
                        throw new EvaluatorException("Too few arguments");
41
                    case 1:
42
                        return 1 / $eval($arguments[0]);
43
                    default:
44
                        list ($first, $rest) = toHeadTail($arguments);
45
                        return array_reduce($rest, function ($result, $argument) use ($eval) {
46
                            return $result / $eval($argument);
47
                        }, $eval($first));
48
                }
49
            }),
50
            '%' => new NativeExpression(function ($eval, array $arguments) {
51
                if (sizeof($arguments) != 2) {
52
                    throw new EvaluatorException("Modulo operation requires exactly two arguments");
53
                }
54
                list ($number, $div) = $arguments;
55
                return $eval($number) % $eval($div);
56
            }),
57 View Code Duplication
            'pow' => new NativeExpression(function ($eval, 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...
58
                switch (sizeof($arguments)) {
59
                    case 0:
60
                    case 1:
61
                        throw new EvaluatorException("Function \"pow\" requires at least two arguments");
62
                    default:
63
                        list ($first, $rest) = toHeadTail($arguments);
64
                        return array_reduce($rest, function ($result, $argument) use ($eval) {
65
                            return pow($result, $eval($argument));
66
                        }, $eval($first));
67
                }
68
            })
69
        ]
70
    ];
71
}
72