CommandInterfaceProxy   F
last analyzed

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