AbstractComparisonOperatorCondition::isEmpty()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 1
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 AbstractComparisonOperatorCondition extends AbstractCondition
11
{
12
    protected
13
        $leftOperand,
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 $leftOperand.

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
        $rightOperand;
15
16 158
    public function __construct(Type $leftOperand, $rightOperand)
17
    {
18 158
        $this->leftOperand = $leftOperand;
19 158
        $this->rightOperand = $rightOperand;
20 158
    }
21
22 192
    public function toString(Escaper $escaper): string
23
    {
24 192
        if($this->isEmpty())
25
        {
26 11
            return '';
27
        }
28
29 181
        return sprintf(
30 181
            '%s %s %s',
31 181
            $this->generateFieldOperand($this->leftOperand),
32 181
            $this->getConditionOperator(),
33 181
            $this->generateRightOperand($escaper)
34
        );
35
    }
36
37 181
    private function generateFieldOperand(Type $field): string
38
    {
39 181
        return $field->getName();
40
    }
41
42 181
    private function generateRightOperand(Escaper $escaper)
43
    {
44 181
        if($this->rightOperand instanceof Type)
45
        {
46 36
            return $this->generateFieldOperand($this->rightOperand);
47
        }
48
49 145
        return $this->escapeValue($this->rightOperand, $escaper);
50
    }
51
52 206
    public function isEmpty(): bool
53
    {
54 206
        $columnName = $this->leftOperand->getName();
55
56 206
        return empty($columnName);
57
    }
58
59
    abstract protected function getConditionOperator(): string;
60
61 145 View Code Duplication
    private function escapeValue($value, Escaper $escaper)
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...
62
    {
63 145
        $value = $this->leftOperand->format($value);
64
65 145
        if($this->leftOperand->isEscapeRequired())
66
        {
67 105
            $value = $escaper->escape($value);
68
        }
69
70 145
        return $value;
71
    }
72
}
73