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

Table   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 197
Duplicated Lines 5.08 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 19
lcom 2
cbo 1
dl 10
loc 197
c 0
b 0
f 0
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A getShortcut() 0 4 1
A getColumns() 0 4 1
A getColumnsWithValue() 0 4 1
A setColumnsWithValue() 0 5 1
A __invoke() 0 5 1
A defineNameAndShortcut() 0 21 5
A defineColumns() 0 15 3
A obtainColumnsClassName() 0 8 2
A createColumnInstance() 0 4 1
A generate() 10 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace BfwSql\Queries\Parts;
4
5
use \Exception;
6
7
class Table extends AbstractPart
8
{
9
    /**
10
     * @const ERR_TABLE_INFOS_BAD_FORMAT Exception if the datas which contains
11
     * table name and shortcut have not the expected format.
12
     */
13
    const ERR_TABLE_INFOS_BAD_FORMAT = 2515001;
14
    
15
    /**
16
     * @var string $name The table name
17
     */
18
    protected $name = '';
19
    
20
    /**
21
     * @var string|null $shortcut The table shortcut
22
     */
23
    protected $shortcut;
24
    
25
    /**
26
     * @var \BfwSql\Queries\Parts\ColumnsList|null $columns Object containing
27
     * all columns of this table to use into the request
28
     */
29
    protected $columns;
30
    
31
    /**
32
     * @var boolean $columnsWithValue If the columnList object will contain
33
     * the column value or not (change the __invoke() format).
34
     */
35
    protected $columnsWithValue = false;
36
    
37
    /**
38
     * Getter accessor to property name
39
     * 
40
     * @return string
41
     */
42
    public function getName(): string
43
    {
44
        return $this->name;
45
    }
46
    
47
    /**
48
     * Getter accessor to property shortcut
49
     * 
50
     * @return string|null
51
     */
52
    public function getShortcut()
53
    {
54
        return $this->shortcut;
55
    }
56
57
    /**
58
     * Getter accessor to property columns
59
     * 
60
     * @return \BfwSql\Queries\Parts\ColumnsList|null
61
     */
62
    public function getColumns()
63
    {
64
        return $this->columns;
65
    }
66
    
67
    /**
68
     * Getter accessor to property columnsWithValue
69
     * 
70
     * @return bool
71
     */
72
    public function getColumnsWithValue(): bool
73
    {
74
        return $this->columnsWithValue;
75
    }
76
    
77
    /**
78
     * Setter accessor to property columnsWithValue
79
     * 
80
     * @param bool $columnsWithValue If the columnList object will contain
81
     * the column value or not (change the __invoke() format).
82
     * 
83
     * @return $this
84
     */
85
    public function setColumnsWithValue(bool $columnsWithValue): self
86
    {
87
        $this->columnsWithValue = $columnsWithValue;
88
        return $this;
89
    }
90
    
91
    /**
92
     * Magic method __invoke, used when the user call object like a function
93
     * @link http://php.net/manual/en/language.oop5.magic.php#object.invoke
94
     * 
95
     * @param string|array $nameInfos Table name.
96
     *  It can be an array if a table shortcut is declared.
97
     *  In array mode, the format is ['shortcut' => 'name']
98
     * @param string|array|null $columns (default: null) Columns into this
99
     *  table which will be use on the final query
100
     * 
101
     * @return void
102
     */
103
    public function __invoke($nameInfos, $columns = null)
104
    {
105
        $this->defineNameAndShortcut($nameInfos);
106
        $this->defineColumns($columns);
107
    }
108
    
109
    /**
110
     * Find the table name and the shortcut from info passed in argument.
111
     * 
112
     * @param string|array $nameInfos Table name.
113
     *  It can be an array if a table shortcut is declared.
114
     *  In array mode, the format is ['shortcut' => 'name']
115
     * 
116
     * @return void
117
     * 
118
     * @throws \Exception If the format is not correct (string or array)
119
     */
120
    protected function defineNameAndShortcut($nameInfos)
121
    {
122
        if (!is_array($nameInfos) && !is_string($nameInfos)) {
123
            throw new Exception(
124
                'Table information is not in the right format.',
125
                self::ERR_TABLE_INFOS_BAD_FORMAT
126
            );
127
        }
128
        
129
        if (is_array($nameInfos)) {
130
            $this->name     = (string) reset($nameInfos);
131
            $this->shortcut = (string) key($nameInfos);
132
        } else {
133
            $this->name     = (string) $nameInfos;
134
            $this->shortcut = null;
135
        }
136
        
137
        if (!empty($this->tablePrefix)) {
138
            $this->name = $this->tablePrefix.$this->name;
139
        }
140
    }
141
    
142
    /**
143
     * Define the columns object and declare columns passed in argument
144
     * 
145
     * @param string|array|null $columns (default: null) Columns into this
146
     *  table which will be use on the final query
147
     * 
148
     * @return void
149
     */
150
    protected function defineColumns($columns)
151
    {
152
        $class         = $this->obtainColumnsClassName();
153
        $this->columns = new $class($this->querySystem, $this);
154
        
155
        if ($columns === null) {
156
            return;
157
        }
158
        
159
        if (!is_array($columns)) {
160
            $columns = [$columns];
161
        }
162
        
163
        $this->columns->__invoke($columns);
164
    }
165
    
166
    /**
167
     * Find the correct class (with namespace) to use for columns object.
168
     * Based on the value of the property columnWithValue.
169
     * 
170
     * @return string
171
     */
172
    protected function obtainColumnsClassName(): string
173
    {
174
        if ($this->columnsWithValue === true) {
175
            return __NAMESPACE__.'\ColumnValueList';
176
        }
177
        
178
        return __NAMESPACE__.'\ColumnList';
179
    }
180
    
181
    /**
182
     * Instanciate the columnsList object.
183
     * To generate it before the call to __invoke() when is necessary
184
     */
185
    public function createColumnInstance()
186
    {
187
        $this->defineColumns(null);
188
    }
189
    
190
    /**
191
     * {@inheritdoc}
192
     */
193 View Code Duplication
    public function generate(): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
194
    {
195
        $partQuery = '`'.$this->name.'`';
196
        
197
        if ($this->shortcut !== null) {
198
            $partQuery .= ' AS `'.$this->shortcut.'`';
199
        }
200
        
201
        return $partQuery;
202
    }
203
}
204