Passed
Push — master ( 9b7912...982aac )
by Janis
02:15
created

HavingTrait::having()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Janisbiz\LightOrm\Dms\MySQL\QueryBuilder\Traits;
4
5
use Janisbiz\LightOrm\Dms\MySQL\Enum\ConditionEnum;
6
use Janisbiz\LightOrm\Dms\MySQL\QueryBuilder\QueryBuilderException;
7
8
trait HavingTrait
9
{
10
    /**
11
     * @var array
12
     */
13
    protected $having = [];
14
15
    /**
16
     * @param string $condition
17
     * @param array $bind
18
     *
19
     * @return $this
20
     * @throws QueryBuilderException
21
     */
22
    public function having(string $condition, array $bind = [])
23
    {
24
        if (!$condition) {
25
            throw new QueryBuilderException('You must pass $condition to having function!');
26
        }
27
28
        $this->having[] = $condition;
29
30
        if (!empty($bind)) {
31
            $this->bind($bind);
0 ignored issues
show
Bug introduced by
It seems like bind() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
            $this->/** @scrutinizer ignore-call */ 
32
                   bind($bind);
Loading history...
32
        }
33
34
        return $this;
35
    }
36
37
    /**
38
     * @return null|string
39
     */
40
    protected function buildHavingQueryPart(): ?string
41
    {
42
        return empty($this->having)
43
            ? null
44
            : \sprintf('%s %s', ConditionEnum::HAVING, \implode(' AND ', \array_unique($this->having)))
45
        ;
46
    }
47
}
48