Completed
Push — master ( 8bf117...951fd4 )
by SignpostMarv
03:41
created

TestObjectRepository::__construct()   C

Complexity

Conditions 12
Paths 40

Size

Total Lines 76
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 39
CRAP Score 12.2111

Importance

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