| Conditions | 1 |
| Paths | 1 |
| Total Lines | 126 |
| Code Lines | 72 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 | 'combined with other top level rules' => [ |
||
| 125 | 'hello', |
||
| 126 | [ |
||
| 127 | new Number(), |
||
| 128 | new StopOnError([ |
||
| 129 | new Length(max: 1), |
||
| 130 | new Length(min: 10), |
||
| 131 | ]), |
||
| 132 | new Length(min: 7), |
||
| 133 | ], |
||
| 134 | [ |
||
| 135 | '' => [ |
||
| 136 | 'Value must be a number.', |
||
| 137 | 'This value must contain at most 1 character.', |
||
| 138 | 'This value must contain at least 7 characters.', |
||
| 139 | ], |
||
| 140 | ], |
||
| 141 | ], |
||
| 142 | 'combined with other top level rules, skipOnError: true' => [ |
||
| 143 | 'hello', |
||
| 144 | [ |
||
| 145 | new Number(), |
||
| 146 | new StopOnError( |
||
| 147 | [ |
||
| 148 | new Length(max: 1), |
||
| 149 | new Length(min: 10), |
||
| 150 | ], |
||
| 151 | skipOnError: true, |
||
| 152 | ), |
||
| 153 | new Length(min: 7), |
||
| 154 | ], |
||
| 155 | [ |
||
| 156 | '' => [ |
||
| 157 | 'Value must be a number.', |
||
| 158 | 'This value must contain at least 7 characters.', |
||
| 159 | ], |
||
| 160 | ], |
||
| 161 | ], |
||
| 162 | 'attributes, multiple StopOnError rules combined with other top level rules' => [ |
||
| 163 | [], |
||
| 164 | [ |
||
| 165 | 'a' => new Required(), |
||
| 166 | 'b' => new StopOnError([ |
||
| 167 | new Required(), |
||
| 168 | new Number(min: 7), |
||
| 169 | ]), |
||
| 170 | 'c' => new StopOnError([ |
||
| 171 | new Required(), |
||
| 172 | new Number(min: 42), |
||
| 173 | ]), |
||
| 174 | 'd' => new Required(), |
||
| 175 | ], |
||
| 176 | [ |
||
| 177 | 'a' => ['Value not passed.'], |
||
| 178 | 'b' => ['Value not passed.'], |
||
| 179 | 'c' => ['Value not passed.'], |
||
| 180 | 'd' => ['Value not passed.'], |
||
| 181 | ], |
||
| 182 | ], |
||
| 183 | 'attributes, multiple StopOnError rules combined with other top level rules, skipOnError: true' => [ |
||
| 184 | [], |
||
| 185 | [ |
||
| 186 | 'a' => new Required(), |
||
| 187 | 'b' => new StopOnError( |
||
| 188 | [ |
||
| 189 | new Required(), |
||
| 190 | new Number(min: 7), |
||
| 191 | ], |
||
| 192 | skipOnError: true, |
||
| 193 | ), |
||
| 194 | 'c' => new StopOnError( |
||
| 195 | [ |
||
| 196 | new Required(), |
||
| 197 | new Number(min: 42), |
||
| 198 | ], |
||
| 199 | skipOnError: true, |
||
| 200 | ), |
||
| 201 | 'd' => new Required(), |
||
| 202 | ], |
||
| 203 | [ |
||
| 204 | 'a' => ['Value not passed.'], |
||
| 205 | 'd' => ['Value not passed.'], |
||
| 206 | ], |
||
| 207 | ], |
||
| 208 | 'check for missing data set' => [ |
||
| 209 | ['b' => null], |
||
| 210 | [ |
||
| 211 | 'a' => new StopOnError([ |
||
| 212 | new Required(), |
||
| 213 | ]), |
||
| 214 | 'b' => new Required(), |
||
| 215 | ], |
||
| 216 | [ |
||
| 217 | 'a' => ['Value not passed.'], |
||
| 218 | 'b' => ['Value cannot be blank.'], |
||
| 219 | ], |
||
| 220 | ], |
||
| 221 | 'rules normalization, callable' => [ |
||
| 222 | [], |
||
| 223 | new StopOnError([ |
||
| 224 | static fn (): Result => (new Result())->addError('Custom error.'), |
||
| 225 | ]), |
||
| 226 | ['' => ['Custom error.']], |
||
| 227 | ], |
||
| 245 |