Completed
Push — master ( 505b3b...86fb92 )
by SignpostMarv
01:46
created

TestObjectRepository::__construct()   C

Complexity

Conditions 12
Paths 40

Size

Total Lines 70
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 38
CRAP Score 12.2265

Importance

Changes 0
Metric Value
cc 12
eloc 49
nc 40
nop 2
dl 0
loc 70
ccs 38
cts 43
cp 0.8837
crap 12.2265
rs 5.6441
c 0
b 0
f 0

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
* Base daft objects.
4
*
5
* @author SignpostMarv
6
*/
7
declare(strict_types=1);
8
9
namespace SignpostMarv\DaftObject\EasyDB;
10
11
use ParagonIE\EasyDB\EasyDB;
12
use ReflectionClass;
13
use ReflectionType;
14
use RuntimeException;
15
use SignpostMarv\DaftObject\AbstractDaftObjectEasyDBRepository;
16
17
class TestObjectRepository extends AbstractDaftObjectEasyDBRepository
18
{
19 6
    protected function __construct(string $type, EasyDB $db)
20
    {
21 6
        parent::__construct($type, $db);
22
        $query =
23
            'CREATE TABLE ' .
24 6
            $db->escapeIdentifier($this->DaftObjectDatabaseTable()) .
25 6
            ' (';
26
27 6
        $queryParts = [];
28
29 6
        $ref = new ReflectionClass($type);
30 6
        $nullables = $type::DaftObjectNullableProperties();
31
32 6
        foreach ($type::DaftObjectProperties() as $i => $prop) {
33 6
            $methodName = 'Get' . ucfirst($prop);
34 6
            if ($ref->hasMethod($methodName) === true) {
35 6
                $refReturn = $ref->getMethod($methodName)->getReturnType();
36
37
                if (
38 6
                    ($refReturn instanceof ReflectionType) &&
39 6
                    $refReturn->isBuiltin()
40
                ) {
41 6
                    $queryPart = $db->escapeIdentifier($prop);
42 6
                    switch ($refReturn->__toString()) {
43 6
                        case 'string':
44 6
                            $queryPart .= ' VARCHAR(255)';
45 6
                        break;
46 6
                        case 'float':
47 6
                            $queryPart .= ' REAL';
48 6
                        break;
49 6
                        case 'int':
50 6
                        case 'bool':
51 6
                            $queryPart .= ' INTEGER';
52 6
                        break;
53
                        default:
54
                            throw new RuntimeException(
55
                                sprintf(
56
                                    'Unsupported data type! (%s)',
57
                                    $refReturn->__toString()
58
                                )
59
                            );
60
                    }
61 6
                    if (in_array($prop, $nullables, true) === false) {
62 6
                        $queryPart .= ' NOT NULL';
63
                    }
64
65 6
                    $queryParts[] = $queryPart;
66
                } else {
67
                    throw new RuntimeException('Only supports builtins');
68
                }
69
            }
70
        }
71
72 6
        $primaryKeyCols = [];
73 6
        foreach ($type::DaftObjectIdProperties() as $col) {
74 6
            $primaryKeyCols[] = $db->escapeIdentifier($col);
75
        }
76
77 6
        if (count($primaryKeyCols) > 0) {
78 6
            $queryParts[] =
79
                'PRIMARY KEY (' .
80 6
                implode(',', $primaryKeyCols) .
81 6
                ')';
82
        }
83
84
        $query .=
85 6
            implode(',', $queryParts) .
86 6
            ');';
87
88 6
        $db->safeQuery($query);
89 6
    }
90
91 6
    protected function DaftObjectDatabaseTable() : string
92
    {
93 6
        return preg_replace('/[^a-z]+/', '_', mb_strtolower($this->type));
94
    }
95
}
96