LegacyDatabase::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 25
ccs 12
cts 12
cp 1
rs 9.9
cc 1
nc 1
nop 9
crap 1

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 namespace AgelxNash\Modx\Evo\Database;
2
3
/**
4
 * @deprecated
5
 */
6
class LegacyDatabase extends AbstractDatabase
7
{
8
    /**
9
     * @param string $host
10
     * @param string $database
11
     * @param string $username
12
     * @param string $password
13
     * @param string $prefix
14
     * @param string $charset
15
     * @param string $method
16
     * @param string $collation
17
     * @param string $driver
18
     * @throws Exceptions\Exception
19
     */
20 6
    public function __construct(
21
        $host = '',
22
        $database = '',
23
        $username = '',
24
        $password = '',
25
        $prefix = '',
26
        $charset = 'utf8mb4',
27
        $method = 'SET CHARACTER SET',
28
        $collation = 'utf8mb4_unicode_ci',
29
        $driver = Drivers\MySqliDriver::class
30
    ) {
31 6
        $database = trim($database, '`');
32
33 6
        $this->setConfig(compact(
34 6
            'host',
35 6
            'database',
36 6
            'username',
37 6
            'password',
38 6
            'prefix',
39 6
            'charset',
40 6
            'method',
41 6
            'collation'
42
        ));
43
44 6
        $this->setDriver($driver);
45 6
    }
46
47
    /**
48
     * @param $tableName
49
     * @param bool $force
50
     * @return null|string|string[]
51
     * @throws Exceptions\TableNotDefinedException
52
     */
53 3
    public function replaceFullTableName($tableName, $force = false)
54
    {
55 3
        $tableName = trim($tableName);
56 3
        if ((bool)$force === true) {
57 1
            $result = $this->getFullTableName($tableName);
58 3
        } elseif (strpos($tableName, '[+prefix+]') !== false) {
59 3
            $dbase = trim($this->getConfig('database'), '`');
60 3
            $prefix = $this->getConfig('prefix');
61
62 3
            $result = preg_replace(
63 3
                '@\[\+prefix\+\](\w+)@',
64 3
                '`' . $dbase . '`.`' . $prefix . '$1`',
65 3
                $tableName
66
            );
67
        } else {
68 3
            $result = $tableName;
69
        }
70
71 3
        return $result;
72
    }
73
74
    /**
75
     * {@inheritDoc}
76
     */
77 2
    public function query($sql)
78
    {
79 2
        $out = [];
80 2
        if (\is_array($sql)) {
81 1
            foreach ($sql as $query) {
82 1
                $out[] = parent::query($this->replaceFullTableName($query));
83
            }
84
        } else {
85 1
            $out = parent::query($this->replaceFullTableName($sql));
86
        }
87
88 2
        return $out;
89
    }
90
}
91