Completed
Pull Request — master (#28)
by
unknown
11:48
created

Driver   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 82.35%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 3
dl 0
loc 64
ccs 14
cts 17
cp 0.8235
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A connect() 0 4 1
A getDatabasePlatform() 0 4 1
A getSchemaManager() 0 4 1
A getName() 0 4 1
A getDatabase() 0 4 1
A constructPdoDsn() 0 12 4
1
<?php
2
/**
3
 * Licensed to CRATE Technology GmbH("Crate") under one or more contributor
4
 * license agreements.  See the NOTICE file distributed with this work for
5
 * additional information regarding copyright ownership.  Crate licenses
6
 * this file to you under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.  You may
8
 * obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
15
 * License for the specific language governing permissions and limitations
16
 * under the License.
17
 *
18
 * However, if you have executed another commercial license agreement
19
 * with Crate these terms will supersede the license and you may use the
20
 * software solely pursuant to the terms of the relevant commercial agreement.
21
 */
22
namespace Crate\DBAL\Driver\PDOCrate;
23
24
class Driver implements \Doctrine\DBAL\Driver
25
{
26
    const VERSION = '0.2.1';
27
    const NAME = 'crate';
28
29
    /**
30
     * {@inheritDoc}
31
     * @return PDOConnection The database connection.
32
     */
33 6
    public function connect(array $params, $username = null, $password = null, array $driverOptions = array())
34
    {
35 6
        return new PDOConnection($this->constructPdoDsn($params), $username, $password, $driverOptions);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Crate\DBAL\D...sword, $driverOptions); (Crate\DBAL\Driver\PDOCrate\PDOConnection) is incompatible with the return type declared by the interface Doctrine\DBAL\Driver::connect of type Doctrine\DBAL\Driver\Connection.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
36
    }
37
38
    /**
39
     * Constructs the Crate PDO DSN.
40
     *
41
     * @return string The DSN.
42
     */
43 6
    private function constructPdoDsn(array $params)
44
    {
45 6
        $dsn = self::NAME . ':';
46 6
        if (isset($params['host']) && $params['host'] != '') {
47 6
            $dsn .= $params['host'];
48
        } else {
49
            $dsn .= 'localhost';
50
        }
51 6
        $dsn .= ':' . (isset($params['port']) ? $params['port'] : '4200');
52
53 6
        return $dsn;
54
    }
55
56
    /**
57
     * {@inheritDoc}
58
     */
59 7
    public function getDatabasePlatform()
60
    {
61 7
        return new \Crate\DBAL\Platforms\CratePlatform();
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     */
67 2
    public function getSchemaManager(\Doctrine\DBAL\Connection $conn)
68
    {
69 2
        return new \Crate\DBAL\Schema\CrateSchemaManager($conn);
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     */
75
    public function getName()
76
    {
77
        return self::NAME;
78
    }
79
80
    /**
81
     * {@inheritDoc}
82
     */
83 2
    public function getDatabase(\Doctrine\DBAL\Connection $conn)
84
    {
85 2
        return null;
86
    }
87
}
88