TableMetadata   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 235
Duplicated Lines 10.21 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 24
loc 235
rs 10
wmc 21
lcom 1
cbo 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
B fromClassName() 0 28 4
A getTableColumnType() 12 12 2
A getColumnDefaultValue() 12 12 2
A getTableColumnName() 0 16 3
A getTableColumnAlias() 0 17 3
A getTablePrimaryField() 0 4 1
A getTableColumnIndex() 0 4 1
A isColumnExists() 0 4 2
A isColumnAliasExists() 0 5 1
A isColumnNameExists() 0 4 1
A isColumnNullable() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php declare(strict_types=1);
2
namespace samsonframework\orm;
3
4
/**
5
 * Base table value class.
6
 *
7
 * @author Vitaly Iegorov <[email protected]>
8
 */
9
class TableMetadata
10
{
11
    /** @var string Database table name */
12
    public $tableName;
13
14
    /** @var string Entity class name */
15
    public $className;
16
17
    /** @var array Collection of database table columns */
18
    public $columns = [];
19
20
    /** @var array Collection of database table columns types */
21
    public $columnTypes = [];
22
23
    /** @var array Collection of database table columns aliases to real column names */
24
    public $columnAliases = [];
25
26
    /** @var array Collection of lower case table column aliases to real column names */
27
    public $lowerColumnAliases = [];
28
29
    /** @var string Database table primary field */
30
    public $primaryField;
31
32
    /** @var array Collection of database UNIQUE table columns */
33
    public $uniqueColumns = [];
34
35
    /** @var array Collection of database INDEXED table columns */
36
    public $indexColumns = [];
37
38
    /** @var array Collection of database table columns default values */
39
    public $columnDefaults = [];
40
41
    /** @var array Collection of database table columns is nullable status */
42
    public $columnNullable = [];
43
44
    /**
45
     * Create metadata instance from entity class name
46
     * @param string $className Entity class name
47
     * @deprecated  This is temporary old approach
48
     * @return TableMetadata Metadata instance
49
     *
50
     * @throws \InvalidArgumentException If entity class not found
51
     */
52
    public static function fromClassName(string $className) : TableMetadata
53
    {
54
        $queryClassName = $className . 'Query';
55
        if (class_exists($queryClassName)) {
56
            $metadata = new TableMetadata();
57
            $metadata->primaryField = $queryClassName::$primaryFieldName;
58
            $metadata->className = $queryClassName::$identifier;
59
            $metadata->columnAliases = $queryClassName::$fieldNames;
60
            $metadata->columns = array_values($queryClassName::$fieldNames);
61
            $metadata->tableName = $queryClassName::$tableName;
62
            $metadata->columnTypes = $queryClassName::$fieldDataTypes;
63
            $metadata->columnDefaults = $queryClassName::$fieldDefaults;
64
65
            // Fill in nullables
66
            foreach ($queryClassName::$fieldNullable as $columnName => $nullable) {
67
                $metadata->columnNullable[$columnName] = $nullable === 'YES';
68
            }
69
70
            // Store lower case aliases
71
            foreach ($metadata->columnAliases as $alias => $name) {
72
                $metadata->lowerColumnAliases[strtolower($alias)] = $name;
73
            }
74
75
            return $metadata;
76
        }
77
78
        throw new \InvalidArgumentException('Cannot create metadata for entity ' . $className);
79
    }
80
81
    /**
82
     * Get table column type by column name or alias.
83
     *
84
     * @param string $columnNameOrAlias Table column name or alias
85
     *
86
     * @return string Table column type
87
     * @throws \InvalidArgumentException
88
     */
89 View Code Duplication
    public function getTableColumnType(string $columnNameOrAlias) : string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
    {
91
        $columnName = $this->getTableColumnName($columnNameOrAlias);
92
93
        if (array_key_exists($columnName, $this->columnTypes)) {
94
            return $this->columnTypes[$columnName];
95
        }
96
97
        throw new \InvalidArgumentException(
98
            'Column ' . $columnNameOrAlias . ' type is not defined table ' . $this->tableName
99
        );
100
    }
101
102
    /**
103
     * Get table column default value by column name or alias.
104
     *
105
     * @param string $columnNameOrAlias Table column name or alias
106
     *
107
     * @return mixed Table column default value
108
     * @throws \InvalidArgumentException
109
     */
110 View Code Duplication
    public function getColumnDefaultValue(string $columnNameOrAlias)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
    {
112
        $columnName = $this->getTableColumnName($columnNameOrAlias);
113
114
        if (array_key_exists($columnName, $this->columnDefaults)) {
115
            return $this->columnDefaults[$columnName];
116
        }
117
118
        throw new \InvalidArgumentException(
119
            'Column ' . $columnNameOrAlias . ' type is not defined table ' . $this->tableName
120
        );
121
    }
122
123
    /**
124
     * Get table column name by column name or alias.
125
     *
126
     * @param string $columnNameOrAlias Table column name or alias
127
     *
128
     * @return string Table column name
129
     * @throws \InvalidArgumentException
130
     */
131
    public function getTableColumnName(string $columnNameOrAlias) : string
132
    {
133
        // Case insensitive search
134
        if ($this->isColumnAliasExists($columnNameOrAlias)) {
135
            return $this->lowerColumnAliases[strtolower($columnNameOrAlias)];
136
        }
137
138
        // Search real column names
139
        if ($this->isColumnNameExists($columnNameOrAlias)) {
140
            return $columnNameOrAlias;
141
        }
142
143
        throw new \InvalidArgumentException(
144
            'Column ' . $columnNameOrAlias . ' not found in table ' . $this->tableName
145
        );
146
    }
147
148
    /**
149
     * Get table column alias by column name or alias.
150
     *
151
     * @param string $columnNameOrAlias Table column name or alias
152
     *
153
     * @return string Table column alias
154
     * @throws \InvalidArgumentException
155
     */
156
    public function getTableColumnAlias(string $columnNameOrAlias) : string
157
    {
158
        // Case insensitive search
159
        if ($this->isColumnAliasExists($columnNameOrAlias)) {
160
            $columnName = $this->lowerColumnAliases[strtolower($columnNameOrAlias)];
161
            return array_flip($this->columnAliases)[$columnName];
162
        }
163
164
        // Search real column names
165
        if ($this->isColumnNameExists($columnNameOrAlias)) {
166
            return array_flip($this->columnAliases)[$columnNameOrAlias];
167
        }
168
169
        throw new \InvalidArgumentException(
170
            'Column ' . $columnNameOrAlias . ' not found in table ' . $this->tableName
171
        );
172
    }
173
174
    /**
175
     * Get table primary field name.
176
     *
177
     * @return string Table primary field name
178
     * @throws \InvalidArgumentException
179
     */
180
    public function getTablePrimaryField(): string
181
    {
182
        return $this->getTableColumnName($this->primaryField);
183
    }
184
185
    /**
186
     * Get table column index by column name or alias.
187
     *
188
     * @param string $columnNameOrAlias Table column name or alias
189
     *
190
     * @return int Table column index
191
     * @throws \InvalidArgumentException
192
     */
193
    public function getTableColumnIndex(string $columnNameOrAlias) : int
194
    {
195
        return array_search($this->getTableColumnName($columnNameOrAlias), $this->columns, true);
196
    }
197
198
    /**
199
     * Define if passed column name or alias exists.
200
     *
201
     * @param string $columnNameOrAlias Table column name or alias
202
     * @return bool True if passed column name or alias exists
203
     */
204
    public function isColumnExists(string $columnNameOrAlias): bool
205
    {
206
        return $this->isColumnAliasExists($columnNameOrAlias) || $this->isColumnNameExists($columnNameOrAlias);
207
    }
208
209
    /**
210
     * Is column alias exists using case insensitive search.
211
     *
212
     * @param string $columnAlias Column name alias
213
     * @return bool True if column alias exists otherwise false
214
     */
215
    protected function isColumnAliasExists(string $columnAlias): bool
216
    {
217
//        return array_key_exists($columnAlias, $this->columnAliases);
218
        return array_key_exists(strtolower($columnAlias), $this->lowerColumnAliases);
219
    }
220
221
    /**
222
     * Is column name exists.
223
     *
224
     * @param string $columnName Column name
225
     * @return bool True if column name exists otherwise false
226
     */
227
    protected function isColumnNameExists(string $columnName): bool
228
    {
229
        return in_array($columnName, $this->columns, true);
230
    }
231
232
    /**
233
     * Is column nullable.
234
     *
235
     * @param string $columnNameOrAlias Column name or alias
236
     * @return bool True if column is nullable otherwise false
237
     * @throws \InvalidArgumentException
238
     */
239
    public function isColumnNullable(string $columnNameOrAlias): bool
240
    {
241
        return $this->columnNullable[$this->getTableColumnName($columnNameOrAlias)];
242
    }
243
}
244