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

GenericElasticsearchDefinitions   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 95
Duplicated Lines 18.95 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 18
loc 95
wmc 3
lcom 0
cbo 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B create() 18 89 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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