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 |
||
138 | public function dataValidationFailed(): array |
||
139 | { |
||
140 | $class = new class () { |
||
141 | public $attr1 = 1; |
||
142 | public $attr2 = null; |
||
143 | }; |
||
144 | $array = ['attr1' => 1, 'attr2' => null]; |
||
145 | |||
146 | return [ |
||
147 | 'incorrect input' => [ |
||
148 | 1, |
||
149 | [new AtLeast(['attr2'])], |
||
150 | ['' => ['Value must be an array or an object.']], |
||
151 | ], |
||
152 | 'custom incorrect input message' => [ |
||
153 | 1, |
||
154 | [new AtLeast(['attr2'], incorrectInputMessage: 'Custom incorrect input message.')], |
||
155 | ['' => ['Custom incorrect input message.']], |
||
156 | ], |
||
157 | 'custom incorrect input message with parameters' => [ |
||
158 | 1, |
||
159 | [new AtLeast(['attr2'], incorrectInputMessage: 'Attribute - {attribute}, type - {type}.')], |
||
160 | ['' => ['Attribute - , type - int.']], |
||
161 | ], |
||
162 | 'custom incorrect input message with parameters, attribute set' => [ |
||
163 | ['attribute' => 1], |
||
164 | [ |
||
165 | 'attribute' => new AtLeast( |
||
166 | ['attr2'], |
||
167 | incorrectInputMessage: 'Attribute - {attribute}, type - {type}.', |
||
168 | ), |
||
169 | ], |
||
170 | ['attribute' => ['Attribute - attribute, type - int.']], |
||
171 | ], |
||
172 | 'object' => [ |
||
173 | $class, |
||
174 | [new AtLeast(['attr2'])], |
||
175 | ['' => ['The model is not valid. Must have at least "1" filled attributes.']], |
||
176 | ], |
||
177 | 'object, custom min' => [ |
||
178 | $class, |
||
179 | [new AtLeast(['attr1', 'attr2'], min: 2)], |
||
180 | ['' => ['The model is not valid. Must have at least "2" filled attributes.']], |
||
181 | ], |
||
182 | 'array' => [ |
||
183 | $array, |
||
184 | [new AtLeast(['attr2'])], |
||
185 | ['' => ['The model is not valid. Must have at least "1" filled attributes.']], |
||
186 | ], |
||
187 | 'array, custom min' => [ |
||
188 | $array, |
||
189 | [new AtLeast(['attr2'], min: 2)], |
||
190 | ['' => ['The model is not valid. Must have at least "2" filled attributes.']], |
||
191 | ], |
||
192 | 'custom message' => [ |
||
193 | $class, |
||
194 | [new AtLeast(['attr1', 'attr2'], min: 2, message: 'Custom message.')], |
||
195 | ['' => ['Custom message.']], |
||
196 | ], |
||
197 | 'custom message with parameters' => [ |
||
198 | $class, |
||
199 | [new AtLeast(['attr1', 'attr2'], min: 2, message: 'Attribute - {attribute}, min - {min}.')], |
||
200 | ['' => ['Attribute - , min - 2.']], |
||
201 | ], |
||
202 | 'custom message with parameters, attribute set' => [ |
||
203 | ['data' => $class], |
||
204 | ['data' => new AtLeast(['attr1', 'attr2'], min: 2, message: 'Attribute - {attribute}, min - {min}.')], |
||
205 | ['data' => ['Attribute - data, min - 2.']], |
||
206 | ], |
||
226 |