Like   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 22.03 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 13
loc 59
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A getFilter() 13 13 2

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
 * This file is part of the Happyr Doctrine Specification package.
5
 *
6
 * (c) Tobias Nyholm <[email protected]>
7
 *     Kacper Gunia <[email protected]>
8
 *     Peter Gribanov <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Happyr\DoctrineSpecification\Filter;
15
16
use Doctrine\ORM\Query\Expr\Comparison as DoctrineComparison;
17
use Doctrine\ORM\QueryBuilder;
18
use Happyr\DoctrineSpecification\Operand\ArgumentToOperandConverter;
19
use Happyr\DoctrineSpecification\Operand\LikePattern;
20
use Happyr\DoctrineSpecification\Operand\Operand;
21
22
class Like implements Filter
23
{
24
    const CONTAINS = LikePattern::CONTAINS;
25
26
    const ENDS_WITH = LikePattern::ENDS_WITH;
27
28
    const STARTS_WITH = LikePattern::STARTS_WITH;
29
30
    /**
31
     * @var Operand|string
32
     */
33
    private $field;
34
35
    /**
36
     * @var LikePattern
37
     */
38
    private $value;
39
40
    /**
41
     * @var string|null
42
     */
43
    private $dqlAlias;
44
45
    /**
46
     * @param Operand|string     $field
47
     * @param LikePattern|string $value
48
     * @param string             $format
49
     * @param string|null        $dqlAlias
50
     */
51
    public function __construct($field, $value, $format = LikePattern::CONTAINS, $dqlAlias = null)
52
    {
53
        if (!($value instanceof LikePattern)) {
54
            $value = new LikePattern($value, $format);
55
        }
56
        $this->field = $field;
57
        $this->value = $value;
58
        $this->dqlAlias = $dqlAlias;
59
    }
60
61
    /**
62
     * @param QueryBuilder $qb
63
     * @param string       $dqlAlias
64
     *
65
     * @return string
66
     */
67 View Code Duplication
    public function getFilter(QueryBuilder $qb, $dqlAlias)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
68
    {
69
        if (null !== $this->dqlAlias) {
70
            $dqlAlias = $this->dqlAlias;
71
        }
72
73
        $field = ArgumentToOperandConverter::toField($this->field);
74
75
        $field = $field->transform($qb, $dqlAlias);
76
        $value = $this->value->transform($qb, $dqlAlias);
77
78
        return (string) new DoctrineComparison($field, 'LIKE', $value);
79
    }
80
}
81