| Conditions | 7 |
| Paths | 2 |
| Total Lines | 78 |
| Code Lines | 42 |
| 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 |
||
| 74 | public function rules() |
||
| 75 | { |
||
| 76 | return [ |
||
| 77 | [ |
||
| 78 | [ |
||
| 79 | 'created_at', |
||
| 80 | 'updated_at', |
||
| 81 | ], |
||
| 82 | 'safe', |
||
| 83 | ], |
||
| 84 | [ |
||
| 85 | [ |
||
| 86 | 'active', |
||
| 87 | ], |
||
| 88 | 'required', |
||
| 89 | ], |
||
| 90 | [ |
||
| 91 | [ |
||
| 92 | 'parentId', |
||
| 93 | 'active' |
||
| 94 | ], |
||
| 95 | 'integer', |
||
| 96 | ], |
||
| 97 | [ |
||
| 98 | [ |
||
| 99 | 'icon', |
||
| 100 | 'alias', |
||
| 101 | ], |
||
| 102 | 'string', |
||
| 103 | 'max' => 128, |
||
| 104 | ], |
||
| 105 | [ |
||
| 106 | 'parentId', |
||
| 107 | 'filter', |
||
| 108 | 'filter' => function ($value) { |
||
| 109 | if (empty($value)) { |
||
| 110 | return null; |
||
| 111 | } else { |
||
| 112 | return MenuWidget::checkNewParentId($this, $value) ? $value : $this->getOldAttribute('parentId'); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | ], |
||
| 116 | [ |
||
| 117 | 'alias', |
||
| 118 | 'filter', |
||
| 119 | 'filter' => function ($value) { |
||
| 120 | return preg_replace( '/[^a-z0-9_]+/', '-', strtolower(trim($value))); |
||
| 121 | } |
||
| 122 | ], |
||
| 123 | [ |
||
| 124 | 'alias', |
||
| 125 | 'unique', |
||
| 126 | 'skipOnError' => true, |
||
| 127 | 'targetClass' => static::class, |
||
| 128 | 'filter' => $this->getScenario() == ModelInterface::SCENARIO_UPDATE ? 'id != '.$this->id : '' |
||
| 129 | ], |
||
| 130 | [ |
||
| 131 | UploadModelInterface::FILE_TYPE_THUMB, |
||
| 132 | function($attribute){ |
||
| 133 | if (!is_numeric($this->{$attribute}) && !is_string($this->{$attribute})){ |
||
| 134 | $this->addError($attribute, 'Tumbnail content must be a numeric or string.'); |
||
| 135 | } |
||
| 136 | }, |
||
| 137 | 'skipOnError' => false, |
||
| 138 | ], |
||
| 139 | [ |
||
| 140 | UploadModelInterface::FILE_TYPE_IMAGE, |
||
| 141 | function($attribute) { |
||
| 142 | if (!is_array($this->{$attribute})) { |
||
| 143 | $this->addError($attribute, 'Image field content must be an array.'); |
||
| 144 | } |
||
| 145 | }, |
||
| 146 | 'skipOnError' => false, |
||
| 147 | ], |
||
| 148 | [ |
||
| 149 | 'albums', |
||
| 150 | 'each', |
||
| 151 | 'rule' => ['integer'], |
||
| 152 | ], |
||
| 283 |