Passed
Push — 4 ( 3b5c72...d8499a )
by Steve
07:17
created

NullDatabase   B

Complexity

Total Complexity 50

Size/Duplication

Total Lines 313
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 50
eloc 44
c 1
b 0
f 0
dl 0
loc 313
rs 8.4

50 Methods

Rating   Name   Duplication   Size   Complexity  
A query() 0 3 1
A transactionEnd() 0 2 1
A formattedDatetimeClause() 0 3 1
A selectDatabase() 0 2 1
A supportsExtensions() 0 3 1
A escapeIdentifier() 0 3 1
A random() 0 3 1
A datetimeDifferenceClause() 0 3 1
A nullCheckClause() 0 3 1
A preparedQuery() 0 3 1
A supportsCollations() 0 3 1
A canLock() 0 3 1
A getLock() 0 3 1
A concatOperator() 0 3 1
A getQueryBuilder() 0 3 1
A getConnector() 0 3 1
A getSchemaManager() 0 3 1
A getGeneratedID() 0 2 1
A getVersion() 0 3 1
A transactionStart() 0 2 1
A clearAllData() 0 3 1
A affectedRows() 0 3 1
A setQueryErrorMessage() 0 4 1
A transactionSavepoint() 0 2 1
A now() 0 3 1
A supportsLocks() 0 3 1
A comparisonClause() 0 9 1
A supportsTransactionMode() 0 3 1
A withTransaction() 0 6 1
A transactionDepth() 0 3 1
A transactionRollback() 0 2 1
A getSelectedDatabase() 0 2 1
A supportsTimezoneOverride() 0 3 1
A getDatabaseServer() 0 3 1
A supportsSavepoints() 0 3 1
A dropSelectedDatabase() 0 2 1
A searchEngine() 0 11 1
A releaseLock() 0 3 1
A setErrorMessage() 0 4 1
A escapeColumnKeys() 0 3 1
A connect() 0 2 1
A isActive() 0 3 1
A clearTable() 0 3 1
A supportsTransactions() 0 3 1
A escapeString() 0 3 1
A quoteString() 0 3 1
A databaseList() 0 3 1
A datetimeIntervalClause() 0 3 1
A databaseExists() 0 3 1
A manipulate() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like NullDatabase often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use NullDatabase, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace SilverStripe\ORM\Connect;
4
5
use BadMethodCallException;
6
use Exception;
7
8
/**
9
 * Utility class required due to bad coupling in framework.
10
 * Not every framework execution should require a working database connection.
11
 * For example, when generating class and config manifests for deployment bundles,
12
 * or when generating code in a silverstripe/graphql schema build.
13
 *
14
 * This class creates the required no-ops to fulfill the contract,
15
 * and create exceptions as required.
16
 *
17
 * It also avoids introducing new third party core dependencies that
18
 * would be required with https://github.com/tractorcow/silverstripe-proxy-db.
19
 *
20
 * @internal
21
 */
22
class NullDatabase extends Database
23
{
24
    /**
25
     * @var string
26
     */
27
    private $errorMessage = 'Using NullDatabase, cannot interact with database';
28
29
    /**
30
     * @var string
31
     */
32
    private $queryErrorMessage = 'Using NullDatabase, cannot execute query: %s';
33
34
    /**
35
     * @param string $msg
36
     */
37
    public function setErrorMessage(string $msg): self
38
    {
39
        $this->errorMessage = $msg;
40
        return $this;
41
    }
42
43
    /**
44
     * @param string $msg
45
     */
46
    public function setQueryErrorMessage(string $msg): self
47
    {
48
        $this->queryErrorMessage = $msg;
49
        return $this;
50
    }
51
52
    /**
53
     * @throws NullDatabaseException
54
     */
55
    public function query($sql, $errorLevel = E_USER_ERROR)
56
    {
57
        throw new NullDatabaseException(sprintf($this->queryErrorMessage, $sql));
58
    }
59
60
    /**
61
     * @throws NullDatabaseException
62
     */
63
    public function preparedQuery($sql, $parameters, $errorLevel = E_USER_ERROR)
64
    {
65
        throw new NullDatabaseException(sprintf($this->queryErrorMessage, $sql));
66
    }
67
68
    /**
69
     * @throws NullDatabaseException
70
     */
71
    public function getConnector()
72
    {
73
        throw new NullDatabaseException($this->errorMessage);
74
    }
75
76
    /**
77
     * @throws NullDatabaseException
78
     */
79
    public function getSchemaManager()
80
    {
81
        throw new NullDatabaseException($this->errorMessage);
82
    }
83
84
    /**
85
     * @throws NullDatabaseException
86
     */
87
    public function getQueryBuilder()
88
    {
89
        throw new NullDatabaseException($this->errorMessage);
90
    }
91
92
93
    public function getGeneratedID($table)
94
    {
95
        // no-op
96
    }
97
98
    public function isActive()
99
    {
100
        return true;
101
    }
102
103
    public function escapeString($value)
104
    {
105
        return $value;
106
    }
107
108
    public function quoteString($value)
109
    {
110
        return $value;
111
    }
112
113
    public function escapeIdentifier($value, $separator = '.')
114
    {
115
        return $value;
116
    }
117
118
    protected function escapeColumnKeys($fieldValues)
119
    {
120
        return $fieldValues;
121
    }
122
123
    /**
124
     * @throws NullDatabaseException
125
     */
126
    public function manipulate($manipulation)
127
    {
128
        throw new NullDatabaseException($this->errorMessage);
129
    }
130
131
    /**
132
     * @throws NullDatabaseException
133
     */
134
    public function clearAllData()
135
    {
136
        throw new NullDatabaseException($this->errorMessage);
137
    }
138
139
    /**
140
     * @throws NullDatabaseException
141
     */
142
    public function clearTable($table)
143
    {
144
        throw new NullDatabaseException($this->errorMessage);
145
    }
146
147
    public function nullCheckClause($field, $isNull)
148
    {
149
        return '';
150
    }
151
152
    public function comparisonClause(
153
        $field,
154
        $value,
155
        $exact = false,
156
        $negate = false,
157
        $caseSensitive = null,
158
        $parameterised = false
159
    ) {
160
        return '';
161
    }
162
163
    public function formattedDatetimeClause($date, $format)
164
    {
165
        return '';
166
    }
167
168
    public function datetimeIntervalClause($date, $interval)
169
    {
170
        return '';
171
    }
172
173
    public function datetimeDifferenceClause($date1, $date2)
174
    {
175
        return '';
176
    }
177
178
    public function concatOperator()
179
    {
180
        return '';
181
    }
182
183
    public function supportsCollations()
184
    {
185
        return false;
186
    }
187
188
    public function supportsTimezoneOverride()
189
    {
190
        return false;
191
    }
192
193
    public function getVersion()
194
    {
195
        return '';
196
    }
197
198
    public function getDatabaseServer()
199
    {
200
        return '';
201
    }
202
203
    public function affectedRows()
204
    {
205
        return 0;
206
    }
207
208
    public function searchEngine(
209
        $classesToSearch,
210
        $keywords,
211
        $start,
212
        $pageLength,
213
        $sortBy = "Relevance DESC",
214
        $extraFilter = "",
215
        $booleanSearch = false,
216
        $alternativeFileFilter = "",
217
        $invertedMatch = false
218
    ) {
219
        // no-op
220
    }
221
222
    public function supportsTransactions()
223
    {
224
        return false;
225
    }
226
227
    public function supportsSavepoints()
228
    {
229
        return false;
230
    }
231
232
233
    public function supportsTransactionMode(string $mode): bool
234
    {
235
        return false;
236
    }
237
238
    public function withTransaction(
239
        $callback,
240
        $errorCallback = null,
241
        $transactionMode = false,
242
        $errorIfTransactionsUnsupported = false
243
    ) {
244
        // no-op
245
    }
246
247
    public function supportsExtensions($extensions)
248
    {
249
        return false;
250
    }
251
252
    public function transactionStart($transactionMode = false, $sessionCharacteristics = false)
253
    {
254
        // no-op
255
    }
256
257
    public function transactionSavepoint($savepoint)
258
    {
259
        // no-op
260
    }
261
262
    public function transactionRollback($savepoint = false)
263
    {
264
        // no-op
265
    }
266
267
    public function transactionEnd($chain = false)
268
    {
269
        // no-op
270
    }
271
272
    public function transactionDepth()
273
    {
274
        return 0;
275
    }
276
277
    public function supportsLocks()
278
    {
279
        return false;
280
    }
281
282
    public function canLock($name)
283
    {
284
        return false;
285
    }
286
287
    public function getLock($name, $timeout = 5)
288
    {
289
        return false;
290
    }
291
292
    public function releaseLock($name)
293
    {
294
        return false;
295
    }
296
297
    public function connect($parameters)
298
    {
299
        // no-op
300
    }
301
302
    public function databaseExists($name)
303
    {
304
        return false;
305
    }
306
307
    public function databaseList()
308
    {
309
        return [];
310
    }
311
312
    public function selectDatabase($name, $create = false, $errorLevel = E_USER_ERROR)
313
    {
314
        // no-op
315
    }
316
317
    public function dropSelectedDatabase()
318
    {
319
        // no-op
320
    }
321
322
    public function getSelectedDatabase()
323
    {
324
        // no-op
325
    }
326
327
    public function now()
328
    {
329
        return '';
330
    }
331
332
    public function random()
333
    {
334
        return '';
335
    }
336
}
337