Test Failed
Push — master ( d02081...898276 )
by P.R.
04:01
created

TableColumnsMetadata::getPreviousColumn()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SetBased\Audit\Metadata;
4
5
use SetBased\Audit\MySql\Metadata\AlterColumnMetadata;
6
use SetBased\Audit\MySql\Metadata\AuditColumnMetadata;
7
use SetBased\Audit\MySql\Metadata\ColumnMetadata;
1 ignored issue
show
Bug introduced by
This use statement conflicts with another class in this namespace, SetBased\Audit\Metadata\ColumnMetadata. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
use SetBased\Exception\FallenException;
9
10
/**
11
 * Metadata of a list of table columns.
12
 */
13
class TableColumnsMetadata
14
{
15
  //--------------------------------------------------------------------------------------------------------------------
16
  /**
17
   * The metadata of the columns.
18
   *
19
   * @var ColumnMetadata[]
20
   */
21
  private $columns = [];
22
23
  //--------------------------------------------------------------------------------------------------------------------
24
  /**
25
   * Object constructor.
26
   *
27
   * @param array[] $columns The metadata of the columns as returned by AuditDataLayer::getTableColumns().
28
   * @param string  $type    The class for columns metadata.
29
   */
30
  public function __construct($columns = [], $type = 'ColumnMetadata')
31
  {
32
    foreach ($columns as $columnName => $column)
33
    {
34
      $this->columns[$column['column_name']] = static::columnFactory($type, $column);
35
    }
36
  }
37
38
  //--------------------------------------------------------------------------------------------------------------------
39
  /**
40
   * Combines the metadata of two lists of table columns.
41
   *
42
   * @param TableColumnsMetadata $columns1 The first metadata of a list of table columns.
43
   * @param TableColumnsMetadata $columns2 The second metadata of a list of table columns.
44
   *
45
   * @return TableColumnsMetadata
46
   */
47
  public static function combine($columns1, $columns2)
48
  {
49
    $columns = new TableColumnsMetadata();
50
51
    $columns->appendTableColumns($columns1);
52
    $columns->appendTableColumns($columns2);
53
54
    return $columns;
55
  }
56
57
  //--------------------------------------------------------------------------------------------------------------------
58
  /**
59
   * Compares two lists of table columns and returns a list of  table columns the are in both lists but have different
60
   * metadata
61
   *
62
   * @param TableColumnsMetadata $columns1 The first list of table columns.
63
   * @param TableColumnsMetadata $columns2 The second list of table columns.
64
   * @param string[]             $ignore   The properties to be ignored.
65
   *
66
   * @return TableColumnsMetadata
67
   */
68
  public static function differentColumnTypes($columns1, $columns2, $ignore = [])
69
  {
70
    $diff = new TableColumnsMetadata();
71
    foreach ($columns1->columns as $column_name => $column1)
72
    {
73
      if (isset($columns2->columns[$column_name]))
74
      {
75
        if (!ColumnMetadata::compare($column1, $columns2->columns[$column_name], $ignore))
76
        {
77
          $diff->appendTableColumn($column1);
78
        }
79
      }
80
    }
81
82
    return $diff;
83
  }
84
85
  //--------------------------------------------------------------------------------------------------------------------
86
  /**
87
   * Compares two lists of table columns and returns a list of table columns that are in the first list of table columns
88
   * but not in the second list of table columns.
89
   *
90
   * @param TableColumnsMetadata $columns1 The first list of table columns.
91
   * @param TableColumnsMetadata $columns2 The second list of table columns.
92
   *
93
   * @return TableColumnsMetadata
94
   */
95
  public static function notInOtherSet($columns1, $columns2)
96
  {
97
    $diff = new TableColumnsMetadata();
98
    foreach ($columns1->columns as $column_name => $column1)
99
    {
100
      if (!isset($columns2->columns[$column_name]))
101
      {
102
        $diff->appendTableColumn($column1);
103
      }
104
    }
105
106
    return $diff;
107
  }
108
109
  //--------------------------------------------------------------------------------------------------------------------
110
  /**
111
   * A factory for table column metadata.
112
   *
113
   * @param string $type   The type of the metadata.
114
   * @param array  $column The metadata of the column
115
   *
116
   * @return AlterColumnMetadata|AuditColumnMetadata|ColumnMetadata
117
   */
118
  private static function columnFactory($type, $column)
119
  {
120
    switch ($type)
121
    {
122
      case 'ColumnMetadata':
123
        return new ColumnMetadata($column);
124
125
      case 'AlterColumnMetadata':
126
        return new AlterColumnMetadata($column);
127
128
      case 'AuditColumnMetadata':
129
        return new AuditColumnMetadata($column);
130
131
      default:
132
        throw new FallenException('type', $type);
133
    }
134
  }
135
136
  //--------------------------------------------------------------------------------------------------------------------
137
  /**
138
   * Appends a table column to this list of table columns.
139
   *
140
   * @param ColumnMetadata $column The metadata of the table column.
141
   */
142
  public function appendTableColumn($column)
143
  {
144
    $this->columns[$column->getName()] = $column;
145
  }
146
147
  //--------------------------------------------------------------------------------------------------------------------
148
  /**
149
   * Appends table columns to this list of table columns.
150
   *
151
   * @param TableColumnsMetadata $columns The metadata of the table columns.
152
   */
153
  public function appendTableColumns($columns)
154
  {
155
    foreach ($columns->columns as $column)
156
    {
157
      $this->appendTableColumn($column);
158
    }
159
  }
160
161
  //--------------------------------------------------------------------------------------------------------------------
162
  /**
163
   * Returns a column given the column name.
164
   *
165
   * @param string $columnName The name of the column.
166
   *
167
   * @return ColumnMetadata
168
   */
169
  public function getColumn($columnName)
170
  {
171
    return $this->columns[$columnName];
172
  }
173
174
  //--------------------------------------------------------------------------------------------------------------------
175
  /**
176
   * Returns the columns names.
177
   *
178
   * @return string[]
179
   */
180
  public function getColumnNames()
181
  {
182
    return array_keys($this->columns);
183
  }
184
185
  //--------------------------------------------------------------------------------------------------------------------
186
  /**
187
   * Returns the underlying array with metadata of this list of table columns.
188
   *
189
   * @return ColumnMetadata[]
190
   */
191
  public function getColumns()
192
  {
193
    return $this->columns;
194
  }
195
196
  //--------------------------------------------------------------------------------------------------------------------
197
  /**
198
   * Returns the length of the longest column name.
199
   *
200
   * @return int
201
   */
202
  public function getLongestColumnNameLength()
203
  {
204
    $max = 0;
205
    foreach ($this->columns as $column)
206
    {
207
      $max = max($max, mb_strlen($column->getName()));
208
    }
209
210
    return $max;
211
  }
212
213
  //--------------------------------------------------------------------------------------------------------------------
214
  /**
215
   * Returns the number of columns.
216
   *
217
   * @return int
218
   */
219
  public function getNumberOfColumns()
220
  {
221
    return count($this->columns);
222
  }
223
224
  //--------------------------------------------------------------------------------------------------------------------
225
  /**
226
   * Returns previous column of a columns. Returns null if the column name is not found in this TableColumnsMetadata.
227
   *
228
   * @param string $columnName The column name.
229
   *
230
   * @return string|null
231
   */
232
  public function getPreviousColumn($columnName)
233
  {
234
    $columns = array_keys($this->columns);
235
    $key     = array_search($columnName, $columns);
236
237
    if ($key>=1)
238
    {
239
      return $columns[$key - 1];
240
    }
241
242
    return null;
243
  }
244
245
  //--------------------------------------------------------------------------------------------------------------------
246
  /**
247
   * Makes all columns nullable.
248
   */
249
  public function makeNullable()
250
  {
251
    foreach ($this->columns as $column)
252
    {
253
      $column->makeNullable();
254
    }
255
  }
256
257
  //--------------------------------------------------------------------------------------------------------------------
258
  /**
259
   * Prepends table columns to this list of table columns.
260
   *
261
   * @param TableColumnsMetadata $columns The metadata of the table columns.
262
   */
263
  public function prependTableColumns($columns)
264
  {
265
    $this->columns = array_merge($columns->columns, $this->columns);
266
  }
267
268
  //--------------------------------------------------------------------------------------------------------------------
269
  /**
270
   * Removes a table column.
271
   *
272
   * @param string $columnName The table column name.
273
   */
274
  public function removeColumn($columnName)
275
  {
276
    unset($this->columns[$columnName]);
277
  }
278
279
  //--------------------------------------------------------------------------------------------------------------------
280
  /**
281
   * Removes the default values from all columns.
282
   */
283
  public function unsetDefaults()
284
  {
285
    foreach ($this->columns as $column)
286
    {
287
      $column->unsetDefault();
288
    }
289
  }
290
291
  //--------------------------------------------------------------------------------------------------------------------
292
}
293
294
//----------------------------------------------------------------------------------------------------------------------
295