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

DiffTableHelper   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 192
Duplicated Lines 4.17 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 26
c 1
b 0
f 0
lcom 1
cbo 3
dl 8
loc 192
ccs 0
cts 92
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
C addHighlighting() 8 65 17
A appendRows() 0 11 2
B appendTableOption() 0 20 5
A getRows() 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
2
//----------------------------------------------------------------------------------------------------------------------
3
namespace SetBased\Audit\MySql\Helper;
4
5
use SetBased\Audit\MySql\AuditDataLayer;
6
use SetBased\Stratum\MySql\StaticDataLayer;
7
use Symfony\Component\Console\Helper\TableSeparator;
8
9
//----------------------------------------------------------------------------------------------------------------------
10
/**
11
 * A helper class for creating printing Tables.
12
 */
13
class DiffTableHelper
14
{
15
  //--------------------------------------------------------------------------------------------------------------------
16
  /**
17
   * Audit columns from config file.
18
   *
19
   * @var array
20
   */
21
  private $auditColumns;
22
23
  /**
24
   * Audit options from audit schema.
25
   *
26
   * @var array
27
   */
28
  private $auditTableOptions;
29
30
  /**
31
   * Audit options from data schema.
32
   *
33
   * @var array
34
   */
35
  private $dataTableOptions;
36
37
  /**
38
   * Check existing separator.
39
   *
40
   * @var bool
41
   */
42
  private $existSeparator = false;
43
44
  /**
45
   * Full option.
46
   *
47
   * @var bool
48
   */
49
  private $fullOption;
50
51
  /**
52
   * Array with rows for table.
53
   *
54
   * @var \array[]
55
   */
56
  private $rows = [];
57
58
  //--------------------------------------------------------------------------------------------------------------------
59
  /**
60
   * Object constructor.
61
   *
62
   * @param string  $dataSchema      Data schema name.
63
   * @param string  $auditSchema     Audit schema name.
64
   * @param string  $tableName       The table name.
65
   * @param array[] $theAuditColumns Audit columns from config file.
66
   * @param bool    $fullOption      If set append table options to rows.
67
   */
68
  public function __construct($dataSchema, $auditSchema, $tableName, $theAuditColumns, $fullOption)
69
  {
70
    $this->auditColumns      = $theAuditColumns;
71
    $this->fullOption        = $fullOption;
72
    $this->auditTableOptions = AuditDataLayer::getTableOptions($auditSchema, $tableName);
73
    $this->dataTableOptions  = AuditDataLayer::getTableOptions($dataSchema, $tableName);
74
  }
75
76
  //--------------------------------------------------------------------------------------------------------------------
77
  /**
78
   * Add highlighting to columns.
79
   */
80
  public function addHighlighting()
81
  {
82
    $styledColumns = [];
83
    foreach ($this->rows as $key => $column)
84
    {
85
      $styledColumn = $column;
86
      if (is_array($column))
87
      {
88
        // Highlighting for data table column types and audit.
89
        if (!empty($column['data_table_type']))
90
        {
91
          if (isset($column['data_table_type']) && !isset($column['audit_table_type']))
92
          {
93 View Code Duplication
            if (!isset($column['column_name']))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
94
            {
95
              $styledColumns[$key - 1]['column_name'] = sprintf('<mm_column>%s</>', $styledColumns[$key - 1]['column_name']);
96
            }
97
            $styledColumn['column_name']     = sprintf('<mm_column>%s</>', $styledColumn['column_name']);
98
            $styledColumn['data_table_type'] = sprintf('<mm_type>%s</>', $styledColumn['data_table_type']);
99
          }
100
          else if (!isset($column['data_table_type']) && isset($column['audit_table_type']))
101
          {
102
            $styledColumn['audit_table_type'] = sprintf('<mm_type>%s</>', $styledColumn['audit_table_type']);
103
          }
104
          else if (strcmp($column['data_table_type'], $column['audit_table_type']))
105
          {
106 View Code Duplication
            if (!isset($column['column_name']))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
107
            {
108
              $styledColumns[$key - 1]['column_name'] = sprintf('<mm_column>%s</>', $styledColumns[$key - 1]['column_name']);
109
            }
110
            $styledColumn['column_name']      = sprintf('<mm_column>%s</>', $styledColumn['column_name']);
111
            $styledColumn['data_table_type']  = sprintf('<mm_type>%s</>', $styledColumn['data_table_type']);
112
            $styledColumn['audit_table_type'] = sprintf('<mm_type>%s</>', $styledColumn['audit_table_type']);
113
          }
114
        }
115
        else
116
        {
117
          // Highlighting for audit table column types and audit_columns in config file.
118
          $searchColumn = StaticDataLayer::searchInRowSet('column_name', $styledColumn['column_name'], $this->auditColumns);
119
          if (isset($searchColumn))
120
          {
121
            $configType = $this->auditColumns[$searchColumn]['column_type'];
122
            if (isset($configType) && !isset($column['audit_table_type']))
123
            {
124
              $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']);
125
              $styledColumn['config_type'] = sprintf('<mm_type>%s</>', $styledColumn['config_type']);
126
            }
127
            else if (!isset($configType) && isset($column['audit_table_type']))
128
            {
129
              $styledColumn['audit_table_type'] = sprintf('<mm_type>%s</>', $column['audit_table_type']);
130
            }
131
            else if (strcmp($configType, $column['audit_table_type']))
132
            {
133
              $styledColumn['column_name']      = sprintf('<mm_column>%s</>', $styledColumn['column_name']);
134
              $styledColumn['audit_table_type'] = sprintf('<mm_type>%s</>', $column['audit_table_type']);
135
              $styledColumn['config_type']      = sprintf('<mm_type>%s</>', $styledColumn['config_type']);
136
            }
137
          }
138
        }
139
      }
140
      $styledColumns[] = $styledColumn;
141
    }
142
143
    $this->rows = $styledColumns;
144
  }
145
146
  //--------------------------------------------------------------------------------------------------------------------
147
  /**
148
   * Appends rows.
149
   *
150
   * @param array[] $theRows Rows array.
151
   */
152
  public function appendRows($theRows)
153
  {
154
    /** @var ColumnMetadataExtended $row */
155
    foreach ($theRows as $row)
156
    {
157
      DiffTableRowHelper::appendRow($this->rows, $row);
0 ignored issues
show
Documentation introduced by
$row is of type array, but the function expects a object<SetBased\Audit\My...ColumnMetadataExtended>.

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...
158
    }
159
    $this->appendTableOption('engine');
160
    $this->appendTableOption('character_set_name', 'character set');
161
    $this->appendTableOption('table_collation', 'collation');
162
  }
163
164
  //--------------------------------------------------------------------------------------------------------------------
165
  /**
166
   * Append row with table option.
167
   *
168
   * @param string      $theOption The option.
169
   * @param null|string $theName   Display name.
170
   */
171
  public function appendTableOption($theOption, $theName = null)
172
  {
173
    if ($this->dataTableOptions[$theOption]!=$this->auditTableOptions[$theOption] || $this->fullOption)
174
    {
175
      if (!$this->existSeparator)
176
      {
177
        $this->rows[]         = new TableSeparator();
178
        $this->existSeparator = true;
179
      }
180
      if ($theName===null)
181
      {
182
        $theName = $theOption;
183
      }
184
      $tableRow               = ['column_name'        => $theName,
185
                                 'data_column_type'   => $this->dataTableOptions[$theOption],
186
                                 'audit_column_type'  => $this->auditTableOptions[$theOption],
187
                                 'config_column_type' => null];
188
      $this->rows[$theOption] = DiffTableRowHelper::createTableRow($tableRow);
189
    }
190
  }
191
192
  //--------------------------------------------------------------------------------------------------------------------
193
  /**
194
   * Get rows.
195
   *
196
   * @return \array[]
197
   */
198
  public function getRows()
199
  {
200
    return $this->rows;
201
  }
202
203
  //--------------------------------------------------------------------------------------------------------------------
204
}
205
206
//----------------------------------------------------------------------------------------------------------------------
207