1 | <?php |
||
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) { |
|
71 | |||
72 | 28 | private function getRelationshipDetails($relationship) { |
|
97 | |||
98 | 28 | private function createRelationships($type, $relationships) { |
|
108 | |||
109 | /** |
||
110 | * |
||
111 | * @return array<Relationship> |
||
112 | */ |
||
113 | 18 | public function getRelationships() { |
|
116 | |||
117 | 24 | public function getPrimaryKey() { |
|
120 | |||
121 | 10 | public function getAutoPrimaryKey() { |
|
124 | |||
125 | 10 | public function getFields() { |
|
128 | |||
129 | 10 | public function getUniqueKeys() { |
|
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.