Completed
Pull Request — master (#50)
by Dima
03:19
created

AuditDiffTable::removeMatchingColumns()   C

Complexity

Conditions 13
Paths 7

Size

Total Lines 48
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 48
ccs 0
cts 36
cp 0
rs 5.0877
cc 13
eloc 26
nc 7
nop 0
crap 182

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
//----------------------------------------------------------------------------------------------------------------------
3
namespace SetBased\Audit\MySql;
4
5
use SetBased\Audit\MySql\Helper\DiffTableColumns;
6
use SetBased\Audit\MySql\Metadata\MultiSourceColumnMetadata;
7
use SetBased\Audit\MySql\Metadata\TableColumnsMetadata;
8
use SetBased\Stratum\MySql\StaticDataLayer;
9
10
//----------------------------------------------------------------------------------------------------------------------
11
/**
12
 * Class for executing auditing actions for tables.
13
 */
14
class AuditDiffTable
15
{
16
  //--------------------------------------------------------------------------------------------------------------------
17
  /**
18
   * Audit database schema.
19
   *
20
   * @var string
21
   */
22
  private $auditSchema;
23
24
  /**
25
   * Audit columns from config file
26
   *
27
   * @var array[]
28
   */
29
  private $configColumns;
30
31
  /**
32
   * Data database schema.
33
   *
34
   * @var string
35
   */
36
  private $dataSchema;
37
38
  /**
39
   * Difference between data and audit tables.
40
   *
41
   * @var DiffTableColumns
42
   */
43
  private $diffColumns;
44
45
  /**
46
   * Table name.
47
   *
48
   * @var string
49
   */
50
  private $tableName;
51
52
  //--------------------------------------------------------------------------------------------------------------------
53
  /**
54
   * Object constructor.
55
   *
56
   * @param string  $dataSchema         Data database schema.
57
   * @param string  $auditSchema        Audit database schema.
58
   * @param string  $tableName          Table name.
59
   * @param array[] $configAuditColumns Audit columns from config file.
60
   * @param array[] $configColumns      Data columns from config file.
61
   */
62
  public function __construct($dataSchema, $auditSchema, $tableName, $configAuditColumns, $configColumns)
63
  {
64
    $this->dataSchema    = $dataSchema;
65
    $this->auditSchema   = $auditSchema;
66
    $this->tableName     = $tableName;
67
    $this->configColumns = array_merge($configAuditColumns, $configColumns);
68
69
    $dataColumns  = new TableColumnsMetadata(AuditDataLayer::getTableColumns($this->dataSchema, $this->tableName));
70
    $auditColumns = AuditDataLayer::getTableColumns($this->auditSchema, $this->tableName);
71
    $auditColumns = $this->addNotNull($auditColumns);
72
    $auditColumns = new TableColumnsMetadata($auditColumns);
73
74
    $this->createDiffArray($dataColumns, $auditColumns);
75
  }
76
77
  //--------------------------------------------------------------------------------------------------------------------
78
  /**
79
   * Return diff columns.
80
   *
81
   * @return TableColumnsMetadata
82
   */
83
  public function getDiffColumns()
84
  {
85
    return $this->diffColumns->getColumns();
86
  }
87
88
  //--------------------------------------------------------------------------------------------------------------------
89
  /**
90
   * Check full and return array without new or obsolete columns if full not set.
91
   *
92
   * @return DiffTableColumns
0 ignored issues
show
Documentation introduced by
Should the return type not be TableColumnsMetadata?

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...
93
   */
94
  public function removeMatchingColumns()
95
  {
96
    $metadata = $this->diffColumns->getColumns();
97
    /** @var MultiSourceColumnMetadata $column */
98
    foreach ($metadata->getColumns() as $columnName => $column)
99
    {
100
      $data   = $column->getProperty('data');
101
      $audit  = $column->getProperty('audit');
102
      $config = $column->getProperty('config');
103
104
      if (!isset($data))
105
      {
106
        if (isset($audit) && isset($config))
107
        {
108
          if ($audit->getProperty('column_type')==$config->getProperty('column_type'))
0 ignored issues
show
Bug introduced by
The method getProperty cannot be called on $audit (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
Bug introduced by
The method getProperty cannot be called on $config (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
109
          {
110
            $metadata->removeColumn($columnName);
111
          }
112
        }
113
      }
114
      else
115
      {
116
        if (isset($audit) && isset($config))
117
        {
118
          $config_character_set_name = $config->getProperty('character_set_name');
0 ignored issues
show
Bug introduced by
The method getProperty cannot be called on $config (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
119
          $config_collation_name     = $config->getProperty('collation_name');
0 ignored issues
show
Bug introduced by
The method getProperty cannot be called on $config (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
120
          $audit_character_set_name  = $audit->getProperty('character_set_name');
0 ignored issues
show
Bug introduced by
The method getProperty cannot be called on $audit (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
121
          $audit_collation_name      = $audit->getProperty('collation_name');
0 ignored issues
show
Bug introduced by
The method getProperty cannot be called on $audit (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
122
123
          $data_character_set_name = $data->getProperty('character_set_name');
0 ignored issues
show
Bug introduced by
The method getProperty cannot be called on $data (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
124
          $data_collation_name     = $data->getProperty('collation_name');
0 ignored issues
show
Bug introduced by
The method getProperty cannot be called on $data (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
125
126
          if (
127
            $audit->getProperty('column_type')==$data->getProperty('column_type')
0 ignored issues
show
Bug introduced by
The method getProperty cannot be called on $data (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
Bug introduced by
The method getProperty cannot be called on $audit (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
128
            && $audit_character_set_name==$data_character_set_name
129
            && $audit_character_set_name==$config_character_set_name
130
            && $audit_collation_name==$config_collation_name
131
            && $audit_collation_name==$data_collation_name
132
          )
133
          {
134
            $metadata->removeColumn($columnName);
135
          }
136
        }
137
      }
138
    }
139
140
    return $metadata;
141
  }
142
143
  //--------------------------------------------------------------------------------------------------------------------
144
  /**
145
   * Add not null to audit columns if it not nullable.
146
   *
147
   * @param array $theColumns Audit columns.
148
   *
149
   * @return array
150
   */
151
  private function addNotNull($theColumns)
152
  {
153
    $modifiedColumns = [];
154
    foreach ($theColumns as $column)
155
    {
156
      $modifiedColumn = $column;
157
      $auditColumn    = StaticDataLayer::searchInRowSet('column_name', $modifiedColumn['column_name'], $this->configColumns);
158
      if (isset($auditColumn))
159
      {
160
        if ($modifiedColumn['is_nullable']==='NO')
161
        {
162
          $modifiedColumn['column_type'] = sprintf('%s not null', $modifiedColumn['column_type']);
163
        }
164
      }
165
      $modifiedColumns[] = $modifiedColumn;
166
    }
167
168
    return $modifiedColumns;
169
  }
170
171
  //--------------------------------------------------------------------------------------------------------------------
172
  /**
173
   * Get the difference between data and audit tables.
174
   *
175
   * @param TableColumnsMetadata $dataColumns  The table columns from data schema.
176
   * @param TableColumnsMetadata $auditColumns The table columns from audit schema.
177
   */
178
  private function createDiffArray($dataColumns, $auditColumns)
179
  {
180
    $configColumns     = new TableColumnsMetadata($this->configColumns);
0 ignored issues
show
Documentation introduced by
$this->configColumns is of type array<integer,array>, but the function expects a array<integer,object<array>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
181
    $this->diffColumns = new DiffTableColumns($configColumns, $auditColumns, $dataColumns);
182
  }
183
184
  //--------------------------------------------------------------------------------------------------------------------
185
}
186
187
//----------------------------------------------------------------------------------------------------------------------
188