| Total Lines | 68 |
| Code Lines | 48 |
| 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 |
||
| 159 | public function dataValidationFailed(): array |
||
| 160 | { |
||
| 161 | $class = new class () { |
||
| 162 | public $attr1 = 1; |
||
| 163 | public $attr2 = null; |
||
| 164 | }; |
||
| 165 | $array = ['attr1' => 1, 'attr2' => null]; |
||
| 166 | |||
| 167 | return [ |
||
| 168 | 'incorrect input' => [ |
||
| 169 | 1, |
||
| 170 | [new AtLeast(['attr2'])], |
||
| 171 | ['' => ['Value must be an array or an object.']], |
||
| 172 | ], |
||
| 173 | 'custom incorrect input message' => [ |
||
| 174 | 1, |
||
| 175 | [new AtLeast(['attr2'], incorrectInputMessage: 'Custom incorrect input message.')], |
||
| 176 | ['' => ['Custom incorrect input message.']], |
||
| 177 | ], |
||
| 178 | 'custom incorrect input message with parameters' => [ |
||
| 179 | 1, |
||
| 180 | [new AtLeast(['attr2'], incorrectInputMessage: 'Attribute - {attribute}, type - {type}.')], |
||
| 181 | ['' => ['Attribute - , type - int.']], |
||
| 182 | ], |
||
| 183 | 'custom incorrect input message with parameters, attribute set' => [ |
||
| 184 | ['attribute' => 1], |
||
| 185 | [ |
||
| 186 | 'attribute' => new AtLeast( |
||
| 187 | ['attr2'], |
||
| 188 | incorrectInputMessage: 'Attribute - {attribute}, type - {type}.', |
||
| 189 | ), |
||
| 190 | ], |
||
| 191 | ['attribute' => ['Attribute - attribute, type - int.']], |
||
| 192 | ], |
||
| 193 | 'object' => [ |
||
| 194 | $class, |
||
| 195 | [new AtLeast(['attr2'])], |
||
| 196 | ['' => ['The model is not valid. Must have at least "1" filled attributes.']], |
||
| 197 | ], |
||
| 198 | 'object, custom min' => [ |
||
| 199 | $class, |
||
| 200 | [new AtLeast(['attr1', 'attr2'], min: 2)], |
||
| 201 | ['' => ['The model is not valid. Must have at least "2" filled attributes.']], |
||
| 202 | ], |
||
| 203 | 'array' => [ |
||
| 204 | $array, |
||
| 205 | [new AtLeast(['attr2'])], |
||
| 206 | ['' => ['The model is not valid. Must have at least "1" filled attributes.']], |
||
| 207 | ], |
||
| 208 | 'array, custom min' => [ |
||
| 209 | $array, |
||
| 210 | [new AtLeast(['attr2'], min: 2)], |
||
| 211 | ['' => ['The model is not valid. Must have at least "2" filled attributes.']], |
||
| 212 | ], |
||
| 213 | 'custom message' => [ |
||
| 214 | $class, |
||
| 215 | [new AtLeast(['attr1', 'attr2'], min: 2, message: 'Custom message.')], |
||
| 216 | ['' => ['Custom message.']], |
||
| 217 | ], |
||
| 218 | 'custom message with parameters' => [ |
||
| 219 | $class, |
||
| 220 | [new AtLeast(['attr1', 'attr2'], min: 2, message: 'Attribute - {attribute}, min - {min}.')], |
||
| 221 | ['' => ['Attribute - , min - 2.']], |
||
| 222 | ], |
||
| 223 | 'custom message with parameters, attribute set' => [ |
||
| 224 | ['data' => $class], |
||
| 225 | ['data' => new AtLeast(['attr1', 'attr2'], min: 2, message: 'Attribute - {attribute}, min - {min}.')], |
||
| 226 | ['data' => ['Attribute - data, min - 2.']], |
||
| 227 | ], |
||
| 247 |