Conditions | 1 |
Total Lines | 138 |
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 |
||
122 | public function dataValidationFailed(): array |
||
123 | { |
||
124 | $lessThanMinmessage = 'This value must contain at least 3 items.'; |
||
125 | $greaterThanMaxMessage = 'This value must contain at most 3 items.'; |
||
126 | |||
127 | return [ |
||
128 | 'incorrect input' => [ |
||
129 | 1, |
||
130 | [new Count(min: 3)], |
||
131 | ['' => ['This value must be an array or implement \Countable interface.']], |
||
132 | ], |
||
133 | 'custom incorrect input message' => [ |
||
134 | 1, |
||
135 | [new Count(min: 3, incorrectInputMessage: 'Custom incorrect input message.')], |
||
136 | ['' => ['Custom incorrect input message.']], |
||
137 | ], |
||
138 | 'custom incorrect input message with parameters' => [ |
||
139 | 1, |
||
140 | [new Count(min: 3, incorrectInputMessage: 'Attribute - {attribute}, type - {type}.')], |
||
141 | ['' => ['Attribute - , type - int.']], |
||
142 | ], |
||
143 | 'custom incorrect input message, attribute set' => [ |
||
144 | ['data' => 1], |
||
145 | ['data' => new Count(min: 3, incorrectInputMessage: 'Attribute - {attribute}, type - {type}.')], |
||
146 | ['data' => ['Attribute - data, type - int.']], |
||
147 | ], |
||
148 | |||
149 | [[1], new Count(3), ['' => ['This value must contain exactly 3 items.']]], |
||
150 | [[1], [new Count(min: 3)], ['' => [$lessThanMinmessage]]], |
||
151 | [[], [new Count(min: 3)], ['' => [$lessThanMinmessage]]], |
||
152 | [[0, 0], [new Count(min: 3)], ['' => [$lessThanMinmessage]]], |
||
153 | [[1.1], [new Count(min: 3)], ['' => [$lessThanMinmessage]]], |
||
154 | [[''], [new Count(min: 3)], ['' => [$lessThanMinmessage]]], |
||
155 | [['some string'], [new Count(min: 3)], ['' => [$lessThanMinmessage]]], |
||
156 | [[new stdClass()], [new Count(min: 3)], ['' => [$lessThanMinmessage]]], |
||
157 | 'value: array iterator with lower count, min: positive' => [ |
||
158 | new ArrayIterator([0, 0]), |
||
159 | [new Count(min: 3)], |
||
160 | ['' => [$lessThanMinmessage]], |
||
161 | ], |
||
162 | // https://www.php.net/manual/ru/class.countable.php |
||
163 | 'value: class with min count returned from count method but not implenting Countable interface, min: 3' => [ |
||
164 | [ |
||
165 | new class () { |
||
166 | protected int $myCount = 3; |
||
167 | |||
168 | public function count(): int |
||
169 | { |
||
170 | return $this->myCount; |
||
171 | } |
||
172 | }, |
||
173 | ], |
||
174 | [new Count(min: 3)], |
||
175 | ['' => [$lessThanMinmessage]], |
||
176 | ], |
||
177 | [[0, 0, 0, 0], [new Count(max: 3)], ['' => [$greaterThanMaxMessage]]], |
||
178 | |||
179 | 'custom less than min message' => [ |
||
180 | [0, 0], |
||
181 | [new Count(min: 3, lessThanMinMessage: 'Custom less than min message.')], |
||
182 | ['' => ['Custom less than min message.']], |
||
183 | ], |
||
184 | 'custom less than min message with parameters' => [ |
||
185 | [0, 0], |
||
186 | [new Count(min: 3, lessThanMinMessage: 'Min - {min}, attribute - {attribute}, number - {number}.')], |
||
187 | ['' => ['Min - 3, attribute - , number - 2.']], |
||
188 | ], |
||
189 | 'custom less than min message with parameters, attribute set' => [ |
||
190 | ['data' => [0, 0]], |
||
191 | [ |
||
192 | 'data' => new Count( |
||
193 | min: 3, |
||
194 | lessThanMinMessage: 'Min - {min}, attribute - {attribute}, number - {number}.', |
||
195 | ), |
||
196 | ], |
||
197 | ['data' => ['Min - 3, attribute - data, number - 2.']], |
||
198 | ], |
||
199 | |||
200 | 'custom greater than max message' => [ |
||
201 | [0, 0, 0, 0], |
||
202 | [new Count(max: 3, greaterThanMaxMessage: 'Custom greater than max message.')], |
||
203 | ['' => ['Custom greater than max message.']], |
||
204 | ], |
||
205 | 'custom greater than max message with parameters' => [ |
||
206 | [0, 0, 0, 0], |
||
207 | [new Count(max: 3, greaterThanMaxMessage: 'Max - {max}, attribute - {attribute}, number - {number}.')], |
||
208 | ['' => ['Max - 3, attribute - , number - 4.']], |
||
209 | ], |
||
210 | 'custom greater than max message with parameters, attribute set' => [ |
||
211 | ['data' => [0, 0, 0, 0]], |
||
212 | [ |
||
213 | 'data' => new Count( |
||
214 | max: 3, |
||
215 | greaterThanMaxMessage: 'Max - {max}, attribute - {attribute}, number - {number}.', |
||
216 | ), |
||
217 | ], |
||
218 | ['data' => ['Max - 3, attribute - data, number - 4.']], |
||
219 | ], |
||
220 | |||
221 | 'custom not exactly message' => [ |
||
222 | [0, 0, 0, 0], |
||
223 | [new Count(3, notExactlyMessage: 'Custom not exactly message.')], |
||
224 | ['' => ['Custom not exactly message.']], |
||
225 | ], |
||
226 | 'custom not exactly message with parameters' => [ |
||
227 | [0, 0, 0, 0], |
||
228 | [ |
||
229 | new Count( |
||
230 | exactly: 3, |
||
231 | notExactlyMessage: 'Exactly - {exactly}, attribute - {attribute}, number - {number}.', |
||
232 | ), |
||
233 | ], |
||
234 | ['' => ['Exactly - 3, attribute - , number - 4.']], |
||
235 | ], |
||
236 | 'custom not exactly message with parameters, attribute set' => [ |
||
237 | ['data' => [0, 0, 0, 0]], |
||
238 | [ |
||
239 | 'data' => new Count( |
||
240 | exactly: 3, |
||
241 | notExactlyMessage: 'Exactly - {exactly}, attribute - {attribute}, number - {number}.', |
||
242 | ), |
||
243 | ], |
||
244 | ['data' => ['Exactly - 3, attribute - data, number - 4.']], |
||
245 | ], |
||
246 | 'class attribute' => [ |
||
247 | new CountDto(), |
||
248 | null, |
||
249 | ['' => ['This value must contain at least 2 items.']], |
||
250 | ], |
||
251 | 'value: array with greater count, exactly: 0' => [ |
||
252 | [0], |
||
253 | [new Count(0)], |
||
254 | ['' => ['This value must contain exactly 0 items.']], |
||
255 | ], |
||
256 | 'value: empty array iterator, exactly: positive' => [ |
||
257 | new ArrayIterator(), |
||
258 | [new Count(1)], |
||
259 | ['' => ['This value must contain exactly 1 item.']], |
||
260 | ], |
||
285 |