Passed
Branch feature-dbal (b0d62a)
by Thomas
03:17
created

Sqlite::describe()   D

Complexity

Conditions 10
Paths 24

Size

Total Lines 38
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 10

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 38
ccs 22
cts 22
cp 1
rs 4.8196
c 1
b 0
f 0
cc 10
eloc 23
nc 24
nop 1
crap 10

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 ORM\Dbal;
4
5
use ORM\Dbal;
6
use ORM\Exception;
7
8
class Sqlite extends Dbal
9
{
10
    protected static $typeMapping = [
11
        'integer' => Type\Integer::class,
12
        'int' => Type\Integer::class,
13
14
        'double' => Type\Double::class,
15
        'real' => Type\Double::class,
16
        'float' => Type\Double::class,
17
        'numeric' => Type\Double::class,
18
        'decimal' => Type\Double::class,
19
20
        'varchar' => Type\VarChar::class,
21
        'character' => Type\VarChar::class,
22
23
        'text' => Type\Text::class,
24
25
        'boolean' => Type\Boolean::class,
26
        'json' => Type\Json::class,
27
28
        'datetime' => Type\DateTime::class,
29
        'date' => Type\DateTime::class,
30
        'time' => Type\Time::class,
31
    ];
32
33 2
    public function insert($entity, $useAutoIncrement = true)
34
    {
35 2
        $statement = $this->buildInsertStatement($entity);
36 2
        $pdo = $this->em->getConnection();
37
38 2
        if ($useAutoIncrement && $entity::isAutoIncremented()) {
39 1
            $pdo->query($statement);
40 1
            return $pdo->lastInsertId();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $pdo->lastInsertId(); (string) is incompatible with the return type of the parent method ORM\Dbal::insert of type boolean|integer.

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...
41
        }
42
43 1
        $pdo->query($statement);
44 1
        $this->em->sync($entity, true);
45 1
        return true;
46
    }
47
48 32
    public function describe($schemaTable)
49
    {
50 32
        $table = explode(static::$identifierDivider, $schemaTable);
51 32
        list($schema, $table) = count($table) === 2 ? $table : [null, $table[0]];
52 32
        $schema = $schema !== null ? $this->escapeIdentifier($schema) . '.' : '';
53
54 32
        $result = $this->em->getConnection()->query(
55 32
            'PRAGMA ' . $schema . 'table_info(' . $this->escapeIdentifier($table) . ')'
56
        );
57 32
        $rawColumns = $result->fetchAll(\PDO::FETCH_ASSOC);
58
59 32
        if (count($rawColumns) === 0) {
60 2
            throw new Exception('Unknown table '  . $table);
61
        }
62
63 30
        $cols = [];
64 30
        foreach ($rawColumns as $i => $rawColumn) {
65 30
            $type = $this->normlizeType($rawColumn['type']);
66 30
            $class = isset(static::$typeMapping[$type]) ? static::$typeMapping[$type] : Dbal\Type\Text::class;
67
68 30
            $hasDefault = $rawColumn['dflt_value'] !== null;
69
70 30
            if (!$hasDefault && $rawColumn['type'] === 'integer' && $rawColumn['pk'] === '1' &&
71 30
                !$this->hasMultiplePrimaryKey($rawColumns)
72
            ) {
73 1
                $hasDefault = true;
74
            }
75
76 30
            $cols[]     = new Column(
77 30
                $rawColumn['name'],
78 30
                new $class,
79
                $hasDefault,
80 30
                $rawColumn['notnull'] === '0'
81
            );
82
        }
83
84 30
        return $cols;
85
    }
86
87
    protected function hasMultiplePrimaryKey($rawColumns)
88
    {
89 2
        return count(array_filter(array_map(function ($rawColumn) {
90 2
            return $rawColumn['pk'];
91 2
        }, $rawColumns))) > 1;
92
    }
93
}
94