AbstractJoin::buildOnConditionClause()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
ccs 6
cts 6
cp 1
rs 9.2
cc 4
eloc 6
nc 6
nop 0
crap 4
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Puzzle\QueryBuilder\Queries\Snippets\Joins;
6
7
use Puzzle\QueryBuilder\Snippet;
8
use Puzzle\QueryBuilder\Queries\Snippets\Join;
9
use Puzzle\QueryBuilder\Queries\Snippets;
10
11
abstract class AbstractJoin implements Join, Snippet
12
{
13
    private
14
        $table,
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 $table.

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...
15
        $using,
16
        $on;
17
18 23
    public function __construct(string $table, ?string $alias = null)
19
    {
20 23
        $this->table = new Snippets\TableName($table, $alias);
21 23
        $this->on = [];
22 23
    }
23
24
    /**
25
     * @param array[string]|string $column
0 ignored issues
show
Documentation introduced by
The doc-type array[string]|string could not be parsed: Expected "]" at position 2, but found "string". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
26
     */
27 9
    public function using($column): self
28
    {
29 9
        $this->on = [];
30
31 9
        $this->using = new Snippets\Using($column);
32
33 9
        return $this;
34
    }
35
36 16
    public function on(?string $leftColumn, ?string $rightColumn): self
37
    {
38 16
        $this->using = null;
39 16
        $this->on[] = new Snippets\On($leftColumn, $rightColumn);
40
41 16
        return $this;
42
    }
43
44 23
    public function toString(): string
45
    {
46 23
        $joinQueryPart = sprintf(
47 23
            '%s %s',
48 23
            $this->getJoinDeclaration(),
49 23
            $this->table->toString()
50
        );
51
52 23
        $joinQueryPart .= $this->buildOnConditionClause();
53 23
        $joinQueryPart .= $this->buildUsingConditionClause();
54
55 23
        return $joinQueryPart;
56
    }
57
58
    abstract protected function getJoinDeclaration(): string;
59
60 23
    private function buildUsingConditionClause(): string
61
    {
62 23
        if(!$this->using instanceof Snippet)
63
        {
64 17
            return '';
65
        }
66
67 9
        return ' ' . $this->using->toString();
68
    }
69
70 23
    private function buildOnConditionClause(): string
71
    {
72 23
        $conditionClause = array();
73
74 23
        foreach($this->on as $on)
75
        {
76 16
            if($on instanceof Snippet)
77
            {
78 16
                $conditionClause[] = $on->toString();
79
            }
80
        }
81
82 23
        return empty($conditionClause) ? '' : ' ' . implode('', $conditionClause);
83
    }
84
}
85