Conditions | 1 |
Paths | 1 |
Total Lines | 134 |
Code Lines | 78 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
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 |
||
101 | public function dataValidationFailed(): array |
||
102 | { |
||
103 | return [ |
||
104 | 'basic' => [ |
||
105 | 'hello', |
||
106 | [ |
||
107 | new StopOnError([ |
||
108 | new Length(min: 10), |
||
109 | new Length(max: 1), |
||
110 | ]), |
||
111 | ], |
||
112 | ['' => ['This value must contain at least 10 characters.']], |
||
113 | ], |
||
114 | 'basic, different order' => [ |
||
115 | 'hello', |
||
116 | [ |
||
117 | new StopOnError([ |
||
118 | new Length(max: 1), |
||
119 | new Length(min: 10), |
||
120 | ]), |
||
121 | ], |
||
122 | ['' => ['This value must contain at most 1 character.']], |
||
123 | ], |
||
124 | 'basic, plain StopOnError rule' => [ |
||
125 | 'hello', |
||
126 | new StopOnError([ |
||
127 | new Length(min: 10), |
||
128 | new Length(max: 1), |
||
129 | ]), |
||
130 | ['' => ['This value must contain at least 10 characters.']], |
||
131 | ], |
||
132 | 'combined with other top level rules' => [ |
||
133 | 'hello', |
||
134 | [ |
||
135 | new Number(), |
||
136 | new StopOnError([ |
||
137 | new Length(max: 1), |
||
138 | new Length(min: 10), |
||
139 | ]), |
||
140 | new Length(min: 7), |
||
141 | ], |
||
142 | [ |
||
143 | '' => [ |
||
144 | 'Value must be a number.', |
||
145 | 'This value must contain at most 1 character.', |
||
146 | 'This value must contain at least 7 characters.', |
||
147 | ], |
||
148 | ], |
||
149 | ], |
||
150 | 'combined with other top level rules, skipOnError: true' => [ |
||
151 | 'hello', |
||
152 | [ |
||
153 | new Number(), |
||
154 | new StopOnError( |
||
155 | [ |
||
156 | new Length(max: 1), |
||
157 | new Length(min: 10), |
||
158 | ], |
||
159 | skipOnError: true, |
||
160 | ), |
||
161 | new Length(min: 7), |
||
162 | ], |
||
163 | [ |
||
164 | '' => [ |
||
165 | 'Value must be a number.', |
||
166 | 'This value must contain at least 7 characters.', |
||
167 | ], |
||
168 | ], |
||
169 | ], |
||
170 | 'attributes, multiple StopOnError rules combined with other top level rules' => [ |
||
171 | [], |
||
172 | [ |
||
173 | 'a' => new Required(), |
||
174 | 'b' => new StopOnError([ |
||
175 | new Required(), |
||
176 | new Number(min: 7), |
||
177 | ]), |
||
178 | 'c' => new StopOnError([ |
||
179 | new Required(), |
||
180 | new Number(min: 42), |
||
181 | ]), |
||
182 | 'd' => new Required(), |
||
183 | ], |
||
184 | [ |
||
185 | 'a' => ['Value not passed.'], |
||
186 | 'b' => ['Value not passed.'], |
||
187 | 'c' => ['Value not passed.'], |
||
188 | 'd' => ['Value not passed.'], |
||
189 | ], |
||
190 | ], |
||
191 | 'attributes, multiple StopOnError rules combined with other top level rules, skipOnError: true' => [ |
||
192 | [], |
||
193 | [ |
||
194 | 'a' => new Required(), |
||
195 | 'b' => new StopOnError( |
||
196 | [ |
||
197 | new Required(), |
||
198 | new Number(min: 7), |
||
199 | ], |
||
200 | skipOnError: true, |
||
201 | ), |
||
202 | 'c' => new StopOnError( |
||
203 | [ |
||
204 | new Required(), |
||
205 | new Number(min: 42), |
||
206 | ], |
||
207 | skipOnError: true, |
||
208 | ), |
||
209 | 'd' => new Required(), |
||
210 | ], |
||
211 | [ |
||
212 | 'a' => ['Value not passed.'], |
||
213 | 'd' => ['Value not passed.'], |
||
214 | ], |
||
215 | ], |
||
216 | 'check for missing data set' => [ |
||
217 | ['b' => null], |
||
218 | [ |
||
219 | 'a' => new StopOnError([ |
||
220 | new Required(), |
||
221 | ]), |
||
222 | 'b' => new Required(), |
||
223 | ], |
||
224 | [ |
||
225 | 'a' => ['Value not passed.'], |
||
226 | 'b' => ['Value cannot be blank.'], |
||
227 | ], |
||
228 | ], |
||
229 | 'rules normalization, callable' => [ |
||
230 | [], |
||
231 | new StopOnError([ |
||
232 | static fn (): Result => (new Result())->addError('Custom error.'), |
||
233 | ]), |
||
234 | ['' => ['Custom error.']], |
||
235 | ], |
||
253 |