Database   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 92
Duplicated Lines 29.35 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 27
loc 92
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A replaceFullTableName() 20 20 3
A query() 7 17 4
A insertFrom() 0 23 5
A setDebug() 0 12 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace EvolutionCMS;
2
3
use AgelxNash\Modx\Evo\Database\Database as BaseDatabase;
4
use AgelxNash\Modx\Evo\Database\Drivers\IlluminateDriver;
5
use Exception;
6
use AgelxNash\Modx\Evo\Database\Exceptions;
7
8
class Database extends BaseDatabase implements Interfaces\DatabaseInterface
9
{
10
    public $config;
11
12
    /**
13
     * @param $tableName
14
     * @param bool $force
15
     * @return null|string|string[]
16
     * @throws Exceptions\TableNotDefinedException
17
     */
18 View Code Duplication
    public function replaceFullTableName($tableName, $force = false)
19
    {
20
        $tableName = trim($tableName);
21
        if ((bool)$force === true) {
22
            $result = $this->getFullTableName($tableName);
23
        } elseif (strpos($tableName, '[+prefix+]') !== false) {
24
            $dbase = trim($this->getConfig('database'), '`');
25
            $prefix = $this->getConfig('prefix');
26
27
            $result = preg_replace(
28
                '@\[\+prefix\+\](\w+)@',
29
                '`' . $dbase . '`.`' . $prefix . '$1`',
30
                $tableName
31
            );
32
        } else {
33
            $result = $tableName;
34
        }
35
36
        return $result;
37
    }
38
39
    /**
40
     * {@inheritDoc}
41
     */
42
    public function query($sql)
43
    {
44
        try {
45
            $out = [];
46 View Code Duplication
            if (\is_array($sql)) {
47
                foreach ($sql as $query) {
48
                    $out[] = parent::query($this->replaceFullTableName($query));
0 ignored issues
show
Bug introduced by
It seems like $this->replaceFullTableName($query) targeting EvolutionCMS\Database::replaceFullTableName() can also be of type null; however, AgelxNash\Modx\Evo\Datab...stractDatabase::query() does only seem to accept string|array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
49
                }
50
            } else {
51
                $out = parent::query($this->replaceFullTableName($sql));
0 ignored issues
show
Bug introduced by
It seems like $this->replaceFullTableName($sql) targeting EvolutionCMS\Database::replaceFullTableName() can also be of type null; however, AgelxNash\Modx\Evo\Datab...stractDatabase::query() does only seem to accept string|array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
52
            }
53
54
            return $out;
55
        } catch (Exception $exception) {
56
            evolutionCMS()->getService('ExceptionHandler')->messageQuit($exception->getMessage());
57
        }
58
    }
59
60
    public function insertFrom(
61
        $fields,
62
        $table,
63
        $fromFields = '*',
64
        $fromTable = '',
65
        $where = '',
66
        $limit = ''
67
    ) {
68
        if (is_array($fields)) {
69
            $onlyKeys = true;
70
            foreach ($fields as $key => $value) {
71
                if (!empty($value)) {
72
                    $onlyKeys = false;
73
                    break;
74
                }
75
            }
76
            if ($onlyKeys) {
77
                $fields = array_keys($fields);
78
            }
79
        }
80
81
        return parent::insertFrom($fields, $table, $fromFields, $fromTable, $where, $limit);
82
    }
83
84
    /**
85
     * {@inheritDoc}
86
     */
87
    public function setDebug($flag)
88
    {
89
        parent::setDebug($flag);
90
        $driver = $this->getDriver();
91
        if ($driver instanceof IlluminateDriver) {
92
            if ($this->isDebug()) {
93
                $driver->getConnect()->enableQueryLog();
94
            } else {
95
                $driver->getConnect()->disableQueryLog();
96
            }
97
        }
98
    }
99
}
100