Completed
Pull Request — master (#36)
by Thomas
02:53
created

Pgsql::describe()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 15
cts 15
cp 1
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 16
nc 4
nop 1
crap 3
1
<?php
2
3
namespace ORM\Dbal;
4
5
use ORM\Exception;
6
use ORM\QueryBuilder\QueryBuilder;
7
use PDO;
8
9
/**
10
 * Database abstraction for PostgreSQL databases
11
 *
12
 * @package ORM\Dbal
13
 * @author  Thomas Flori <[email protected]>
14
 */
15
class Pgsql extends Dbal
16
{
17
    protected static $typeMapping = [
18
        'integer' => Type\Integer::class,
19
        'smallint' => Type\Integer::class,
20
        'bigint' => Type\Integer::class,
21
22
        'numeric' => Type\Double::class,
23
        'real' => Type\Double::class,
24
        'double precision' => Type\Double::class,
25
        'money' => Type\Double::class,
26
27
        'character varying' => Type\VarChar::class,
28
        'character' => Type\VarChar::class,
29
30
        'text' => Type\Text::class,
31
32
        'date' => Type\DateTime::class,
33
        'timestamp without time zone' => Type\DateTime::class,
34
        'timestamp with time zone' => Type\DateTime::class,
35
        'time without time zone' => Type\Time::class,
36
        'time with time zone' => Type\Time::class,
37
38
        'json' => Type\Json::class,
39
        'boolean' => Type\Boolean::class,
40
    ];
41
42 2
    public function insert($entity, $useAutoIncrement = true)
43
    {
44 2
        $statement = $this->buildInsertStatement($entity);
45 2
        $pdo = $this->em->getConnection();
46
47 2
        if ($useAutoIncrement && $entity::isAutoIncremented()) {
48 1
            $statement .= ' RETURNING ' . $entity::getColumnName($entity::getPrimaryKeyVars()[0]);
49 1
            $result = $pdo->query($statement);
50 1
            return $result->fetchColumn();
1 ignored issue
show
Bug Best Practice introduced by
The return type of return $result->fetchColumn(); (string) is incompatible with the return type of the parent method ORM\Dbal\Dbal::insert of type boolean.

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...
51
        }
52
53 1
        $pdo->query($statement);
54 1
        $this->em->sync($entity, true);
55 1
        return true;
56
    }
57
58 26
    public function describe($schemaTable)
59
    {
60 26
        $table = explode(static::$identifierDivider, $schemaTable);
61 26
        list($schema, $table) = count($table) === 2 ? $table : ['public', $table[0]];
62
63 26
        $query = new QueryBuilder('INFORMATION_SCHEMA.COLUMNS');
64 26
        $query->where('table_name', $table)->andWhere('table_schema', $schema);
65 26
        $query->columns([
66 26
            'column_name', 'column_default', 'data_type', 'is_nullable', 'character_maximum_length',
67
            'datetime_precision'
68
        ]);
69
70 26
        $result = $this->em->getConnection()->query($query->getQuery());
71 26
        $rawColumns = $result->fetchAll(PDO::FETCH_ASSOC);
72 26
        if (count($rawColumns) === 0) {
73 2
            throw new Exception('Unknown table '  . $schemaTable);
74
        }
75
76 24
        $cols = array_map(function ($columnDefinition) {
77 24
            return Column::factory($columnDefinition, $this->getType($columnDefinition));
78 24
        }, $rawColumns);
79
80 24
        return $cols;
81
    }
82
83 24
    protected function getType($columnDefinition)
84
    {
85 24
        if (isset(static::$typeMapping[$columnDefinition['data_type']])) {
86 17
            return call_user_func([static::$typeMapping[$columnDefinition['data_type']], 'factory'], $columnDefinition);
87
        }
88
89 7
        return parent::getType($columnDefinition);
90
    }
91
}
92