Completed
Pull Request — master (#40)
by Dima
03:24
created

ColumnMetadataExtended::getExtendMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
//----------------------------------------------------------------------------------------------------------------------
3
namespace SetBased\Audit\MySql\Helper;
4
5
//----------------------------------------------------------------------------------------------------------------------
6
use SetBased\Audit\MySql\Metadata\ColumnMetadata;
7
8
/**
9
 * Class for extended column type.
10
 */
11
class ColumnMetadataExtended
12
{
13
  //--------------------------------------------------------------------------------------------------------------------
14
  /**
15
   * The metadata of the column.
16
   *
17
   * @var array[]
18
   */
19
  private $columnTypes = [];
20
21
  //--------------------------------------------------------------------------------------------------------------------
22
  /**
23
   * Object constructor.
24
   *
25
   * @param array[]|ColumnMetadata $columnTypes The metadata of the column.
26
   * @param null|string            $typePrefix  Prefix for column type name.
27
   */
28
  public function __construct($columnTypes, $typePrefix)
29
  {
30
    $this->extendColumnTypes($columnTypes, $typePrefix);
31
  }
32
33
  //--------------------------------------------------------------------------------------------------------------------
34
  /**
35
   * Create array with all columns types.
36
   *
37
   * @param array[]|ColumnMetadata $properties The metadata of the column.
38
   * @param null|string            $typePrefix Prefix for column type name.
39
   */
40
  public function extendColumnTypes($properties, $typePrefix)
41
  {
42
    $columnTypes = $properties->getProperties();
0 ignored issues
show
Bug introduced by
It seems like $properties is not always an object, but can also be of type array<integer,array>. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
43
    foreach ($columnTypes as $typeName => $typeValue)
44
    {
45
      if ($typeName=='column_name')
46
      {
47
        if (!isset($this->columnTypes['column_name']))
48
        {
49
          $this->columnTypes[$typeName] = $typeValue;
50
        }
51
      }
52
      else
53
      {
54
        $format = '%s_%s';
55
        if (isset($typePrefix))
56
        {
57
          $this->columnTypes[sprintf($format, $typePrefix, $typeName)] = $typeValue;
58
        }
59
        else
60
        {
61
          $this->columnTypes[$typeName] = $typeValue;
62
        }
63
      }
64
    }
65
  }
66
67
  //--------------------------------------------------------------------------------------------------------------------
68
  /**
69
   * Get columns type.
70
   *
71
   * @return \array[]
0 ignored issues
show
Documentation introduced by
Should the return type not be array[]?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
72
   */
73
  public function getExtendMetadata()
74
  {
75
    return $this->columnTypes;
76
  }
77
78
  //--------------------------------------------------------------------------------------------------------------------
79
}
80
81
//----------------------------------------------------------------------------------------------------------------------
82