Completed
Push — master ( b54f87...bde51f )
by Beniamin
04:50
created

AbstractTable::__toString()   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
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
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
    const ROOT_FROM = 1;
17
    const ROOT_UPDATE = 2;
18
    const ROOT_INSERT = 3;
19
20
    /**
21
     * @var AbstractBuilder $qb
22
     */
23
    private $qb;
24
25
    /**
26
     * @var string $tableAlias
27
     */
28
    private $tableAlias;
29
30
    /**
31
     * @var string $joinType
32
     */
33
    private $joinType;
34
35
    /**
36
     * @var int $root
37
     */
38
    private $rootType;
39
40
    /**
41
     * @var string $joinOn
42
     */
43
    private $joinOn;
44
45
    /**
46
     * @var bool $join
47
     */
48
    private $join = false;
49
50
    /**
51
     * @param AbstractBuilder $qb
52
     */
53 28
    public function __construct(AbstractBuilder $qb)
54
    {
55 28
        $this->qb = $qb;
56 28
    }
57
58
    /**
59
     * @return string
60
     */
61 18
    public function __toString()
62
    {
63 18
        return $this->qb->getReferenceManager()->register($this);
64
    }
65
66
    /**
67
     * @return string
68
     */
69
    abstract public function getTableName();
70
71
    /**
72
     * @return AbstractBuilder
73
     */
74 1
    public function getQueryBuilder()
75
    {
76 1
        return $this->qb;
77
    }
78
79
    /**
80
     * @return string
81
     */
82 26
    public function getAlias()
83
    {
84 26
        return $this->tableAlias;
85
    }
86
87
    /**
88
     * @param string $alias
89
     *
90
     * @return $this
91
     */
92 8
    public function setAlias($alias)
93
    {
94 8
        $this->tableAlias = $alias;
95
96 8
        return $this;
97
    }
98
99
    /**
100
     * @return bool
101
     */
102 26
    public function isJoin()
103
    {
104 26
        return $this->join;
105
    }
106
107
    /**
108
     * @return string
109
     */
110 3
    public function getJoinType()
111
    {
112 3
        return $this->joinType;
113
    }
114
115
    /**
116
     * @param string $joinType
117
     *
118
     * @return $this
119
     */
120 3
    public function setJoinType($joinType)
121
    {
122 3
        $this->joinType = $joinType;
123 3
        $this->join = true;
124
125 3
        return $this;
126
    }
127
128
    /**
129
     * @return bool
130
     */
131
    public function isRoot()
132
    {
133
        return null !== $this->rootType;
134
    }
135
136
    /**
137
     * @param int $rootType
138
     *
139
     * @return $this
140
     */
141
    public function setRootType($rootType)
142
    {
143
        $this->rootType = $rootType;
144
145
        return $this;
146
    }
147
148
    /**
149
     * @return int
150
     */
151
    public function getRootType()
152
    {
153
        return $this->rootType;
154
    }
155
156
    /**
157
     * @return string
158
     */
159 18
    public function getAliasOrName()
160
    {
161 18
        return $this->getAlias() ?: $this->getTableName();
162
    }
163
164
    /**
165
     * @param string $clause
166
     *
167
     * @return $this
168
     */
169 4
    public function addSelect($clause)
170
    {
171 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...
172
173 4
        return $this;
174
    }
175
176
    /**
177
     * @param string $clause
178
     *
179
     * @return $this
180
     */
181 2
    public function joinOn($clause)
182
    {
183 2
        $this->joinOn = $clause;
184
185 2
        return $this;
186
    }
187
188
    /**
189
     * @return string
190
     */
191 26
    public function getJoinOn()
192
    {
193 26
        return $this->joinOn;
194
    }
195
196
    /**
197
     * @param string $name
198
     *
199
     * @return string
200
     */
201 18
    public function column($name)
202
    {
203 18
        return $this . '.' . $name;
204
    }
205
}