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

DiffTableHelper::addHighlighting()   C

Complexity

Conditions 17
Paths 13

Size

Total Lines 65
Code Lines 34

Duplication

Lines 8
Ratio 12.31 %

Code Coverage

Tests 0
CRAP Score 306

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 8
loc 65
ccs 0
cts 57
cp 0
rs 5.9044
cc 17
eloc 34
nc 13
nop 0
crap 306

How to fix   Long Method    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\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