Completed
Push — master ( ece51a...7e2c41 )
by Beniamin
02:51
created

JoinComponentTrait::straightJoin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 3
crap 1
1
<?php
2
3
/**
4
 * This file is part of Phuria SQL Builder package.
5
 *
6
 * Copyright (c) 2016 Beniamin Jonatan Šimko
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Phuria\SQLBuilder\QueryBuilder\Component;
13
14
use Phuria\SQLBuilder\JoinType;
15
use Phuria\SQLBuilder\QueryBuilder\BuilderInterface;
16
use Phuria\SQLBuilder\Table\AbstractTable;
17
use Phuria\SQLBuilder\TableFactory\TableFactoryInterface;
18
19
/**
20
 * @author Beniamin Jonatan Šimko <[email protected]>
21
 */
22
trait JoinComponentTrait
23
{
24
    /**
25
     * @var array $joinTables
26
     */
27
    private $joinTables = [];
28
29
    /**
30
     * @return TableFactoryInterface
31
     */
32
    abstract protected function getTableFactory();
33
    /**
34
     * @return BuilderInterface
35
     */
36
    abstract public function getQueryBuilder();
37
38
    /**
39
     * @param string      $joinType
40
     * @param mixed       $table
41
     * @param string|null $alias
42
     * @param string|null $joinOn
43
     *
44
     * @return AbstractTable
45
     */
46 3
    private function doJoin($joinType, $table, $alias = null, $joinOn = null)
47
    {
48 3
        $this->joinTables[] = $table = $this->getTableFactory()->createNewTable($table, $this->getQueryBuilder());
49 3
        $table->setJoinType($joinType);
50
51 3
        if ($alias) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $alias of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
52 1
            $table->setAlias($alias);
53 1
        }
54
55 3
        if ($joinOn) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $joinOn of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
56 1
            $table->joinOn($joinOn);
57 1
        }
58
59 3
        return $table;
60
    }
61
62
    /**
63
     * @param mixed       $table
64
     * @param string|null $alias
65
     * @param string|null $joinOn
66
     *
67
     * @return AbstractTable
68
     */
69 3
    public function join($table, $alias = null, $joinOn = null)
70
    {
71 3
        return $this->doJoin(JoinType::JOIN, $table, $alias, $joinOn);
72
    }
73
74
    /**
75
     * @param mixed       $table
76
     * @param string|null $alias
77
     * @param string|null $joinOn
78
     *
79
     * @return AbstractTable
80
     */
81 1
    public function straightJoin($table, $alias = null, $joinOn = null)
82
    {
83 1
        return $this->doJoin(JoinType::STRAIGHT_JOIN, $table, $alias, $joinOn);
84
    }
85
86
    /**
87
     * @param mixed       $table
88
     * @param string|null $alias
89
     * @param string|null $joinOn
90
     *
91
     * @return AbstractTable
92
     */
93 1
    public function crossJoin($table, $alias = null, $joinOn = null)
94
    {
95 1
        return $this->doJoin(JoinType::CROSS_JOIN, $table, $alias, $joinOn);
96
    }
97
98
    /**
99
     * @param mixed       $table
100
     * @param string|null $alias
101
     * @param string|null $joinOn
102
     *
103
     * @return AbstractTable
104
     */
105 1
    public function leftJoin($table, $alias = null, $joinOn = null)
106
    {
107 1
        return $this->doJoin(JoinType::LEFT_JOIN, $table, $alias, $joinOn);
108
    }
109
110
    /**
111
     * @param mixed       $table
112
     * @param string|null $alias
113
     * @param string|null $joinOn
114
     *
115
     * @return AbstractTable
116
     */
117 1
    public function rightJoin($table, $alias = null, $joinOn = null)
118
    {
119 1
        return $this->doJoin(JoinType::RIGHT_JOIN, $table, $alias, $joinOn);
120
    }
121
122
    /**
123
     * @param mixed       $table
124
     * @param string|null $alias
125
     * @param string|null $joinOn
126
     *
127
     * @return AbstractTable
128
     */
129 1
    public function innerJoin($table, $alias = null, $joinOn = null)
130
    {
131 1
        return $this->doJoin(JoinType::INNER_JOIN, $table, $alias, $joinOn);
132
    }
133
134
    /**
135
     * @return array
136
     */
137 1
    public function getJoinTables()
138
    {
139 1
        return $this->joinTables;
140
    }
141
}