Conditions | 23 |
Paths | 5248 |
Total Lines | 53 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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 | |||
169 |