Passed
Branch feature-dbal (8a3860)
by Thomas
08:32
created

Sqlite::describe()   C

Complexity

Conditions 15
Paths 120

Size

Total Lines 53
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 15

Importance

Changes 0
Metric Value
dl 0
loc 53
ccs 30
cts 30
cp 1
rs 5.7335
c 0
b 0
f 0
cc 15
eloc 31
nc 120
nop 1
crap 15

How to fix   Long Method    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 = strtolower($rawColumn['type']);
66
67
            // remove size for mapping
68 30
            if (($p = strpos($type, '(')) !== false && $p > 0) {
69 4
                $type = substr($type, 0, $p);
70
            }
71
72 30
            $class  = isset(static::$typeMapping[$type]) ? static::$typeMapping[$type] : Dbal\Type\Text::class;
73
74 30
            $hasDefault = $rawColumn['dflt_value'] !== null;
75
76 30
            if (!$hasDefault && $rawColumn['type'] === 'integer' && $rawColumn['pk'] === '1') {
77 2
                $multiplePrimaryKey = false;
78 2
                if ($i < count($rawColumns) - 1) {
79 1
                    for ($j = $i+1; $j < count($rawColumns); $j++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
80 1
                        if ($rawColumns[$j]['pk'] !== '0') {
81 1
                            $multiplePrimaryKey = true;
82 1
                            break;
83
                        }
84
                    }
85
                }
86 2
                if (!$multiplePrimaryKey) {
87 1
                    $hasDefault = true;
88
                }
89
            }
90
91 30
            $cols[]     = new Column(
92 30
                $rawColumn['name'],
93 30
                new $class,
94
                $hasDefault,
95 30
                $rawColumn['notnull'] === '0'
96
            );
97
        }
98
99 30
        return $cols;
100
    }
101
}
102