Completed
Push — EZP-29984 ( 3b440c )
by André
19:17
created

Collection   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 97
Duplicated Lines 11.34 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 11
loc 97
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B handle() 11 64 5

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
/**
4
 * File containing the DoctrineDatabase Collection field value handler class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Search\Legacy\Content\Common\Gateway\CriterionHandler\FieldValue\Handler;
10
11
use eZ\Publish\Core\Persistence\Database\DatabaseHandler;
12
use eZ\Publish\Core\Search\Legacy\Content\Common\Gateway\CriterionHandler\FieldValue\Handler;
13
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
14
use eZ\Publish\Core\Persistence\TransformationProcessor;
15
use eZ\Publish\Core\Persistence\Database\SelectQuery;
16
17
/**
18
 * Content locator gateway implementation using the DoctrineDatabase.
19
 *
20
 * Collection value handler is used for creating a filter on a value that is in fact a collection of values,
21
 * separated by a character.
22
 * Eg. list of countries, list of Selection options, list of RelationList Content ids
23
 */
24
class Collection extends Handler
25
{
26
    /**
27
     * Character separating indexed values.
28
     *
29
     * @var string
30
     */
31
    protected $separator;
32
33
    /**
34
     * Creates a new criterion handler.
35
     *
36
     * @param \eZ\Publish\Core\Persistence\Database\DatabaseHandler $dbHandler
37
     * @param \eZ\Publish\Core\Persistence\TransformationProcessor $transformationProcessor
38
     * @param string $separator
39
     */
40
    public function __construct(DatabaseHandler $dbHandler, TransformationProcessor $transformationProcessor, $separator)
41
    {
42
        $this->dbHandler = $dbHandler;
43
        $this->transformationProcessor = $transformationProcessor;
44
        $this->separator = $separator;
45
    }
46
47
    /**
48
     * Generates query expression for operator and value of a Field Criterion.
49
     *
50
     * @param \eZ\Publish\Core\Persistence\Database\SelectQuery $query
51
     * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion
52
     * @param string $column
53
     *
54
     * @return \eZ\Publish\Core\Persistence\Database\Expression
55
     */
56
    public function handle(SelectQuery $query, Criterion $criterion, $column)
57
    {
58
        switch ($criterion->operator) {
59
            case Criterion\Operator::LIKE:
60
            case Criterion\Operator::CONTAINS:
61
                if ($criterion->operator === Criterion\Operator::LIKE) {
62 View Code Duplication
                    if (strpos($criterion->value, '%') !== false) {
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...
63
                        // @deprecated In 6.7.x/6.13.x/7.3.x and higher, to be removed in 8.0
64
                        @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
65
                            "Usage of '%' in Operator::LIKE criteria with Legacy Search Engine was never intended, " .
66
                            "and is deprecated for removal in 8.0. Please use '*' like in FullText, works across engines",
67
                            E_USER_DEPRECATED
68
                        );
69
                        $value = $this->lowerCase($criterion->value);
70
                    } else {
71
                        $value = str_replace('*', '%', $this->prepareLikeString($criterion->value));
72
                    }
73
                    $singleValueExpr = 'like';
74
                } else {
75
                    $value = $this->prepareLikeString($criterion->value);
76
                    $singleValueExpr = 'eq';
77
                }
78
79
                $quotedColumn = $this->dbHandler->quoteColumn($column);
80
                $filter = $query->expr->lOr(
81
                    array(
82
                        $query->expr->$singleValueExpr(
83
                            $quotedColumn,
84
                            $query->bindValue($value, null, \PDO::PARAM_STR)
85
                        ),
86
                        $query->expr->like(
87
                            $quotedColumn,
88
                            $query->bindValue(
89
                                '%' . $this->separator . $value,
90
                                null,
91
                                \PDO::PARAM_STR
92
                            )
93
                        ),
94
                        $query->expr->like(
95
                            $quotedColumn,
96
                            $query->bindValue(
97
                                $value . $this->separator . '%',
98
                                null,
99
                                \PDO::PARAM_STR
100
                            )
101
                        ),
102
                        $query->expr->like(
103
                            $quotedColumn,
104
                            $query->bindValue(
105
                                '%' . $this->separator . $value . $this->separator . '%',
106
                                null,
107
                                \PDO::PARAM_STR
108
                            )
109
                        ),
110
                    )
111
                );
112
                break;
113
114
            default:
115
                $filter = parent::handle($query, $criterion, $column);
116
        }
117
118
        return $filter;
119
    }
120
}
121