Completed
Push — 2.0 ( 1160ec...acba87 )
by Vermeulen
05:15
created

JoinTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 71
c 0
b 0
f 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A joinDefinePrefix() 0 6 1
A join() 0 5 1
A joinLeft() 0 5 1
A joinRight() 0 5 1
A createJoin() 0 10 1
1
<?php
2
3
namespace BfwSql\Queries;
4
5
trait JoinTrait
6
{
7
    /**
8
     * Define the sql prefix to use for each join object
9
     * 
10
     * @return void
11
     */
12
    protected function joinDefinePrefix()
13
    {
14
        $this->queriesParts['join']->setPartPrefix('INNER JOIN');
0 ignored issues
show
Bug introduced by
The property queriesParts does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
15
        $this->queriesParts['joinLeft']->setPartPrefix('LEFT JOIN');
16
        $this->queriesParts['joinRight']->setPartPrefix('RIGHT JOIN');
17
    }
18
    
19
    /**
20
     * Add a INNER JOIN to the request
21
     * 
22
     * @return $this
23
     */
24
    public function join(...$args): self
25
    {
26
        array_unshift($args, 'join');
27
        return $this->createJoin(...$args);
0 ignored issues
show
Bug introduced by
The call to createJoin() misses some required arguments starting with $table.
Loading history...
Documentation introduced by
$args is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
28
    }
29
    
30
    /**
31
     * Add a LEFT JOIN to the request
32
     * 
33
     * @return $this
34
     */
35
    public function joinLeft(...$args): self
36
    {
37
        array_unshift($args, 'joinLeft');
38
        return $this->createJoin(...$args);
0 ignored issues
show
Bug introduced by
The call to createJoin() misses some required arguments starting with $table.
Loading history...
Documentation introduced by
$args is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
39
    }
40
    
41
    /**
42
     * Add a RIGHT JOIN to the request
43
     * 
44
     * @return $this
45
     */
46
    public function joinRight(...$args): self
47
    {
48
        array_unshift($args, 'joinRight');
49
        return $this->createJoin(...$args);
0 ignored issues
show
Bug introduced by
The call to createJoin() misses some required arguments starting with $table.
Loading history...
Documentation introduced by
$args is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
50
    }
51
    
52
    /**
53
     * Add a (inner|left|right) join to the request
54
     * 
55
     * @param string       $joinType The name of the property in this class
56
     *  where the join is add
57
     * @param string|array $table    Name of the table concerned by
58
     * the join. Or an array with the table shortcut in key.
59
     * @param string       $on       SQL part "ON" for this join
60
     * @param string|array $columns  Columns from the table joined to add in
61
     *  the SELECT part of the request
62
     * 
63
     * @return $this
64
     */
65
    protected function createJoin(
66
        string $joinType,
67
        $table,
68
        string $on,
69
        $columns = null
70
    ): self {
71
        $this->queriesParts[$joinType]($table, $on, $columns);
72
        
73
        return $this;
74
    }
75
}
76