1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ntentan\nibii; |
4
|
|
|
|
5
|
|
|
class ModelDescription { |
6
|
|
|
|
7
|
|
|
private $fields = []; |
8
|
|
|
private $primaryKey = []; |
9
|
|
|
private $uniqueKeys = []; |
10
|
|
|
private $autoPrimaryKey = false; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* |
14
|
|
|
* @var array<Relationship> |
15
|
|
|
*/ |
16
|
|
|
private $relationships = []; |
17
|
|
|
private $table; |
18
|
|
|
private $schema; |
19
|
|
|
private $name; |
20
|
|
|
private $container; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* |
24
|
|
|
* @param RecordWrapper $model |
25
|
|
|
*/ |
26
|
28 |
|
public function __construct(ORMContext $context, $model) { |
27
|
28 |
|
$this->table = $model->getDBStoreInformation()['table']; |
28
|
28 |
|
$this->schema = $model->getDBStoreInformation()['schema']; |
29
|
28 |
|
$this->name = $context->getModelName((new \ReflectionClass($model))->getName()); |
30
|
28 |
|
$this->container = $context->getContainer(); |
31
|
28 |
|
$relationships = $model->getRelationships(); |
32
|
28 |
|
$adapter = $context->getContainer()->resolve(DriverAdapter::class); |
33
|
28 |
|
$schema = array_values($context->getDbContext()->getDriver()->describeTable($this->table))[0]; |
34
|
28 |
|
$this->autoPrimaryKey = $schema['auto_increment']; |
35
|
|
|
|
36
|
28 |
|
foreach ($schema['columns'] as $field => $details) { |
37
|
28 |
|
$this->fields[$field] = [ |
38
|
28 |
|
'type' => $adapter->mapDataTypes($details['type']), |
39
|
28 |
|
'required' => !$details['nulls'], |
40
|
28 |
|
'default' => $details['default'], |
41
|
28 |
|
'name' => $field |
42
|
|
|
]; |
43
|
28 |
|
if (isset($details['default'])) { |
44
|
20 |
|
$this->fields[$field]['default'] = $details['default']; |
45
|
|
|
} |
46
|
28 |
|
if (isset($details['length'])) { |
47
|
28 |
|
$this->fields[$field]['length'] = $details['length']; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
28 |
|
$this->appendConstraints($schema['primary_key'], $this->primaryKey, true); |
52
|
28 |
|
$this->appendConstraints($schema['unique_keys'], $this->uniqueKeys); |
53
|
|
|
|
54
|
28 |
|
foreach ($relationships as $type => $relations) { |
55
|
28 |
|
$this->createRelationships($type, $relations); |
56
|
|
|
} |
57
|
28 |
|
} |
58
|
|
|
|
59
|
28 |
|
private function appendConstraints($constraints, &$key, $flat = false) { |
60
|
28 |
|
foreach ($constraints as $constraint) { |
61
|
28 |
|
if ($flat) { |
62
|
28 |
|
$key = $constraint['columns']; |
63
|
28 |
|
break; |
64
|
|
|
} else { |
65
|
16 |
|
$key[] = array( |
66
|
16 |
|
'fields' => $constraint['columns'] |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
} |
70
|
28 |
|
} |
71
|
|
|
|
72
|
28 |
|
private function getRelationshipDetails($relationship) { |
73
|
28 |
|
$relationshipDetails = []; |
|
|
|
|
74
|
28 |
|
if (is_string($relationship)) { |
75
|
|
|
$relationshipDetails = [ |
76
|
22 |
|
'model' => $relationship, |
77
|
22 |
|
'name' => $relationship, |
78
|
22 |
|
'foreign_key' => '', |
79
|
22 |
|
'local_key' => '' |
80
|
|
|
]; |
81
|
6 |
|
} else if (is_array($relationship)) { |
82
|
|
|
$relationshipDetails = [ |
83
|
6 |
|
'model' => $relationship[0], |
84
|
6 |
|
'name' => isset($relationship['as']) ? $relationship['as'] : $relationship[0], |
85
|
6 |
|
'foreign_key' => isset($relationship['foreign_key']) ? $relationship['foreign_key'] : '', |
86
|
6 |
|
'local_key' => isset($relationship['local_key']) ? $relationship['local_key'] : '', |
87
|
|
|
]; |
88
|
|
|
} else { |
89
|
|
|
return null; |
90
|
|
|
} |
91
|
28 |
|
$relationshipDetails['local_table'] = $this->table; |
92
|
28 |
|
if(isset($relationship['through'])) { |
93
|
|
|
$relationshipDetails['through'] = $relationship['through']; |
94
|
|
|
} |
95
|
28 |
|
return $relationshipDetails; |
96
|
|
|
} |
97
|
|
|
|
98
|
28 |
|
private function createRelationships($type, $relationships) { |
99
|
28 |
|
foreach ($relationships as $relationship) { |
100
|
28 |
|
$relationship = $this->getRelationshipDetails($relationship); |
101
|
28 |
|
$class = "\\ntentan\\nibii\\relationships\\{$type}Relationship"; |
102
|
28 |
|
$relationshipObject = $this->container->resolve($class); |
103
|
28 |
|
$relationshipObject->setOptions($relationship); |
104
|
28 |
|
$relationshipObject->setup($this->name, $this->schema, $this->table, $this->primaryKey); |
105
|
28 |
|
$this->relationships[$relationship['name']] = $relationshipObject; |
106
|
|
|
} |
107
|
28 |
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* |
111
|
|
|
* @return array<Relationship> |
112
|
|
|
*/ |
113
|
18 |
|
public function getRelationships() { |
114
|
18 |
|
return $this->relationships; |
115
|
|
|
} |
116
|
|
|
|
117
|
24 |
|
public function getPrimaryKey() { |
118
|
24 |
|
return $this->primaryKey; |
119
|
|
|
} |
120
|
|
|
|
121
|
10 |
|
public function getAutoPrimaryKey() { |
122
|
10 |
|
return $this->autoPrimaryKey; |
123
|
|
|
} |
124
|
|
|
|
125
|
10 |
|
public function getFields() { |
126
|
10 |
|
return $this->fields; |
127
|
|
|
} |
128
|
|
|
|
129
|
10 |
|
public function getUniqueKeys() { |
130
|
10 |
|
return $this->uniqueKeys; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.