GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

BModelCode   A
last analyzed

Complexity

Total Complexity 42

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 165
rs 9.0399
c 0
b 0
f 0
wmc 42
lcom 3
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
B generateLabels() 0 26 7
F generateRules() 0 53 23
A getDefaultSort() 0 12 3
A getTimestampAttribute() 0 12 3
A getBehaviors() 0 35 5
A getColumns() 0 6 1

How to fix   Complexity   

Complex Class

Complex classes like BModelCode often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use BModelCode, and based on these observations, apply Extract Interface, too.

1
<?php
2
Yii::import('system.gii.generators.model.ModelCode');
3
4
class BModelCode extends ModelCode
5
{
6
  public $labelException = array('id');
7
8
  public $requiredException = array('position', 'img', 'visible');
9
10
  public $validateException = array('img');
11
12
  public $module;
13
14
  private $defaultSort;
15
16
  public function generateLabels($table)
17
  {
18
    $labels = array();
19
20
    foreach($table->columns as $column)
21
    {
22
      if( in_array($column->name, $this->labelException) )
23
        continue;
24
25
      if( $this->commentsAsLabels && $column->comment )
26
        $labels[$column->name] = $column->comment;
27
      else
28
      {
29
        $label = ucwords(trim(strtolower(str_replace(array('-', '_'), ' ', preg_replace('/(?<![A-Z])[A-Z]/', ' \0', $column->name)))));
30
        $label = preg_replace('/\s+/', ' ', $label);
31
        if( strcasecmp(substr($label, -3), ' id') === 0 )
32
          $label = substr($label, 0, -3);
33
        if( $label === 'Id' )
34
          $label = 'ID';
35
        $label = str_replace("'", "\\'", $label);
36
        $labels[$column->name] = $label;
37
      }
38
    }
39
40
    return $labels;
41
  }
42
43
  public function generateRules($table)
44
  {
45
    $rules = array();
46
    $required = array();
47
    $integers = array();
48
    $numerical = array();
49
    $length = array();
50
    $safe = array();
51
    $emails = array();
52
53
    /**
54
     * @var CDbColumnSchema $column
55
     */
56
    foreach($table->columns as $column)
57
    {
58
      if( $column->autoIncrement || in_array($column->name, $this->validateException) )
59
        continue;
60
61
      $r = !$column->allowNull && $column->defaultValue === null && $column->dbType != 'text' && $column->dbType != 'timestamp';
62
63
      if( $r && !in_array($column->name, $this->requiredException) )
64
        $required[] = $column->name;
65
      if( $column->type === 'integer' )
66
        $integers[] = $column->name;
67
      elseif( $column->type === 'double' )
68
        $numerical[] = $column->name;
69
      elseif( $column->type === 'string' && $column->size > 0 )
70
        $length[$column->size][] = $column->name;
71
      elseif( !$column->isPrimaryKey && !$r )
72
        $safe[] = $column->name;
73
74
      if( $column->name == 'email')
75
        $emails[] = $column->name;
76
    }
77
78
    if( $required !== array() )
79
      $rules[] = "array('".implode(', ', $required)."', 'required')";
80
    if( $integers !== array() )
81
      $rules[] = "array('".implode(', ', $integers)."', 'numerical', 'integerOnly' => true)";
82
    if( $numerical !== array() )
83
      $rules[] = "array('".implode(', ', $numerical)."', 'numerical')";
84
    if( $length !== array() )
85
    {
86
      foreach($length as $len => $cols)
87
        $rules[] = "array('".implode(', ', $cols)."', 'length', 'max' => $len)";
88
    }
89
    if( $emails !== array() )
90
      $rules[] = "array('".implode(', ', $emails)."', 'email')";
91
    if( $safe !== array() )
92
      $rules[] = "array('".implode(', ', $safe)."', 'safe')";
93
94
    return $rules;
95
  }
96
97
  public function getDefaultSort()
98
  {
99
    if( is_null($this->defaultSort) )
100
    {
101
      $this->defaultSort = '';
102
103
      if( $timestampAttribute = $this->getTimestampAttribute() )
104
        $this->defaultSort = $timestampAttribute;
105
    }
106
107
    return $this->defaultSort;
108
  }
109
110
  public function getTimestampAttribute()
111
  {
112
    foreach($this->getColumns() as $column)
113
    {
114
      if( $column->dbType == 'timestamp' )
115
      {
116
        return $column->name;
117
      }
118
    }
119
120
    return null;
121
  }
122
123
  public function getBehaviors($backend = true)
124
  {
125
    $behaviors = array();
126
127
    if( $backend )
128
    {
129
      if( $timestampAttribute = $this->getTimestampAttribute() )
130
      {
131
        $behaviors['dateFilterBehavior'] = array(
132
          'class' => 'DateFilterBehavior',
133
          'attribute' => $timestampAttribute,
134
        );
135
      }
136
137
      if( isset($this->getColumns()['img']) )
138
      {
139
        $behaviors['uploadBehavior'] = array(
140
          'class' => 'UploadBehavior',
141
          'validAttributes' => "img"
142
        );
143
      }
144
    }
145
    else
146
    {
147
      if( isset($this->getColumns()['img']) )
148
      {
149
        $behaviors['imageBehavior'] = array(
150
          'class' => 'SingleImageBehavior',
151
          'path' => $this->module,
152
         );
153
      }
154
    }
155
156
    return $behaviors;
157
  }
158
159
  /**
160
   * @return CDbColumnSchema[]
161
   */
162
  private function getColumns()
163
  {
164
    $table = Yii::app()->db->getSchema()->getTable($this->tableName);
165
166
    return $table->columns;
167
  }
168
}
169