Completed
Push — master ( 2b9193...abd76f )
by James Ekow Abaka
03:07
created

ModelDescription::getRelationshipDetails()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 22
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6.0106

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
ccs 14
cts 15
cp 0.9333
cc 6
eloc 18
nc 10
nop 1
crap 6.0106
1
<?php
2
3
namespace ntentan\nibii;
4
5
use ntentan\atiaa\Db;
6
7
class ModelDescription {
8
9
    private $fields = [];
10
    private $primaryKey = [];
11
    private $uniqueKeys = [];
12
    private $autoPrimaryKey = false;
13
14
    /**
15
     *
16
     * @var array<Relationship>
17
     */
18
    private $relationships = [];
19
    private $table;
20
    private $name;
21
22
    /**
23
     * 
24
     * @param RecordWrapper $model
25
     */
26 28
    public function __construct($model) {
27 28
        $this->table = $model->getDBStoreInformation()['unquoted_table'];
28 28
        $this->name = Nibii::getModelName((new \ReflectionClass($model))->getName());
29 28
        $relationships = $model->getRelationships();
30 28
        $adapter = DriverAdapter::getDefaultInstance();
31 28
        $schema = array_values(Db::getDriver()->describeTable($this->table))[0];
0 ignored issues
show
Documentation introduced by
$this->table is of type string, but the function expects a object<ntentan\atiaa\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
32 28
        $this->autoPrimaryKey = $schema['auto_increment'];
33
34 28
        foreach ($schema['columns'] as $field => $details) {
35 28
            $this->fields[$field] = [
36 28
                'type' => $adapter->mapDataTypes($details['type']),
37 28
                'required' => !$details['nulls'],
38 28
                'default' => $details['default'],
39 28
                'name' => $field
40
            ];
41 28
            if (isset($details['default'])) {
42 20
                $this->fields[$field]['default'] = $details['default'];
43
            }
44 28
            if (isset($details['length'])) {
45 28
                $this->fields[$field]['length'] = $details['length'];
46
            }
47
        }
48
49 28
        $this->appendConstraints($schema['primary_key'], $this->primaryKey, true);
50 28
        $this->appendConstraints($schema['unique_keys'], $this->uniqueKeys);
51
52 28
        foreach ($relationships as $type => $relations) {
53 28
            $this->createRelationships($type, $relations);
54
        }
55 28
    }
56
57 28
    private function appendConstraints($constraints, &$key, $flat = false) {
58 28
        foreach ($constraints as $constraint) {
59 28
            if ($flat) {
60 28
                $key = $constraint['columns'];
61 28
                break;
62
            } else {
63 16
                $key[] = array(
64 16
                    'fields' => $constraint['columns']
65
                );
66
            }
67
        }
68 28
    }
69
70 28
    private function getRelationshipDetails($relationship) {
71 28
        $relationshipDetails = [];
0 ignored issues
show
Unused Code introduced by
$relationshipDetails is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
72 28
        if (is_string($relationship)) {
73
            $relationshipDetails = [
74 22
                'model' => $relationship,
75 22
                'name' => $relationship,
76 22
                'foreign_key' => '',
77 22
                'local_key' => ''
78
            ];
79 6
        } else if (is_array($relationship)) {
80
            $relationshipDetails = [
81 6
                'model' => $relationship[0],
82 6
                'name' => isset($relationship['as']) ? $relationship['as'] : $relationship[0],
83 6
                'foreign_key' => isset($relationship['foreign_key']) ? $relationship['foreign_key'] : '',
84 6
                'local_key' => isset($relationship['local_key']) ? $relationship['local_key'] : ''
85
            ];
86
        } else {
87
            return null;
88
        }
89 28
        $relationshipDetails['local_table'] = $this->table;
90 28
        return $relationshipDetails;
91
    }
92
93 28
    private function createRelationships($type, $relationships) {
94 28
        foreach ($relationships as $relationship) {
95 28
            $relationship = $this->getRelationshipDetails($relationship);
96 28
            $class = "\\ntentan\\nibii\\relationships\\{$type}Relationship";
97 28
            $relationshipObject = new $class();
98 28
            $relationshipObject->setOptions($relationship);
99 28
            $relationshipObject->setup($this->name, $this->table, $this->primaryKey);
100 28
            $this->relationships[$relationship['name']] = $relationshipObject;
101
        }
102 28
    }
103
104
    /**
105
     * 
106
     * @return array<Relationship>
107
     */
108 18
    public function getRelationships() {
109 18
        return $this->relationships;
110
    }
111
112 24
    public function getPrimaryKey() {
113 24
        return $this->primaryKey;
114
    }
115
116 10
    public function getAutoPrimaryKey() {
117 10
        return $this->autoPrimaryKey;
118
    }
119
120 10
    public function getFields() {
121 10
        return $this->fields;
122
    }
123
124 10
    public function getUniqueKeys() {
125 10
        return $this->uniqueKeys;
126
    }
127
128
}
129