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::generateRules()   F
last analyzed

Complexity

Conditions 23
Paths 5248

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 23
nc 5248
nop 1
dl 0
loc 53
rs 0
c 0
b 0
f 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
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