| Total Complexity | 62 | 
| Total Lines | 394 | 
| Duplicated Lines | 0 % | 
| Changes | 2 | ||
| Bugs | 0 | Features | 0 | 
Complex classes like RuleFactory often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RuleFactory, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 8 | class RuleFactory extends AbstractRuleFactory  | 
            ||
| 9 | { | 
            ||
| 10 | /**  | 
            ||
| 11 | * Create a Rule for a valid boolean.  | 
            ||
| 12 | *  | 
            ||
| 13 | * @param mixed $default  | 
            ||
| 14 | *  | 
            ||
| 15 | * @return \FigTree\Validation\Contracts\RuleInterface  | 
            ||
| 16 | */  | 
            ||
| 17 | public function validBool($default = null): RuleInterface  | 
            ||
| 18 | 	{ | 
            ||
| 19 | return $this->applyDefault($this->create(FILTER_VALIDATE_BOOL, FILTER_NULL_ON_FAILURE), $default);  | 
            ||
| 20 | }  | 
            ||
| 21 | |||
| 22 | /**  | 
            ||
| 23 | * Create a Rule for a valid domain name.  | 
            ||
| 24 | *  | 
            ||
| 25 | * @param boolean $checkHostname Adds ability to specifically validate hostnames (they must start with an alphanumeric character and contain only alphanumerics or hyphens).  | 
            ||
| 26 | * @param mixed $default  | 
            ||
| 27 | *  | 
            ||
| 28 | * @return \FigTree\Validation\Contracts\RuleInterface  | 
            ||
| 29 | */  | 
            ||
| 30 | public function validDomain(bool $checkHostname = false, $default = null): RuleInterface  | 
            ||
| 31 | 	{ | 
            ||
| 32 | $flags = 0;  | 
            ||
| 33 | |||
| 34 | 		if ($checkHostname) { | 
            ||
| 35 | $flags |= FILTER_FLAG_HOSTNAME;  | 
            ||
| 36 | }  | 
            ||
| 37 | |||
| 38 | return $this->applyDefault($this->create(FILTER_VALIDATE_DOMAIN, $flags), $default);  | 
            ||
| 39 | }  | 
            ||
| 40 | |||
| 41 | /**  | 
            ||
| 42 | * Create a Rule for a valid e-mail address.  | 
            ||
| 43 | *  | 
            ||
| 44 | * @param boolean $checkUnicode  | 
            ||
| 45 | * @param mixed $default  | 
            ||
| 46 | *  | 
            ||
| 47 | * @return \FigTree\Validation\Contracts\RuleInterface  | 
            ||
| 48 | */  | 
            ||
| 49 | public function validEmail(bool $checkUnicode = false, $default = null): RuleInterface  | 
            ||
| 50 | 	{ | 
            ||
| 51 | $flags = 0;  | 
            ||
| 52 | |||
| 53 | 		if ($checkUnicode) { | 
            ||
| 54 | $flags |= FILTER_FLAG_EMAIL_UNICODE;  | 
            ||
| 55 | }  | 
            ||
| 56 | |||
| 57 | return $this->applyDefault($this->create(FILTER_VALIDATE_EMAIL, $flags), $default);  | 
            ||
| 58 | }  | 
            ||
| 59 | |||
| 60 | /**  | 
            ||
| 61 | * Create a Rule for a valid floating-point value.  | 
            ||
| 62 | *  | 
            ||
| 63 | * @param float|null $min  | 
            ||
| 64 | * @param float|null $max  | 
            ||
| 65 | * @param integer|null $decimals  | 
            ||
| 66 | * @param boolean $allowThousands Allow thousand separators (commas).  | 
            ||
| 67 | * @param mixed $default  | 
            ||
| 68 | *  | 
            ||
| 69 | * @return \FigTree\Validation\Contracts\RuleInterface  | 
            ||
| 70 | */  | 
            ||
| 71 | public function validFloat(?float $min = null, ?float $max = null, ?int $decimals = null, bool $allowThousands = false, $default = null): RuleInterface  | 
            ||
| 72 | 	{ | 
            ||
| 73 | $flags = 0;  | 
            ||
| 74 | $options = [];  | 
            ||
| 75 | |||
| 76 | 		if (!is_null($min)) { | 
            ||
| 77 | $options['min_range'] = $min;  | 
            ||
| 78 | }  | 
            ||
| 79 | |||
| 80 | 		if (!is_null($max)) { | 
            ||
| 81 | $options['max_range'] = $max;  | 
            ||
| 82 | }  | 
            ||
| 83 | |||
| 84 | 		if (!is_null($decimals)) { | 
            ||
| 85 | $options['decimal'] = $decimals;  | 
            ||
| 86 | }  | 
            ||
| 87 | |||
| 88 | 		if ($allowThousands) { | 
            ||
| 89 | $flags |= FILTER_FLAG_ALLOW_THOUSAND;  | 
            ||
| 90 | }  | 
            ||
| 91 | |||
| 92 | return $this->applyDefault($this->create(FILTER_VALIDATE_FLOAT, $flags, $options), $default);  | 
            ||
| 93 | }  | 
            ||
| 94 | |||
| 95 | /**  | 
            ||
| 96 | * Create a Rule for a valid integer.  | 
            ||
| 97 | *  | 
            ||
| 98 | * @param integer|null $min  | 
            ||
| 99 | * @param integer|null $max  | 
            ||
| 100 | * @param boolean $allowOctal  | 
            ||
| 101 | * @param boolean $allowHex  | 
            ||
| 102 | * @param mixed $default  | 
            ||
| 103 | *  | 
            ||
| 104 | * @return \FigTree\Validation\Contracts\RuleInterface  | 
            ||
| 105 | */  | 
            ||
| 106 | public function validInt(?int $min = null, ?int $max = null, bool $allowOctal = false, bool $allowHex = false, $default = null): RuleInterface  | 
            ||
| 107 | 	{ | 
            ||
| 108 | $flags = 0;  | 
            ||
| 109 | $options = [];  | 
            ||
| 110 | |||
| 111 | 		if (!is_null($min)) { | 
            ||
| 112 | $options['min_range'] = $min;  | 
            ||
| 113 | }  | 
            ||
| 114 | |||
| 115 | 		if (!is_null($max)) { | 
            ||
| 116 | $options['max_range'] = $max;  | 
            ||
| 117 | }  | 
            ||
| 118 | |||
| 119 | 		if ($allowOctal) { | 
            ||
| 120 | $flags |= FILTER_FLAG_ALLOW_OCTAL;  | 
            ||
| 121 | }  | 
            ||
| 122 | |||
| 123 | 		if ($allowHex) { | 
            ||
| 124 | $flags |= FILTER_FLAG_ALLOW_HEX;  | 
            ||
| 125 | }  | 
            ||
| 126 | |||
| 127 | return $this->applyDefault($this->create(FILTER_VALIDATE_INT, $flags, $options), $default);  | 
            ||
| 128 | }  | 
            ||
| 129 | |||
| 130 | /**  | 
            ||
| 131 | * Create a Rule for a valid IP address.  | 
            ||
| 132 | *  | 
            ||
| 133 | * @param boolean $allowV4  | 
            ||
| 134 | * @param boolean $allowV6  | 
            ||
| 135 | * @param boolean $allowPrivateRange Allow IP addresses within private ranges.  | 
            ||
| 136 | * @param boolean $allowReservedRange Allow IP addresses within other reserved ranges.  | 
            ||
| 137 | * @param mixed $default  | 
            ||
| 138 | *  | 
            ||
| 139 | * @return \FigTree\Validation\Contracts\RuleInterface  | 
            ||
| 140 | *  | 
            ||
| 141 | * @see https://en.wikipedia.org/wiki/Reserved_IP_addresses  | 
            ||
| 142 | */  | 
            ||
| 143 | public function validIpAddress(bool $allowV4 = false, bool $allowV6 = false, bool $allowPrivateRange = true, bool $allowReservedRange = true, $default = null): RuleInterface  | 
            ||
| 144 | 	{ | 
            ||
| 145 | $flags = 0;  | 
            ||
| 146 | |||
| 147 | 		if ($allowV4) { | 
            ||
| 148 | $flags |= FILTER_FLAG_IPV4;  | 
            ||
| 149 | }  | 
            ||
| 150 | |||
| 151 | 		if ($allowV6) { | 
            ||
| 152 | $flags |= FILTER_FLAG_IPV6;  | 
            ||
| 153 | }  | 
            ||
| 154 | |||
| 155 | 		if (!$allowPrivateRange) { | 
            ||
| 156 | $flags |= FILTER_FLAG_NO_PRIV_RANGE;  | 
            ||
| 157 | }  | 
            ||
| 158 | |||
| 159 | 		if (!$allowReservedRange) { | 
            ||
| 160 | $flags |= FILTER_FLAG_NO_RES_RANGE;  | 
            ||
| 161 | }  | 
            ||
| 162 | |||
| 163 | return $this->applyDefault($this->create(FILTER_VALIDATE_IP, $flags), $default);  | 
            ||
| 164 | }  | 
            ||
| 165 | |||
| 166 | /**  | 
            ||
| 167 | * Create a Rule for a valid MAC address.  | 
            ||
| 168 | *  | 
            ||
| 169 | * @param mixed $default  | 
            ||
| 170 | *  | 
            ||
| 171 | * @return \FigTree\Validation\Contracts\RuleInterface  | 
            ||
| 172 | */  | 
            ||
| 173 | public function validMacAddress($default = null): RuleInterface  | 
            ||
| 174 | 	{ | 
            ||
| 175 | return $this->applyDefault($this->create(FILTER_VALIDATE_MAC), $default);  | 
            ||
| 176 | }  | 
            ||
| 177 | |||
| 178 | /**  | 
            ||
| 179 | * Create a Rule for a valid regular expression match.  | 
            ||
| 180 | *  | 
            ||
| 181 | * @param string $regex  | 
            ||
| 182 | * @param mixed $default  | 
            ||
| 183 | *  | 
            ||
| 184 | * @return \FigTree\Validation\Contracts\RuleInterface  | 
            ||
| 185 | */  | 
            ||
| 186 | public function validRegExp(string $regex, $default = null): RuleInterface  | 
            ||
| 193 | }  | 
            ||
| 194 | |||
| 195 | /**  | 
            ||
| 196 | * Apply default option to the Rule.  | 
            ||
| 197 | *  | 
            ||
| 198 | * @param \FigTree\Validation\Contracts\RuleInterface $rule  | 
            ||
| 199 | * @param mixed $default  | 
            ||
| 200 | *  | 
            ||
| 201 | * @return \FigTree\Validation\Contracts\RuleInterface  | 
            ||
| 202 | */  | 
            ||
| 203 | protected function applyDefault(RuleInterface $rule, $default = null): RuleInterface  | 
            ||
| 204 | 	{ | 
            ||
| 205 | 		if (!is_null($default)) { | 
            ||
| 206 | 			$rule->setOption('default', $default); | 
            ||
| 207 | }  | 
            ||
| 208 | |||
| 209 | return $rule;  | 
            ||
| 210 | }  | 
            ||
| 211 | |||
| 212 | public function addSlashes(): RuleInterface  | 
            ||
| 213 | 	{ | 
            ||
| 214 | return $this->create(FILTER_SANITIZE_ADD_SLASHES);  | 
            ||
| 215 | }  | 
            ||
| 216 | |||
| 217 | public function cleanEmail(): RuleInterface  | 
            ||
| 218 | 	{ | 
            ||
| 219 | return $this->create(FILTER_SANITIZE_EMAIL);  | 
            ||
| 220 | }  | 
            ||
| 221 | |||
| 222 | public function cleanEncodedString(bool $stripLow = false, bool $stripHigh = false, bool $stripBacktick = false, bool $encodeLow = false, bool $encodeHigh = false): RuleInterface  | 
            ||
| 223 | 	{ | 
            ||
| 224 | $flags = 0;  | 
            ||
| 225 | |||
| 226 | 		if ($stripLow) { | 
            ||
| 227 | $flags |= FILTER_FLAG_STRIP_LOW;  | 
            ||
| 228 | }  | 
            ||
| 229 | |||
| 230 | 		if ($stripHigh) { | 
            ||
| 231 | $flags |= FILTER_FLAG_STRIP_HIGH;  | 
            ||
| 232 | }  | 
            ||
| 233 | |||
| 234 | 		if ($stripBacktick) { | 
            ||
| 235 | $flags |= FILTER_FLAG_STRIP_BACKTICK;  | 
            ||
| 236 | }  | 
            ||
| 237 | |||
| 238 | 		if ($encodeLow) { | 
            ||
| 239 | $flags |= FILTER_FLAG_ENCODE_LOW;  | 
            ||
| 240 | }  | 
            ||
| 241 | |||
| 242 | 		if ($encodeHigh) { | 
            ||
| 243 | $flags |= FILTER_FLAG_ENCODE_HIGH;  | 
            ||
| 244 | }  | 
            ||
| 245 | |||
| 246 | return $this->create(FILTER_SANITIZE_ENCODED, $flags);  | 
            ||
| 247 | }  | 
            ||
| 248 | |||
| 249 | public function cleanFloat(bool $allowFractions = false, bool $allowThousands = false, $allowScientific = false): RuleInterface  | 
            ||
| 250 | 	{ | 
            ||
| 251 | $flags = 0;  | 
            ||
| 252 | |||
| 253 | 		if ($allowFractions) { | 
            ||
| 254 | $flags |= FILTER_FLAG_ALLOW_FRACTION;  | 
            ||
| 255 | }  | 
            ||
| 256 | |||
| 257 | 		if ($allowThousands) { | 
            ||
| 258 | $flags |= FILTER_FLAG_ALLOW_THOUSAND;  | 
            ||
| 259 | }  | 
            ||
| 260 | |||
| 261 | 		if ($allowScientific) { | 
            ||
| 262 | $flags |= FILTER_FLAG_ALLOW_SCIENTIFIC;  | 
            ||
| 263 | }  | 
            ||
| 264 | |||
| 265 | return $this->create(FILTER_SANITIZE_NUMBER_FLOAT, $flags);  | 
            ||
| 266 | }  | 
            ||
| 267 | |||
| 268 | public function cleanFullSpecialChars(bool $encodeQuotes = true): RuleInterface  | 
            ||
| 269 | 	{ | 
            ||
| 270 | $flags = 0;  | 
            ||
| 271 | |||
| 272 | 		if (!$encodeQuotes) { | 
            ||
| 273 | $flags |= FILTER_FLAG_NO_ENCODE_QUOTES;  | 
            ||
| 274 | }  | 
            ||
| 275 | |||
| 276 | return $this->create(FILTER_SANITIZE_FULL_SPECIAL_CHARS, $flags);  | 
            ||
| 
                                                                                                    
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 277 | }  | 
            ||
| 278 | |||
| 279 | public function cleanInt(): RuleInterface  | 
            ||
| 280 | 	{ | 
            ||
| 281 | return $this->create(FILTER_SANITIZE_NUMBER_INT);  | 
            ||
| 282 | }  | 
            ||
| 283 | |||
| 284 | public function cleanSpecialChars(bool $stripLow = false, bool $stripHigh = false, bool $stripBacktick = false, bool $encodeHigh = false): RuleInterface  | 
            ||
| 285 | 	{ | 
            ||
| 286 | $flags = 0;  | 
            ||
| 287 | |||
| 288 | 		if ($stripLow) { | 
            ||
| 289 | $flags |= FILTER_FLAG_STRIP_LOW;  | 
            ||
| 290 | }  | 
            ||
| 291 | |||
| 292 | 		if ($stripHigh) { | 
            ||
| 293 | $flags |= FILTER_FLAG_STRIP_HIGH;  | 
            ||
| 294 | }  | 
            ||
| 295 | |||
| 296 | 		if ($stripBacktick) { | 
            ||
| 297 | $flags |= FILTER_FLAG_STRIP_BACKTICK;  | 
            ||
| 298 | }  | 
            ||
| 299 | |||
| 300 | 		if ($encodeHigh) { | 
            ||
| 301 | $flags |= FILTER_FLAG_ENCODE_HIGH;  | 
            ||
| 302 | }  | 
            ||
| 303 | |||
| 304 | return $this->create(FILTER_SANITIZE_SPECIAL_CHARS, $flags);  | 
            ||
| 305 | }  | 
            ||
| 306 | |||
| 307 | public function cleanString(bool $encodeQuotes = true, bool $stripLow = false, bool $stripHigh = false, bool $stripBacktick = false, bool $encodeLow = false, bool $encodeHigh = false, bool $encodeAmp = false): RuleInterface  | 
            ||
| 308 | 	{ | 
            ||
| 309 | $flags = 0;  | 
            ||
| 310 | |||
| 311 | 		if (!$encodeQuotes) { | 
            ||
| 312 | $flags |= FILTER_FLAG_NO_ENCODE_QUOTES;  | 
            ||
| 313 | }  | 
            ||
| 314 | |||
| 315 | 		if ($stripLow) { | 
            ||
| 316 | $flags |= FILTER_FLAG_STRIP_LOW;  | 
            ||
| 317 | }  | 
            ||
| 318 | |||
| 319 | 		if ($stripHigh) { | 
            ||
| 320 | $flags |= FILTER_FLAG_STRIP_HIGH;  | 
            ||
| 321 | }  | 
            ||
| 322 | |||
| 323 | 		if ($stripBacktick) { | 
            ||
| 324 | $flags |= FILTER_FLAG_STRIP_BACKTICK;  | 
            ||
| 325 | }  | 
            ||
| 326 | |||
| 327 | 		if ($encodeLow) { | 
            ||
| 328 | $flags |= FILTER_FLAG_ENCODE_LOW;  | 
            ||
| 329 | }  | 
            ||
| 330 | |||
| 331 | 		if ($encodeHigh) { | 
            ||
| 332 | $flags |= FILTER_FLAG_ENCODE_HIGH;  | 
            ||
| 333 | }  | 
            ||
| 334 | |||
| 335 | 		if ($encodeAmp) { | 
            ||
| 336 | $flags |= FILTER_FLAG_ENCODE_AMP;  | 
            ||
| 337 | }  | 
            ||
| 338 | |||
| 339 | return $this->create(FILTER_SANITIZE_STRING, $flags);  | 
            ||
| 340 | }  | 
            ||
| 341 | |||
| 342 | public function cleanUnsafe(bool $stripLow = false, bool $stripHigh = false, bool $stripBacktick = false, bool $encodeLow = false, bool $encodeHigh = false, bool $encodeAmp = false): RuleInterface  | 
            ||
| 343 | 	{ | 
            ||
| 344 | $flags = 0;  | 
            ||
| 345 | |||
| 346 | 		if ($stripLow) { | 
            ||
| 347 | $flags |= FILTER_FLAG_STRIP_LOW;  | 
            ||
| 348 | }  | 
            ||
| 349 | |||
| 350 | 		if ($stripHigh) { | 
            ||
| 351 | $flags |= FILTER_FLAG_STRIP_HIGH;  | 
            ||
| 352 | }  | 
            ||
| 353 | |||
| 354 | 		if ($stripBacktick) { | 
            ||
| 355 | $flags |= FILTER_FLAG_STRIP_BACKTICK;  | 
            ||
| 356 | }  | 
            ||
| 357 | |||
| 358 | 		if ($encodeLow) { | 
            ||
| 359 | $flags |= FILTER_FLAG_ENCODE_LOW;  | 
            ||
| 360 | }  | 
            ||
| 361 | |||
| 362 | 		if ($encodeHigh) { | 
            ||
| 363 | $flags |= FILTER_FLAG_ENCODE_HIGH;  | 
            ||
| 364 | }  | 
            ||
| 365 | |||
| 366 | 		if ($encodeAmp) { | 
            ||
| 367 | $flags |= FILTER_FLAG_ENCODE_AMP;  | 
            ||
| 368 | }  | 
            ||
| 369 | |||
| 370 | return $this->create(FILTER_UNSAFE_RAW, $flags);  | 
            ||
| 371 | }  | 
            ||
| 372 | |||
| 373 | public function cleanUrl(): RuleInterface  | 
            ||
| 376 | }  | 
            ||
| 377 | |||
| 378 | /**  | 
            ||
| 379 | * Create a Rule for a valid boolean.  | 
            ||
| 380 | *  | 
            ||
| 381 | * @param callable $callback  | 
            ||
| 382 | *  | 
            ||
| 383 | * @return \FigTree\Validation\Contracts\RuleInterface  | 
            ||
| 384 | */  | 
            ||
| 385 | public function withCallable(callable $callback): RuleInterface  | 
            ||
| 386 | 	{ | 
            ||
| 387 | return $this->create(FILTER_CALLBACK)  | 
            ||
| 388 | ->setCallback(Closure::fromCallable($callback));  | 
            ||
| 389 | }  | 
            ||
| 390 | |||
| 391 | /**  | 
            ||
| 392 | * Create a Rule for a valid boolean.  | 
            ||
| 393 | *  | 
            ||
| 394 | * @param \Closure $callback  | 
            ||
| 395 | *  | 
            ||
| 396 | * @return \FigTree\Validation\Contracts\RuleInterface  | 
            ||
| 397 | */  | 
            ||
| 398 | public function withClosure(Closure $callback): RuleInterface  | 
            ||
| 402 | }  | 
            ||
| 403 | }  | 
            ||
| 404 |