Field   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 53
Duplicated Lines 9.43 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 5
loc 53
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A accept() 0 4 1
B handle() 5 32 9
A like2regexp() 0 6 1

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 Kaliop\EzFindSearchEngineBundle\Core\Persistence\eZFind\Content\Search\Common\Gateway\CriterionHandler;
4
5
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
6
use Kaliop\EzFindSearchEngineBundle\Core\Persistence\eZFind\Content\Search\Common\Gateway\CriteriaConverter;
7
use Kaliop\EzFindSearchEngineBundle\Core\Persistence\eZFind\Content\Search\Common\Gateway\CriterionHandler;
8
9
class Field extends FieldBase
10
{
11
    public function accept( Criterion $criterion )
12
    {
13
        return $criterion instanceof Criterion\Field;
14
    }
15
16
    /// @todo should we apply specific conversions based on field type?
17
    public function handle( CriteriaConverter $converter, Criterion $criterion )
18
    {
19
        $field = $this->determineFieldName($criterion->target);
20
21
        switch ( $criterion->operator )
22
        {
23 View Code Duplication
            case Criterion\Operator::IN:
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...
24
                $values = array_map(array($this, 'escapeValue'), $criterion->value);
25
                return $field . ':(' . implode(' ', $values) . ')';
26
27
            case Criterion\Operator::GT:
28
                return $field . ':{' . $this->escapeValue($criterion->value) . ' TO *]';
29
30
            case Criterion\Operator::GTE:
31
                return $field . ':[' . $this->escapeValue($criterion->value) . ' TO *]';
32
33
            case Criterion\Operator::LT:
34
                return $field . ':[* TO ' . $this->escapeValue($criterion->value) . '}';
35
36
            case Criterion\Operator::LTE:
37
                return $field . ':[* TO ' . $this->escapeValue($criterion->value) . ']';
38
39
            case Criterion\Operator::LIKE:
40
                return $field . ':' . $this->like2regexp($this->escapeValue($criterion->value));
41
42 View Code Duplication
            case Criterion\Operator::BETWEEN:
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...
43
                return $field . ':[' . $this->escapeValue($criterion->value[0]) . ' TO ' . $this->escapeValue($criterion->value[1]) . ']';
44
45
            case Criterion\Operator::CONTAINS:
46
                return $field . ':*' . $this->escapeValue($criterion->value) . '*';
47
        }
48
    }
49
50
    /**
51
     * Transform db-style LIKE in regexp
52
     * - replace %, _ chars
53
     * - escape regexp chars
54
     */
55
    protected function like2regexp($string, $delimiter='/')
56
    {
57
        $string = preg_quote($string, $delimiter);
58
        $string = str_replace(array('%', '_'), array('.*', '.'), $string);
59
        return $delimiter . $string . $delimiter;
60
    }
61
}
62