Completed
Push — master ( 94b62d...da3388 )
by P.R.
9s
created

TableHelper::addHighlighting()   C

Complexity

Conditions 15
Paths 11

Size

Total Lines 57
Code Lines 30

Duplication

Lines 20
Ratio 35.09 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 20
loc 57
rs 6.5498
cc 15
eloc 30
nc 11
nop 0

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
//----------------------------------------------------------------------------------------------------------------------
6
use SetBased\Audit\ColumnTypes;
7
use SetBased\Audit\MySql\DataLayer;
8
use SetBased\Stratum\MySql\StaticDataLayer;
9
use Symfony\Component\Console\Helper\TableSeparator;
10
11
/**
12
 * A helper class for creating printing Tables.
13
 */
14
class TableHelper
15
{
16
  //--------------------------------------------------------------------------------------------------------------------
17
  /**
18
   * Array with rows for table.
19
   *
20
   * @var \array[]
21
   */
22
  private $rows = [];
23
24
  /**
25
   * Audit columns from config file.
26
   *
27
   * @var array
28
   */
29
  private $auditColumns;
30
31
  /**
32
   * Table options from audit schema.
33
   *
34
   * @var array
35
   */
36
  private $auditTableOptions;
37
38
  /**
39
   * Table options from data schema.
40
   *
41
   * @var array
42
   */
43
  private $dataTableOptions;
44
45
  /**
46
   * Full option.
47
   *
48
   * @var bool
49
   */
50
  private $fullOption;
51
52
  /**
53
   * Check existing separator.
54
   *
55
   * @var bool
56
   */
57
  private $existSeparator = false;
58
59
  //--------------------------------------------------------------------------------------------------------------------
60
  /**
61
   * Object constructor.
62
   *
63
   * @param string  $dataSchema      Data schema name.
64
   * @param string  $auditSchema     Audit schema name.
65
   * @param string  $tableName       The table name.
66
   * @param array[] $theAuditColumns Audit columns from config file.
67
   * @param bool    $fullOption      If set append table options to rows.
68
   */
69
  public function __construct($dataSchema, $auditSchema, $tableName, $theAuditColumns, $fullOption)
70
  {
71
    $this->auditColumns      = $theAuditColumns;
72
    $this->fullOption        = $fullOption;
73
    $this->auditTableOptions = DataLayer::getTableOptions($auditSchema, $tableName);
74
    $this->dataTableOptions  = DataLayer::getTableOptions($dataSchema, $tableName);
75
  }
76
77
  //--------------------------------------------------------------------------------------------------------------------
78
  /**
79
   * Append row with table option.
80
   *
81
   * @param string      $theOption The option.
82
   * @param null|string $theName   Display name.
83
   */
84
  public function appendTableOption($theOption, $theName = null)
85
  {
86
    if ($this->dataTableOptions[$theOption]!=$this->auditTableOptions[$theOption] || $this->fullOption)
87
    {
88
      if (!$this->existSeparator)
89
      {
90
        $this->rows[]         = new TableSeparator();
91
        $this->existSeparator = true;
92
      }
93
      if ($theName===null)
94
      {
95
        $theName = $theOption;
96
      }
97
      $tableRow               = ['column_name'        => $theName,
98
                                 'data_column_type'   => $this->dataTableOptions[$theOption],
99
                                 'audit_column_type'  => $this->auditTableOptions[$theOption],
100
                                 'config_column_type' => null];
101
      $this->rows[$theOption] = RowHelper::createTableRow($tableRow);
102
    }
103
  }
104
105
  //--------------------------------------------------------------------------------------------------------------------
106
  /**
107
   * Appends rows.
108
   *
109
   * @param \array[] $theRows Rows array.
110
   */
111
  public function appendRows($theRows)
112
  {
113
    /** @var ColumnTypes $row */
114
    foreach ($theRows as $row)
115
    {
116
      RowHelper::appendRow($this->rows, $row);
117
    }
118
    $this->appendTableOption('engine');
119
    $this->appendTableOption('character_set_name', 'character set');
120
    $this->appendTableOption('table_collation', 'collation');
121
  }
122
123
  //--------------------------------------------------------------------------------------------------------------------
124
  /**
125
   * Add highlighting to columns.
126
   */
127
  public function addHighlighting()
128
  {
129
    $styledColumns = [];
130
    foreach ($this->rows as $column)
131
    {
132
      $styledColumn = $column;
133
      if (is_array($column))
134
      {
135
        // Highlighting for data table column types and audit.
136
        if (!empty($column['data_table_type']))
137
        {
138
          if (isset($column['data_table_type']) && !isset($column['audit_table_type']))
139
          {
140
            $styledColumn['column_name']     = sprintf('<mm_column>%s</>', $styledColumn['column_name']);
141
            $styledColumn['data_table_type'] = sprintf('<mm_type>%s</>', $styledColumn['data_table_type']);
142
          }
143 View Code Duplication
          else if (!isset($column['data_table_type']) && isset($column['audit_table_type']))
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...
144
          {
145
            $styledColumn['audit_table_type'] = sprintf('<mm_type>%s</>', $styledColumn['audit_table_type']);
146
          }
147
          else if (strcmp($column['data_table_type'], $column['audit_table_type']))
148
          {
149
            $styledColumn['column_name']      = sprintf('<mm_column>%s</>', $styledColumn['column_name']);
150
            $styledColumn['data_table_type']  = sprintf('<mm_type>%s</>', $styledColumn['data_table_type']);
151
            $styledColumn['audit_table_type'] = sprintf('<mm_type>%s</>', $styledColumn['audit_table_type']);
152
          }
153
        }
154
        else
155
        {
156
          // Highlighting for audit table column types and audit_columns in config file.
157
          $searchColumn = StaticDataLayer::searchInRowSet('column_name', $styledColumn['column_name'], $this->auditColumns);
158
          if (isset($searchColumn))
159
          {
160
            $configType = $this->auditColumns[$searchColumn]['column_type'];
161
            if (isset($configType) && !isset($column['audit_table_type']))
162
            {
163
              $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']);
164
              $styledColumn['config_type'] = sprintf('<mm_type>%s</>', $styledColumn['config_type']);
165
            }
166 View Code Duplication
            else if (!isset($configType) && isset($column['audit_table_type']))
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...
167
            {
168
              $styledColumn['audit_table_type'] = sprintf('<mm_type>%s</>', $column['audit_table_type']);
169
            }
170
            else if (strcmp($configType, $column['audit_table_type']))
171
            {
172
              $styledColumn['column_name']      = sprintf('<mm_column>%s</>', $styledColumn['column_name']);
173
              $styledColumn['audit_table_type'] = sprintf('<mm_type>%s</>', $column['audit_table_type']);
174
              $styledColumn['config_type']      = sprintf('<mm_type>%s</>', $styledColumn['config_type']);
175
            }
176
          }
177
        }
178
      }
179
      $styledColumns[] = $styledColumn;
180
    }
181
182
    $this->rows = $styledColumns;
183
  }
184
185
  //--------------------------------------------------------------------------------------------------------------------
186
  /**
187
   * Get rows.
188
   *
189
   * @return \array[]
190
   */
191
  public function getRows()
192
  {
193
    return $this->rows;
194
  }
195
196
  //--------------------------------------------------------------------------------------------------------------------
197
}
198
199
//----------------------------------------------------------------------------------------------------------------------
200