MySQLDriverConfig::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
nc 1
nop 8
dl 0
loc 20
ccs 3
cts 3
cp 1
crap 1
rs 9.9666
c 1
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/**
4
 * This file is part of Cycle Database package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Cycle\Database\Config;
13
14
use Cycle\Database\Config\MySQL\ConnectionConfig;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Cycle\Database\Config\ConnectionConfig. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
15
use Cycle\Database\Driver\MySQL\MySQLDriver;
16
17
/**
18
 * @template-extends DriverConfig<ConnectionConfig>
19
 */
20
class MySQLDriverConfig extends DriverConfig
21
{
22
    public function __construct(
23
        ConnectionConfig $connection,
24
        string $driver = MySQLDriver::class,
25
        bool $reconnect = true,
26
        string $timezone = 'UTC',
27 4
        bool $queryCache = true,
28
        bool $readonlySchema = false,
29
        bool $readonly = false,
30
        array $options = [],
31
    ) {
32
        /** @psalm-suppress ArgumentTypeCoercion */
33
        parent::__construct(
34
            connection: $connection,
35
            driver: $driver,
36
            reconnect: $reconnect,
37 4
            timezone: $timezone,
38 4
            queryCache: $queryCache,
39
            readonlySchema: $readonlySchema,
40
            readonly: $readonly,
41
            options: $options,
42
        );
43
    }
44
}
45