AbstractInCondition::toString()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
ccs 9
cts 9
cp 1
rs 9.4285
cc 2
eloc 9
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Puzzle\QueryBuilder\Conditions;
6
7
use Puzzle\QueryBuilder\Escaper;
8
use Puzzle\QueryBuilder\Type;
9
10
abstract class AbstractInCondition extends AbstractCondition
11
{
12
    protected
13
        $type,
1 ignored issue
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $type.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
14
        $values;
15
16 24
    public function __construct(Type $column, array $values)
17
    {
18 24
        $this->type = $column;
19 24
        $this->values = $values;
20 24
    }
21
22 19
    public function toString(Escaper $escaper): string
23
    {
24 19
        if($this->isEmpty())
25
        {
26 2
            return '';
27
        }
28
29 17
        $values = $this->escapeValues($this->values, $escaper);
30
31 17
        return sprintf(
32 17
            '%s %s (%s)',
33 17
            $this->type->getName(),
34 17
            $this->getOperator(),
35 17
            implode(', ', $values)
36
        );
37
    }
38
39 26
    public function isEmpty(): bool
40
    {
41 26
        $columnName = $this->type->getName();
42
43 26
        return empty($columnName);
44
    }
45
46
    abstract protected function getOperator(): string;
47
48 17
    protected function escapeValues(array $values, Escaper $escaper): array
49
    {
50 17
        $escapedValues = [];
51
52 17
        foreach($values as $value)
53
        {
54 17
            $formatedValue = $this->type->format($value);
55 17
            if($this->type->isEscapeRequired())
56
            {
57 13
                $formatedValue = $escaper->escape($formatedValue);
58
            }
59
60 17
            $escapedValues[] = $formatedValue;
61
        }
62
63 17
        return $escapedValues;
64
    }
65
}
66