Passed
Push — main ( d7d864...d7e834 )
by Thierry
01:48
created

AdminQueryTrait::dropAndCreate()   B

Complexity

Conditions 10
Paths 5

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 16
c 1
b 0
f 0
nc 5
nop 7
dl 0
loc 24
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Lagdo\DbAdmin\Db\Traits;
4
5
use function substr;
6
use function strlen;
7
use function preg_replace;
8
9
trait AdminQueryTrait
10
{
11
    /**
12
     * Query printed after execution in the message
13
     *
14
     * @param string $query Executed query
15
     *
16
     * @return string
17
     */
18
    private function messageQuery(string $query/*, string $time*/): string
19
    {
20
        if (strlen($query) > 1e6) {
21
            // [\x80-\xFF] - valid UTF-8, \n - can end by one-line comment
22
            $query = preg_replace('~[\x80-\xFF]+$~', '', substr($query, 0, 1e6)) . "\n…";
23
        }
24
        return $query;
25
    }
26
27
    /**
28
     * Execute query
29
     *
30
     * @param string $query
31
     * @param bool $execute
32
     * @param bool $failed
33
     *
34
     * @return bool
35
     * @throws Exception
36
     */
37
    private function executeQuery(string $query, bool $execute = true, bool $failed = false/*, string $time = ''*/): bool
38
    {
39
        if ($execute) {
40
            // $start = microtime(true);
41
            $failed = !$this->driver->execute($query);
42
            // $time = $this->trans->formatTime($start);
43
        }
44
        if ($failed) {
45
            $sql = '';
46
            if ($query) {
47
                $sql = $this->messageQuery($query/*, $time*/);
48
            }
49
            throw new Exception($this->driver->error() . $sql);
0 ignored issues
show
Bug introduced by
The type Lagdo\DbAdmin\Db\Traits\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
50
        }
51
        return true;
52
    }
53
54
    /**
55
     * Execute remembered queries
56
     *
57
     * @param bool $failed
58
     *
59
     * @return bool
60
     * @throws Exception
61
     */
62
    private function executeSavedQuery(bool $failed): bool
63
    {
64
        list($queries/*, $time*/) = $this->driver->queries();
65
        return $this->executeQuery($queries, false, $failed/*, $time*/);
66
    }
67
68
    /**
69
     * Drop old object and create a new one
70
     *
71
     * @param string $drop Drop old object query
72
     * @param string $create Create new object query
73
     * @param string $dropCreated Drop new object query
74
     * @param string $test Create test object query
75
     * @param string $dropTest Drop test object query
76
     * @param string $oldName
77
     * @param string $newName
78
     *
79
     * @return string
80
     * @throws Exception
81
     */
82
    private function dropAndCreate(string $drop, string $create, string $dropCreated,
83
        string $test, string $dropTest, string $oldName, string $newName): string
84
    {
85
        if ($oldName == '' && $newName == '') {
86
            $this->executeQuery($drop);
87
            return 'dropped';
88
        }
89
        if ($oldName == '') {
90
            $this->executeQuery($create);
91
            return 'created';
92
        }
93
        if ($oldName != $newName) {
94
            $created = $this->driver->execute($create);
95
            $dropped = $this->driver->execute($drop);
96
            // $this->executeSavedQuery(!($created && $this->driver->execute($drop)));
97
            if (!$dropped && $created) {
98
                $this->driver->execute($dropCreated);
99
            }
100
            return 'altered';
101
        }
102
        $this->executeSavedQuery(!($this->driver->execute($test) &&
103
            $this->driver->execute($dropTest) &&
104
            $this->driver->execute($drop) && $this->driver->execute($create)));
105
        return 'altered';
106
    }
107
}
108