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