Completed
Pull Request — master (#2)
by Joao
03:57
created

MySqlDatabase   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 41.03 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 4
dl 16
loc 39
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A prepareEnvironment() 9 9 1
A createDatabase() 7 7 1
A dropDatabase() 0 6 1
A createVersion() 0 5 1
A executeSql() 0 4 1

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
2
3
namespace ByJG\DbMigration\Database;
4
5
use ByJG\AnyDataset\Factory;
6
use ByJG\Util\Uri;
7
8
class MySqlDatabase extends AbstractDatabase
9
{
10
11 View Code Duplication
    public static function prepareEnvironment(Uri $uri)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
12
    {
13
        $database = preg_replace('~^/~', '', $uri->getPath());
14
15
        $customUri = new Uri($uri->__toString());
16
17
        $dbDriver = Factory::getDbRelationalInstance($customUri->withPath('/')->__toString());
18
        $dbDriver->execute("CREATE SCHEMA IF NOT EXISTS `$database` DEFAULT CHARACTER SET utf8 ;");
19
    }
20
21 View Code Duplication
    public function createDatabase()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
22
    {
23
        $database = preg_replace('~^/~', '', $this->getDbDriver()->getUri()->getPath());
24
25
        $this->getDbDriver()->execute("CREATE SCHEMA IF NOT EXISTS `$database` DEFAULT CHARACTER SET utf8 ;");
26
        $this->getDbDriver()->execute("USE `$database`");
27
    }
28
29
    public function dropDatabase()
30
    {
31
        $database = preg_replace('~^/~', '', $this->getDbDriver()->getUri()->getPath());
32
33
        $this->getDbDriver()->execute("drop database `$database`");
34
    }
35
36
    public function createVersion()
37
    {
38
        $this->getDbDriver()->execute('CREATE TABLE IF NOT EXISTS migration_version (version int, status varchar(20))');
39
        $this->checkExistsVersion();
40
    }
41
42
    public function executeSql($sql)
43
    {
44
        $this->getDbDriver()->execute($sql);
45
    }
46
}
47