DDC3634LastInsertIdMockingConnection   F
last analyzed

Complexity

Total Complexity 61

Size/Duplication

Total Lines 317
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 66
c 1
b 0
f 0
dl 0
loc 317
rs 3.52
wmc 61

61 Methods

Rating   Name   Duplication   Size   Complexity  
A beginTransaction() 0 3 1
A delete() 0 3 1
A executeCacheQuery() 0 3 1
A isConnected() 0 3 1
A getPort() 0 3 1
A fetchAssoc() 0 3 1
A setFetchMode() 0 3 1
A getWrappedConnection() 0 3 1
A insert() 0 3 1
A getTransactionIsolation() 0 3 1
A update() 0 3 1
A getParams() 0 3 1
A getTransactionNestingLevel() 0 3 1
A isRollbackOnly() 0 3 1
A rollbackSavepoint() 0 3 1
A getEventManager() 0 3 1
A ping() 0 3 1
A getConfiguration() 0 3 1
A executeUpdate() 0 3 1
A isAutoCommit() 0 3 1
A connect() 0 3 1
A setRollbackOnly() 0 3 1
A getPassword() 0 3 1
A fetchAll() 0 3 1
A resolveParams() 0 3 1
A getNestTransactionsWithSavepoints() 0 3 1
A close() 0 3 1
A errorCode() 0 3 1
A getNestedTransactionSavePointName() 0 3 1
A setAutoCommit() 0 3 1
A prepare() 0 3 1
A getExpressionBuilder() 0 3 1
A quoteIdentifier() 0 3 1
A forwardCall() 0 5 1
A fetchArray() 0 3 1
A project() 0 3 1
A convertToDatabaseValue() 0 3 1
A getDatabase() 0 3 1
A errorInfo() 0 3 1
A commit() 0 3 1
A quote() 0 3 1
A executeQuery() 0 3 1
A createQueryBuilder() 0 3 1
A releaseSavepoint() 0 3 1
A rollBack() 0 3 1
A convertToPHPValue() 0 3 1
A getHost() 0 3 1
A getDatabasePlatform() 0 3 1
A getUsername() 0 3 1
A getDriver() 0 3 1
A lastInsertId() 0 3 1
A transactional() 0 3 1
A setNestTransactionsWithSavepoints() 0 3 1
A exec() 0 3 1
A __construct() 0 4 1
A query() 0 3 1
A getSchemaManager() 0 3 1
A fetchColumn() 0 3 1
A isTransactionActive() 0 3 1
A setTransactionIsolation() 0 3 1
A createSavepoint() 0 3 1

How to fix   Complexity   

Complex Class

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

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Functional\Ticket;
6
7
use Closure;
8
use Doctrine\Common\EventManager;
9
use Doctrine\DBAL\Cache\QueryCacheProfile;
10
use Doctrine\DBAL\Configuration;
11
use Doctrine\DBAL\Connection;
12
use Doctrine\DBAL\Driver;
13
use Doctrine\DBAL\Driver\ResultStatement;
14
use Doctrine\DBAL\Driver\Statement;
15
use Doctrine\DBAL\Platforms\AbstractPlatform;
16
use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
17
use Doctrine\DBAL\Query\QueryBuilder;
18
use Doctrine\DBAL\Schema\AbstractSchemaManager;
19
use Doctrine\ORM\Annotation as ORM;
20
use Doctrine\ORM\EntityManager;
21
use Doctrine\ORM\Tools\ToolsException;
22
use Doctrine\Tests\OrmFunctionalTestCase;
23
use const PHP_INT_MAX;
24
use function call_user_func_array;
25
use function debug_backtrace;
26
27
/**
28
 * @group DDC-3634
29
 */
30
class DDC3634Test extends OrmFunctionalTestCase
31
{
32
    protected function setUp() : void
33
    {
34
        parent::setUp();
35
36
        $metadata = $this->em->getClassMetadata(DDC3634Entity::class);
37
38
        if (! $metadata->getValueGenerationPlan()->containsDeferred()) {
0 ignored issues
show
Bug introduced by
The method getValueGenerationPlan() does not exist on Doctrine\Persistence\Mapping\ClassMetadata. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
        if (! $metadata->/** @scrutinizer ignore-call */ getValueGenerationPlan()->containsDeferred()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
39
            $this->markTestSkipped('Need a post-insert ID generator in order to make this test work correctly');
40
        }
41
42
        try {
43
            $this->schemaTool->createSchema([
44
                $metadata,
45
                $this->em->getClassMetadata(DDC3634JTIBaseEntity::class),
46
                $this->em->getClassMetadata(DDC3634JTIChildEntity::class),
47
            ]);
48
        } catch (ToolsException $e) {
49
            // schema already in place
50
        }
51
    }
52
53
    public function testSavesVeryLargeIntegerAutoGeneratedValue() : void
54
    {
55
        $veryLargeId = PHP_INT_MAX . PHP_INT_MAX;
56
57
        $entityManager = EntityManager::create(
58
            new DDC3634LastInsertIdMockingConnection($veryLargeId, $this->em->getConnection()),
59
            $this->em->getConfiguration()
60
        );
61
62
        $entity = new DDC3634Entity();
63
64
        $entityManager->persist($entity);
65
        $entityManager->flush();
66
67
        self::assertSame($veryLargeId, $entity->id);
68
    }
69
70
    public function testSavesIntegerAutoGeneratedValueAsString() : void
71
    {
72
        $entity = new DDC3634Entity();
73
74
        $this->em->persist($entity);
75
        $this->em->flush();
76
77
        self::assertInternalType('string', $entity->id);
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertInternalType() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3369 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

77
        /** @scrutinizer ignore-deprecated */ self::assertInternalType('string', $entity->id);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
78
    }
79
80
    public function testSavesIntegerAutoGeneratedValueAsStringWithJoinedInheritance() : void
81
    {
82
        $entity = new DDC3634JTIChildEntity();
83
84
        $this->em->persist($entity);
85
        $this->em->flush();
86
87
        self::assertInternalType('string', $entity->id);
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertInternalType() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3369 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

87
        /** @scrutinizer ignore-deprecated */ self::assertInternalType('string', $entity->id);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
88
    }
89
}
90
91
/** @ORM\Entity */
92
class DDC3634Entity
93
{
94
    /** @ORM\Id @ORM\Column(type="bigint") @ORM\GeneratedValue(strategy="AUTO") */
95
    public $id;
96
}
97
98
/**
99
 * @ORM\Entity
100
 * @ORM\InheritanceType("JOINED")
101
 * @ORM\DiscriminatorMap({
102
 *  DDC3634JTIBaseEntity::class  = DDC3634JTIBaseEntity::class,
103
 *  DDC3634JTIChildEntity::class = DDC3634JTIChildEntity::class,
104
 * })
105
 */
106
class DDC3634JTIBaseEntity
107
{
108
    /** @ORM\Id @ORM\Column(type="bigint") @ORM\GeneratedValue(strategy="AUTO") */
109
    public $id;
110
}
111
112
/** @ORM\Entity */
113
class DDC3634JTIChildEntity extends DDC3634JTIBaseEntity
114
{
115
}
116
117
class DDC3634LastInsertIdMockingConnection extends Connection
118
{
119
    /** @var Connection */
120
    private $realConnection;
121
122
    /** @var string */
123
    private $identifier;
124
125
    /**
126
     * @param string $identifier
127
     */
128
    public function __construct($identifier, Connection $realConnection)
129
    {
130
        $this->realConnection = $realConnection;
131
        $this->identifier     = $identifier;
132
    }
133
134
    private function forwardCall()
135
    {
136
        $trace = debug_backtrace(0, 2)[1];
137
138
        return call_user_func_array([$this->realConnection, $trace['function']], $trace['args']);
139
    }
140
141
    public function getParams() : array
142
    {
143
        return $this->forwardCall();
144
    }
145
146
    public function getDatabase() : string
147
    {
148
        return $this->forwardCall();
149
    }
150
151
    public function getHost()
152
    {
153
        return $this->forwardCall();
154
    }
155
156
    public function getPort()
157
    {
158
        return $this->forwardCall();
159
    }
160
161
    public function getUsername()
162
    {
163
        return $this->forwardCall();
164
    }
165
166
    public function getPassword()
167
    {
168
        return $this->forwardCall();
169
    }
170
171
    public function getDriver() : Driver
172
    {
173
        return $this->forwardCall();
174
    }
175
176
    public function getConfiguration() : Configuration
177
    {
178
        return $this->forwardCall();
179
    }
180
181
    public function getEventManager() : EventManager
182
    {
183
        return $this->forwardCall();
184
    }
185
186
    public function getDatabasePlatform() : AbstractPlatform
187
    {
188
        return $this->forwardCall();
189
    }
190
191
    public function getExpressionBuilder() : ExpressionBuilder
192
    {
193
        return $this->forwardCall();
194
    }
195
196
    public function connect() : void
197
    {
198
        $this->forwardCall();
199
    }
200
201
    public function isAutoCommit() : bool
202
    {
203
        return $this->forwardCall();
204
    }
205
206
    public function setAutoCommit($autoCommit) : void
207
    {
208
        $this->forwardCall();
209
    }
210
211
    public function setFetchMode($fetchMode) : void
212
    {
213
        $this->forwardCall();
214
    }
215
216
    public function fetchAssoc($statement, array $params = [], array $types = [])
217
    {
218
        return $this->forwardCall();
219
    }
220
221
    public function fetchArray($statement, array $params = [], array $types = [])
222
    {
223
        return $this->forwardCall();
224
    }
225
226
    public function fetchColumn($statement, array $params = [], $column = 0, array $types = [])
227
    {
228
        return $this->forwardCall();
229
    }
230
231
    public function isConnected() : bool
232
    {
233
        return $this->forwardCall();
234
    }
235
236
    public function isTransactionActive() : bool
237
    {
238
        return $this->forwardCall();
239
    }
240
241
    public function delete($tableExpression, array $identifier, array $types = []) : int
242
    {
243
        return $this->forwardCall();
244
    }
245
246
    public function close() : void
247
    {
248
        $this->forwardCall();
249
    }
250
251
    public function setTransactionIsolation($level) : void
252
    {
253
        $this->forwardCall();
254
    }
255
256
    public function getTransactionIsolation() : int
257
    {
258
        return $this->forwardCall();
259
    }
260
261
    public function update($tableExpression, array $data, array $identifier, array $types = []) : int
262
    {
263
        return $this->forwardCall();
264
    }
265
266
    public function insert($tableExpression, array $data, array $types = []) : int
267
    {
268
        return $this->forwardCall();
269
    }
270
271
    public function quoteIdentifier($str) : string
272
    {
273
        return $this->forwardCall();
274
    }
275
276
    public function quote($input, $type = null) : string
277
    {
278
        return $this->forwardCall();
279
    }
280
281
    public function fetchAll($sql, array $params = [], $types = []) : array
282
    {
283
        return $this->forwardCall();
284
    }
285
286
    public function prepare(string $statement) : Statement
287
    {
288
        return $this->forwardCall();
289
    }
290
291
    public function executeQuery(string $query, array $params = [], $types = [], ?QueryCacheProfile $qcp = null) : ResultStatement
292
    {
293
        return $this->forwardCall();
294
    }
295
296
    public function executeCacheQuery($query, $params, $types, QueryCacheProfile $qcp) : ResultStatement
297
    {
298
        return $this->forwardCall();
299
    }
300
301
    public function project($query, array $params, Closure $function) : array
302
    {
303
        return $this->forwardCall();
304
    }
305
306
    public function query(string $sql) : ResultStatement
307
    {
308
        return $this->forwardCall();
309
    }
310
311
    public function executeUpdate(string $query, array $params = [], array $types = []) : int
312
    {
313
        return $this->forwardCall();
314
    }
315
316
    public function exec(string $statement) : int
317
    {
318
        return $this->forwardCall();
319
    }
320
321
    public function getTransactionNestingLevel() : int
322
    {
323
        return $this->forwardCall();
324
    }
325
326
    public function errorCode()
327
    {
328
        return $this->forwardCall();
329
    }
330
331
    public function errorInfo()
332
    {
333
        return $this->forwardCall();
334
    }
335
336
    public function lastInsertId($seqName = null) : string
337
    {
338
        return $this->identifier;
339
    }
340
341
    public function transactional(Closure $func)
342
    {
343
        return $this->forwardCall();
344
    }
345
346
    public function setNestTransactionsWithSavepoints($nestTransactionsWithSavepoints) : void
347
    {
348
        $this->forwardCall();
349
    }
350
351
    public function getNestTransactionsWithSavepoints() : bool
352
    {
353
        return $this->forwardCall();
354
    }
355
356
    protected function getNestedTransactionSavePointName()
357
    {
358
        return $this->forwardCall();
359
    }
360
361
    public function beginTransaction() : void
362
    {
363
        $this->forwardCall();
364
    }
365
366
    public function commit() : void
367
    {
368
        $this->forwardCall();
369
    }
370
371
    public function rollBack() : void
372
    {
373
        $this->forwardCall();
374
    }
375
376
    public function createSavepoint($savepoint) : void
377
    {
378
        $this->forwardCall();
379
    }
380
381
    public function releaseSavepoint($savepoint) : void
382
    {
383
        $this->forwardCall();
384
    }
385
386
    public function rollbackSavepoint($savepoint) : void
387
    {
388
        $this->forwardCall();
389
    }
390
391
    public function getWrappedConnection() : \Doctrine\DBAL\Driver\Connection
392
    {
393
        return $this->forwardCall();
394
    }
395
396
    public function getSchemaManager() : AbstractSchemaManager
397
    {
398
        return $this->forwardCall();
399
    }
400
401
    public function setRollbackOnly() : void
402
    {
403
        $this->forwardCall();
404
    }
405
406
    public function isRollbackOnly() : bool
407
    {
408
        return $this->forwardCall();
409
    }
410
411
    public function convertToDatabaseValue($value, $type)
412
    {
413
        return $this->forwardCall();
414
    }
415
416
    public function convertToPHPValue($value, $type)
417
    {
418
        return $this->forwardCall();
419
    }
420
421
    public function resolveParams(array $params, array $types) : array
422
    {
423
        return $this->forwardCall();
424
    }
425
426
    public function createQueryBuilder() : QueryBuilder
427
    {
428
        return $this->forwardCall();
429
    }
430
431
    public function ping() : void
432
    {
433
        $this->forwardCall();
434
    }
435
}
436