Completed
Push — master ( 6adfd5...8dd9a1 )
by Rasmus
02:23
created

Database   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 31.37 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 85.71%

Importance

Changes 13
Bugs 1 Features 3
Metric Value
wmc 5
c 13
b 1
f 3
lcom 0
cbo 1
dl 16
loc 51
ccs 12
cts 14
cp 0.8571
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
createDriver() 0 1 ?
A getSchema() 16 16 3
A sql() 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 mindplay\sql\framework;
4
5
use mindplay\sql\model\DeleteQuery;
6
use mindplay\sql\model\InsertQuery;
7
use mindplay\sql\model\Schema;
8
use mindplay\sql\model\SelectQuery;
9
use mindplay\sql\model\SQLQuery;
10
use mindplay\sql\model\Table;
11
use mindplay\sql\model\UpdateQuery;
12
use UnexpectedValueException;
13
14
/**
15
 * This class implements the primary public API of the database model.
16
 */
17
abstract class Database
18
{
19
    /**
20
     * @var DatabaseContainer
21
     */
22
    protected $container;
23
    
24 1
    public function __construct()
25
    {
26 1
        $this->container = new DatabaseContainer();
27
28 1
        $this->container->set(Driver::class, $this->createDriver());
29 1
    }
30
    
31
    /**
32
     * @return Driver
33
     */
34
    abstract protected function createDriver();
35
36
    /**
37
     * @param string Schema class-name
38
     *
39
     * @return Schema
40
     */
41 1 View Code Duplication
    public function getSchema($schema)
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...
42
    {
43 1
        if (! $this->container->has($schema)) {
44 1
            $this->container->register($schema); // auto-wiring (for Schema with no special constructor dependencies)
45
        }
46
47 1
        $schema = $this->container->get($schema);
48
49 1
        if (! $schema instanceof Schema) {
50
            $class_name = get_class($schema);
51
52
            throw new UnexpectedValueException("{$class_name} does not extend the Schema class");
53
        }
54
55 1
        return $schema;
56
    }
57
    
58
    /**
59
     * @param string $sql
60
     * 
61
     * @return SQLQuery
62
     */
63 1
    public function sql($sql)
64
    {
65 1
        return $this->container->create(SQLQuery::class, ['sql' => $sql]);
66
    }
67
}
68