Completed
Push — 1.0 ( b7886a...4c0b42 )
by Peter
07:48 queued 11s
created

LikePattern   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 55
Duplicated Lines 16.36 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 9
loc 55
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A transform() 9 9 1
A formatValue() 0 4 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 Happyr\DoctrineSpecification\Operand;
4
5
use Doctrine\ORM\QueryBuilder;
6
use Happyr\DoctrineSpecification\ValueConverter;
7
8
class LikePattern implements Operand
9
{
10
    const CONTAINS = '%%%s%%';
11
12
    const ENDS_WITH = '%%%s';
13
14
    const STARTS_WITH = '%s%%';
15
16
    /**
17
     * @var string
18
     */
19
    private $value;
20
21
    /**
22
     * @var string
23
     */
24
    private $format;
25
26
    /**
27
     * @param string $value
28
     * @param string $format
29
     */
30
    public function __construct($value, $format = self::CONTAINS)
31
    {
32
        $this->value = $value;
33
        $this->format = $format;
34
    }
35
36
    /**
37
     * @param QueryBuilder $qb
38
     * @param string       $dqlAlias
39
     *
40
     * @return string
41
     */
42 View Code Duplication
    public function transform(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...
43
    {
44
        $paramName = sprintf('comparison_%d', $qb->getParameters()->count());
45
        $value = ValueConverter::convertToDatabaseValue($this->value, $qb);
46
        $value = $this->formatValue($this->format, $value);
47
        $qb->setParameter($paramName, $value);
48
49
        return sprintf(':%s', $paramName);
50
    }
51
52
    /**
53
     * @param string $format
54
     * @param string $value
55
     *
56
     * @return string
57
     */
58
    private function formatValue($format, $value)
59
    {
60
        return sprintf($format, $value);
61
    }
62
}
63