Completed
Push — master ( 1a3ae6...a9ee46 )
by Beniamin
02:36
created

AbstractTable::addSelect()   A

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 1
crap 1
1
<?php
2
3
namespace Phuria\SQLBuilder\Table;
4
5
use Phuria\SQLBuilder\QueryBuilder\AbstractBuilder;
6
7
/**
8
 * @author Beniamin Jonatan Šimko <[email protected]>
9
 */
10
abstract class AbstractTable
11
{
12
    const CROSS_JOIN = 'CROSS JOIN';
13
    const LEFT_JOIN = 'LEFT JOIN';
14
    const INNER_JOIN = 'INNER JOIN';
15
16
    /**
17
     * @var AbstractBuilder $qb
18
     */
19
    private $qb;
20
21
    /**
22
     * @var string $tableAlias
23
     */
24
    private $tableAlias;
25
26
    /**
27
     * @var string $joinType
28
     */
29
    private $joinType;
30
31
    /**
32
     * @var string $joinOn
33
     */
34
    private $joinOn;
35
36
    /**
37
     * @var bool $join
38
     */
39
    private $join = false;
40
41
    /**
42
     * @param AbstractBuilder $qb
43
     */
44 30
    public function __construct(AbstractBuilder $qb)
45
    {
46 30
        $this->qb = $qb;
47 30
    }
48
49
    /**
50
     * @return string
51
     */
52 20
    public function __toString()
53
    {
54 20
        return $this->qb->getReferenceManager()->register($this);
55
    }
56
57
    /**
58
     * @return string
59
     */
60
    abstract public function getTableName();
61
62
    /**
63
     * @return AbstractBuilder
64
     */
65 1
    public function getQueryBuilder()
66
    {
67 1
        return $this->qb;
68
    }
69
70
    /**
71
     * @return string
72
     */
73 28
    public function getAlias()
74
    {
75 28
        return $this->tableAlias;
76
    }
77
78
    /**
79
     * @param string $alias
80
     *
81
     * @return $this
82
     */
83 9
    public function setAlias($alias)
84
    {
85 9
        $this->tableAlias = $alias;
86
87 9
        return $this;
88
    }
89
90
    /**
91
     * @return bool
92
     */
93 28
    public function isJoin()
94
    {
95 28
        return $this->join;
96
    }
97
98
    /**
99
     * @return string
100
     */
101 3
    public function getJoinType()
102
    {
103 3
        return $this->joinType;
104
    }
105
106
    /**
107
     * @param string $joinType
108
     *
109
     * @return $this
110
     */
111 3
    public function setJoinType($joinType)
112
    {
113 3
        $this->joinType = $joinType;
114 3
        $this->join = true;
115
116 3
        return $this;
117
    }
118
119
    /**
120
     * @return string
121
     */
122 20
    public function getAliasOrName()
123
    {
124 20
        return $this->getAlias() ?: $this->getTableName();
125
    }
126
127
    /**
128
     * @param string $clause
129
     *
130
     * @return $this
131
     */
132 4
    public function addSelect($clause)
133
    {
134 4
        $this->qb->addSelect($clause);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Phuria\SQLBuilder\QueryBuilder\AbstractBuilder as the method addSelect() does only exist in the following sub-classes of Phuria\SQLBuilder\QueryBuilder\AbstractBuilder: Phuria\SQLBuilder\QueryBuilder\SelectBuilder. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
135
136 4
        return $this;
137
    }
138
139
    /**
140
     * @param string $clause
141
     *
142
     * @return $this
143
     */
144 2
    public function joinOn($clause)
145
    {
146 2
        $this->joinOn = $clause;
147
148 2
        return $this;
149
    }
150
151
    /**
152
     * @return string
153
     */
154 28
    public function getJoinOn()
155
    {
156 28
        return $this->joinOn;
157
    }
158
159
    /**
160
     * @param string $name
161
     *
162
     * @return string
163
     */
164 20
    public function column($name)
165
    {
166 20
        return $this . '.' . $name;
167
    }
168
}