Completed
Push — master ( 52a6e0...c01d36 )
by Dmitriy
51s queued 49s
created

CommandInterfaceProxy   F

Complexity

Total Complexity 64

Size/Duplication

Total Lines 513
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 116
c 1
b 0
f 0
dl 0
loc 513
rs 3.28
wmc 64

56 Methods

Rating   Name   Duplication   Size   Complexity  
A getParams() 0 3 1
A queryAll() 0 14 2
A collectQueryStart() 0 8 1
A dropView() 0 3 1
A upsert() 0 7 1
A checkIntegrity() 0 3 1
A __construct() 0 4 1
A addColumn() 0 3 1
A bindValue() 0 3 1
A query() 0 15 2
A createTable() 0 3 1
A addForeignKey() 0 10 1
A dropCommentFromTable() 0 3 1
A bindParam() 0 8 1
A prepare() 0 3 1
A execute() 0 14 2
A dropIndex() 0 3 1
A addCommentOnColumn() 0 3 1
A dropUnique() 0 3 1
A insertWithReturningPks() 0 3 1
A createView() 0 3 1
A dropPrimaryKey() 0 3 1
A dropCheck() 0 3 1
A bindValues() 0 3 1
A renameColumn() 0 3 1
A setRetryHandler() 0 3 1
A alterColumn() 0 3 1
A cancel() 0 3 1
A addCheck() 0 3 1
A resetSequence() 0 3 1
A dropTable() 0 3 1
A renameTable() 0 3 1
A showDatabases() 0 3 1
A createIndex() 0 8 1
A addCommentOnTable() 0 3 1
A getSql() 0 3 1
A queryOne() 0 14 3
A collectQueryEnd() 0 3 1
A insert() 0 3 1
A delete() 0 3 1
A setSql() 0 3 1
A dropForeignKey() 0 3 1
A collectQueryError() 0 3 1
A getRawSql() 0 3 1
A addPrimaryKey() 0 3 1
A addDefaultValue() 0 3 1
A dropDefaultValue() 0 3 1
A truncateTable() 0 3 1
A update() 0 3 1
A queryScalar() 0 14 3
A queryColumn() 0 14 2
A dropCommentFromColumn() 0 3 1
A setRawSql() 0 3 1
A addUnique() 0 3 1
A dropColumn() 0 3 1
A batchInsert() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like CommandInterfaceProxy 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 CommandInterfaceProxy, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Debug;
6
7
use Closure;
8
use Throwable;
9
use Yiisoft\Db\Command\CommandInterface;
10
use Yiisoft\Db\Query\Data\DataReaderInterface;
11
use Yiisoft\Db\Query\QueryInterface;
12
13
final class CommandInterfaceProxy implements CommandInterface
14
{
15
    public function __construct(
16
        private CommandInterface $decorated,
17
        private DatabaseCollector $collector
18
    ) {
19
    }
20
21
    /**
22
     * @psalm-suppress  MixedArgument
23
     */
24
    public function addCheck(string $table, string $name, string $expression): static
25
    {
26
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
27
    }
28
29
    /**
30
     * @psalm-suppress  MixedArgument
31
     */
32
    public function addColumn(string $table, string $column, string $type): static
33
    {
34
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
35
    }
36
37
    /**
38
     * @psalm-suppress  MixedArgument
39
     */
40
    public function addCommentOnColumn(string $table, string $column, string $comment): static
41
    {
42
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
43
    }
44
45
    /**
46
     * @psalm-suppress  MixedArgument
47
     */
48
    public function addCommentOnTable(string $table, string $comment): static
49
    {
50
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
51
    }
52
53
    /**
54
     * @psalm-suppress  MixedArgument
55
     */
56
    public function addDefaultValue(string $table, string $name, string $column, mixed $value): static
57
    {
58
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
59
    }
60
61
    /**
62
     * @psalm-suppress  MixedArgument
63
     */
64
    public function addForeignKey(
65
        string $table,
66
        string $name,
67
        array|string $columns,
68
        string $referenceTable,
69
        array|string $referenceColumns,
70
        string $delete = null,
71
        string $update = null
72
    ): static {
73
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
74
    }
75
76
    /**
77
     * @psalm-suppress  MixedArgument
78
     */
79
    public function addPrimaryKey(string $table, string $name, array|string $columns): static
80
    {
81
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
82
    }
83
84
    /**
85
     * @psalm-suppress  MixedArgument
86
     */
87
    public function addUnique(string $table, string $name, array|string $columns): static
88
    {
89
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
90
    }
91
92
    /**
93
     * @psalm-suppress  MixedArgument
94
     */
95
    public function alterColumn(string $table, string $column, string $type): static
96
    {
97
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
98
    }
99
100
    /**
101
     * @psalm-suppress  MixedArgument
102
     */
103
    public function batchInsert(string $table, array $columns, iterable $rows): static
104
    {
105
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
106
    }
107
108
    /**
109
     * @psalm-suppress  MixedArgument
110
     */
111
    public function bindParam(
112
        int|string $name,
113
        mixed &$value,
114
        int $dataType = null,
115
        int $length = null,
116
        mixed $driverOptions = null
117
    ): static {
118
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
119
    }
120
121
    /**
122
     * @psalm-suppress  MixedArgument
123
     */
124
    public function bindValue(int|string $name, mixed $value, int $dataType = null): static
125
    {
126
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
127
    }
128
129
    /**
130
     * @psalm-suppress  MixedArgument
131
     */
132
    public function bindValues(array $values): static
133
    {
134
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
135
    }
136
137
    public function cancel(): void
138
    {
139
        $this->decorated->{__FUNCTION__}(...func_get_args());
140
    }
141
142
    /**
143
     * @psalm-suppress  MixedArgument
144
     */
145
    public function checkIntegrity(string $schema, string $table, bool $check = true): static
146
    {
147
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
148
    }
149
150
    /**
151
     * @psalm-suppress  MixedArgument
152
     */
153
    public function createIndex(
154
        string $table,
155
        string $name,
156
        array|string $columns,
157
        string $indexType = null,
158
        string $indexMethod = null
159
    ): static {
160
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
161
    }
162
163
    /**
164
     * @psalm-suppress  MixedArgument
165
     */
166
    public function createTable(string $table, array $columns, string $options = null): static
167
    {
168
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
169
    }
170
171
    /**
172
     * @psalm-suppress  MixedArgument
173
     */
174
    public function createView(string $viewName, QueryInterface|string $subQuery): static
175
    {
176
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
177
    }
178
179
    /**
180
     * @psalm-suppress  MixedArgument
181
     */
182
    public function delete(string $table, array|string $condition = '', array $params = []): static
183
    {
184
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
185
    }
186
187
    /**
188
     * @psalm-suppress  MixedArgument
189
     */
190
    public function dropCheck(string $table, string $name): static
191
    {
192
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
193
    }
194
195
    /**
196
     * @psalm-suppress  MixedArgument
197
     */
198
    public function dropColumn(string $table, string $column): static
199
    {
200
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
201
    }
202
203
    /**
204
     * @psalm-suppress  MixedArgument
205
     */
206
    public function dropCommentFromColumn(string $table, string $column): static
207
    {
208
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
209
    }
210
211
    /**
212
     * @psalm-suppress  MixedArgument
213
     */
214
    public function dropCommentFromTable(string $table): static
215
    {
216
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
217
    }
218
219
    /**
220
     * @psalm-suppress  MixedArgument
221
     */
222
    public function dropDefaultValue(string $table, string $name): static
223
    {
224
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
225
    }
226
227
    /**
228
     * @psalm-suppress  MixedArgument
229
     */
230
    public function dropForeignKey(string $table, string $name): static
231
    {
232
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
233
    }
234
235
    /**
236
     * @psalm-suppress  MixedArgument
237
     */
238
    public function dropIndex(string $table, string $name): static
239
    {
240
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
241
    }
242
243
    /**
244
     * @psalm-suppress  MixedArgument
245
     */
246
    public function dropPrimaryKey(string $table, string $name): static
247
    {
248
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
249
    }
250
251
    /**
252
     * @psalm-suppress  MixedArgument
253
     */
254
    public function dropTable(string $table): static
255
    {
256
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
257
    }
258
259
    /**
260
     * @psalm-suppress  MixedArgument
261
     */
262
    public function dropUnique(string $table, string $name): static
263
    {
264
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
265
    }
266
267
    /**
268
     * @psalm-suppress  MixedArgument
269
     */
270
    public function dropView(string $viewName): static
271
    {
272
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
273
    }
274
275
    /**
276
     * @psalm-suppress PossiblyUndefinedArrayOffset
277
     */
278
    public function execute(): int
279
    {
280
        [$callStack] = debug_backtrace();
281
282
        $id = random_bytes(36);
283
        $this->collectQueryStart($id, $callStack['file'] . ':' . $callStack['line']);
284
        try {
285
            $result = $this->decorated->execute();
286
        } catch (Throwable $e) {
287
            $this->collectQueryError($id, $e);
288
            throw $e;
289
        }
290
        $this->collectQueryEnd($id, $result);
291
        return $result;
292
    }
293
294
    public function getParams(bool $asValues = true): array
295
    {
296
        return $this->decorated->getParams($asValues);
297
    }
298
299
    public function getRawSql(): string
300
    {
301
        return $this->decorated->getRawSql();
302
    }
303
304
    public function getSql(): string
305
    {
306
        return $this->decorated->getSql();
307
    }
308
309
    /**
310
     * @psalm-suppress  MixedArgument
311
     */
312
    public function insert(string $table, QueryInterface|array $columns): static
313
    {
314
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
315
    }
316
317
    /**
318
     * @psalm-suppress MixedInferredReturnType, MixedReturnStatement
319
     */
320
    public function insertWithReturningPks(string $table, array $columns): bool|array
321
    {
322
        return $this->decorated->{__FUNCTION__}(...func_get_args());
323
    }
324
325
    public function prepare(bool $forRead = null): void
326
    {
327
        $this->decorated->{__FUNCTION__}(...func_get_args());
328
    }
329
330
    /**
331
     * @psalm-suppress PossiblyUndefinedArrayOffset
332
     */
333
    public function query(): DataReaderInterface
334
    {
335
        [$callStack] = debug_backtrace();
336
337
        $id = random_bytes(36);
338
        $this->collectQueryStart($id, $callStack['file'] . ':' . $callStack['line']);
339
        try {
340
            $result = $this->decorated->query();
341
        } catch (Throwable $e) {
342
            $this->collectQueryError($id, $e);
343
            throw $e;
344
        }
345
        $rowsNumber = $result->count();
346
        $this->collectQueryEnd($id, $rowsNumber);
347
        return $result;
348
    }
349
350
    /**
351
     * @psalm-suppress PossiblyUndefinedArrayOffset
352
     */
353
    public function queryAll(): array
354
    {
355
        [$callStack] = debug_backtrace();
356
357
        $id = random_bytes(36);
358
        $this->collectQueryStart($id, $callStack['file'] . ':' . $callStack['line']);
359
        try {
360
            $result = $this->decorated->queryAll();
361
        } catch (Throwable $e) {
362
            $this->collectQueryError($id, $e);
363
            throw $e;
364
        }
365
        $this->collectQueryEnd($id, count($result));
366
        return $result;
367
    }
368
369
    /**
370
     * @psalm-suppress PossiblyUndefinedArrayOffset
371
     */
372
    public function queryColumn(): array
373
    {
374
        [$callStack] = debug_backtrace();
375
376
        $id = random_bytes(36);
377
        $this->collectQueryStart($id, $callStack['file'] . ':' . $callStack['line']);
378
        try {
379
            $result = $this->decorated->queryColumn();
380
        } catch (Throwable $e) {
381
            $this->collectQueryError($id, $e);
382
            throw $e;
383
        }
384
        $this->collectQueryEnd($id, count($result));
385
        return $result;
386
    }
387
388
    /**
389
     * @psalm-suppress PossiblyUndefinedArrayOffset
390
     */
391
    public function queryOne(): array|null
392
    {
393
        [$callStack] = debug_backtrace();
394
395
        $id = random_bytes(36);
396
        $this->collectQueryStart($id, $callStack['file'] . ':' . $callStack['line']);
397
        try {
398
            $result = $this->decorated->queryOne();
399
        } catch (Throwable $e) {
400
            $this->collectQueryError($id, $e);
401
            throw $e;
402
        }
403
        $this->collectQueryEnd($id, $result === null ? 0 : 1);
404
        return $result;
405
    }
406
407
    /**
408
     * @psalm-suppress PossiblyUndefinedArrayOffset
409
     */
410
    public function queryScalar(): bool|string|null|int|float
411
    {
412
        [$callStack] = debug_backtrace();
413
414
        $id = random_bytes(36);
415
        $this->collectQueryStart($id, $callStack['file'] . ':' . $callStack['line']);
416
        try {
417
            $result = $this->decorated->queryScalar();
418
        } catch (Throwable $e) {
419
            $this->collectQueryError($id, $e);
420
            throw $e;
421
        }
422
        $this->collectQueryEnd($id, $result === null ? 0 : 1);
423
        return $result;
424
    }
425
426
    /**
427
     * @psalm-suppress  MixedArgument
428
     */
429
    public function renameColumn(string $table, string $oldName, string $newName): static
430
    {
431
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
432
    }
433
434
    /**
435
     * @psalm-suppress  MixedArgument
436
     */
437
    public function renameTable(string $table, string $newName): static
438
    {
439
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
440
    }
441
442
    /**
443
     * @psalm-suppress  MixedArgument
444
     */
445
    public function resetSequence(string $table, int|string $value = null): static
446
    {
447
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
448
    }
449
450
    /**
451
     * @psalm-suppress  MixedArgument
452
     */
453
    public function setRawSql(string $sql): static
454
    {
455
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
456
    }
457
458
    /**
459
     * @psalm-suppress  MixedArgument
460
     */
461
    public function setRetryHandler(?Closure $handler): static
462
    {
463
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
464
    }
465
466
    /**
467
     * @psalm-suppress  MixedArgument
468
     */
469
    public function setSql(string $sql): static
470
    {
471
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
472
    }
473
474
    /**
475
     * @psalm-suppress  MixedArgument
476
     */
477
    public function truncateTable(string $table): static
478
    {
479
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
480
    }
481
482
    /**
483
     * @psalm-suppress  MixedArgument
484
     */
485
    public function update(string $table, array $columns, array|string $condition = '', array $params = []): static
486
    {
487
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
488
    }
489
490
    /**
491
     * @psalm-suppress  MixedArgument
492
     */
493
    public function upsert(
494
        string $table,
495
        QueryInterface|array $insertColumns,
496
        bool|array $updateColumns = true,
497
        array $params = []
498
    ): static {
499
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
500
    }
501
502
    private function collectQueryStart(string $id, string $line): void
503
    {
504
        $this->collector->collectQueryStart(
505
            id: $id,
506
            sql: $this->decorated->getSql(),
507
            rawSql: $this->decorated->getRawSql(),
508
            params: $this->decorated->getParams(),
509
            line: $line,
510
        );
511
    }
512
513
    private function collectQueryError(string $id, Throwable $exception): void
514
    {
515
        $this->collector->collectQueryError($id, $exception);
516
    }
517
518
    private function collectQueryEnd(string $id, int $rowsNumber): void
519
    {
520
        $this->collector->collectQueryEnd($id, $rowsNumber);
521
    }
522
523
    public function showDatabases(): array
524
    {
525
        return $this->decorated->showDatabases();
526
    }
527
}
528