Completed
Push — master ( 68be21...073c54 )
by Kévin
02:41
created

GenericElasticsearchDefinitions::create()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 89
Code Lines 41

Duplication

Lines 18
Ratio 20.22 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 18
loc 89
rs 8.5731
cc 3
eloc 41
nc 1
nop 1

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 RulerZ\Target\Operators;
4
5
class GenericElasticsearchDefinitions
6
{
7
    /**
8
     * @return Definitions
9
     */
10
    public static function create(Definitions $customOperators)
11
    {
12
        $definitions = new Definitions();
13
14
        // start with a few helpers
15
        $must = function($query) {
16
            return "[
17
    'bool' => ['must' => $query]
18
]";
19
        };
20
        $mustNot = function($query) {
21
            return "[
22
                'bool' => ['must_not' => $query]
23
            ]";
24
        };
25
        $range = function($field, $value, $operator) use ($must) {
26
            return $must("[
27
                'range' => [
28
                    '$field' => ['$operator' => $value],
29
                ]
30
            ]");
31
        };
32
33
        // Here are the operators!
34
        $definitions->defineInlineOperator('and', function ($a, $b) use ($must) {
35
            return $must("[$a, $b]");
36
        });
37
        $definitions->defineInlineOperator('or', function ($a, $b) use ($must) {
38
            return "[
39
                'bool' => ['should' => [$a, $b], 'minimum_should_match' => 1]
40
            ]";
41
        });
42
43 View Code Duplication
        $definitions->defineInlineOperator('like', function ($a, $b) use ($must) {
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...
44
            $value = is_array($b) ? implode(' ', $b) : $b;
45
46
            return $must("[
47
                'match' => [
48
                    '$a' => '$value',
49
                ]
50
            ]");
51
        });
52 View Code Duplication
        $definitions->defineInlineOperator('has', function ($a, $b) use ($must) {
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...
53
            $value = is_array($b) ? '[' . implode(', ', $b) . ']' : $b;
54
55
            return $must("[
56
                'terms' => [
57
                    '$a' => $value,
58
                ]
59
            ]");
60
        });
61
        $definitions->defineInlineOperator('in', $definitions->getInlineOperator('has'));
62
63
        $definitions->defineInlineOperator('=', function ($a, $b) use ($must) {
64
            return $must("[
65
                'term' => [
66
                    '$a' => $b,
67
                ]
68
            ]");
69
        });
70
71
        $definitions->defineInlineOperator('!=', function ($a, $b) use ($mustNot) {
72
            return $mustNot("[
73
                'term' => [
74
                    '$a' => $b,
75
                ]
76
            ]");
77
        });
78
79
        $definitions->defineInlineOperator('not', $mustNot);
80
81
        $definitions->defineInlineOperator('>', function ($a, $b) use ($range) {
82
            return $range($a, $b, 'gt');
83
        });
84
85
        $definitions->defineInlineOperator('>=', function ($a, $b) use ($range) {
86
            return $range($a, $b, 'gte');
87
        });
88
89
        $definitions->defineInlineOperator('<', function ($a, $b) use ($range) {
90
            return $range($a, $b, 'lt');
91
        });
92
93
        $definitions->defineInlineOperator('<=', function ($a, $b) use ($range) {
94
            return $range($a, $b, 'lte');
95
        });
96
97
        return $definitions->mergeWith($customOperators);
98
    }
99
}
100