Complex classes like LogicalStatement 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 LogicalStatement, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Cornford\Logical; |
||
| 5 | class LogicalStatement implements LogicalStatementInterface { |
||
| 6 | |||
| 7 | /** |
||
| 8 | * Array of custom logical statements |
||
| 9 | * |
||
| 10 | * @var array |
||
| 11 | */ |
||
| 12 | protected $customStatements = []; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Define a custom logical statement |
||
| 16 | * |
||
| 17 | * @param string $name The statement name |
||
| 18 | * @param callable $callback The callback method to execute |
||
| 19 | * |
||
| 20 | * @return void |
||
| 21 | */ |
||
| 22 | public function defineCustomStatement($name, callable $callback) |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Checks that a custom logical statement exists |
||
| 29 | * |
||
| 30 | * @param string $name The statement name |
||
| 31 | * |
||
| 32 | * @return boolean |
||
| 33 | */ |
||
| 34 | public function customStatementExists($name) |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Returns all custom logical statements |
||
| 45 | * |
||
| 46 | * @return array |
||
| 47 | */ |
||
| 48 | public function getCustomStatements() |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Define a custom logical statement |
||
| 55 | * |
||
| 56 | * @param string $name The statement name |
||
| 57 | * @param string|integer|boolean $input The input value |
||
| 58 | * @param string|integer|boolean $expected The expected type |
||
| 59 | * |
||
| 60 | * @return boolean |
||
| 61 | */ |
||
| 62 | public function callCustomStatement($name, $input, $expected) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * The input value equals the expected value |
||
| 69 | * |
||
| 70 | * @param string $input The input value |
||
| 71 | * @param string $expected The expected value |
||
| 72 | * |
||
| 73 | * @return boolean |
||
| 74 | */ |
||
| 75 | public function equals($input, $expected) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * The input value doesn't equal the expected value |
||
| 86 | * |
||
| 87 | * @param string $input The input value |
||
| 88 | * @param string $expected The expected value |
||
| 89 | * |
||
| 90 | * @return boolean |
||
| 91 | */ |
||
| 92 | public function notEquals($input, $expected) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * The input value has is the same length as the expected value |
||
| 99 | * |
||
| 100 | * @param string $input The input value |
||
| 101 | * @param string $expected The expected value |
||
| 102 | * |
||
| 103 | * @return boolean |
||
| 104 | */ |
||
| 105 | public function isLength($input, $expected) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * The input value isn't the same length as the expected value |
||
| 116 | * |
||
| 117 | * @param string $input The input value |
||
| 118 | * @param string $expected The expected value |
||
| 119 | * |
||
| 120 | * @return boolean |
||
| 121 | */ |
||
| 122 | public function isNotLength($input, $expected) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * The input value is of the expected type |
||
| 129 | * |
||
| 130 | * @param string $input The input value |
||
| 131 | * @param string $expected The expected type |
||
| 132 | * |
||
| 133 | * @return boolean |
||
| 134 | */ |
||
| 135 | public function is($input, $expected) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * The input value doesn't equal the expected value |
||
| 151 | * |
||
| 152 | * @param string $input The input value |
||
| 153 | * @param string $expected The expected value |
||
| 154 | * |
||
| 155 | * @return boolean |
||
| 156 | */ |
||
| 157 | public function isNot($input, $expected) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * The input value contains the expected value |
||
| 164 | * |
||
| 165 | * @param string $input The input value |
||
| 166 | * @param string $expected The expected value |
||
| 167 | * |
||
| 168 | * @return boolean |
||
| 169 | */ |
||
| 170 | public function contains($input, $expected) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * The input value doesn't contain the expected value |
||
| 181 | * |
||
| 182 | * @param string $input The input value |
||
| 183 | * @param string $expected The expected value |
||
| 184 | * |
||
| 185 | * @return boolean |
||
| 186 | */ |
||
| 187 | public function notContains($input, $expected) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * The input value is contained in the expected value array |
||
| 194 | * |
||
| 195 | * @param string $input The input value |
||
| 196 | * @param array $expected The expected value array |
||
| 197 | * |
||
| 198 | * @return boolean |
||
| 199 | */ |
||
| 200 | public function containedIn($input, $expected) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * The input value is not contained in the expected value array |
||
| 213 | * |
||
| 214 | * @param string $input The input value |
||
| 215 | * @param array $expected The expected value array |
||
| 216 | * |
||
| 217 | * @return boolean |
||
| 218 | */ |
||
| 219 | public function notContainedIn($input, $expected) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * The input value is in the expected value array |
||
| 226 | * |
||
| 227 | * @param string $input The input value |
||
| 228 | * @param array $expected The expected value array |
||
| 229 | * |
||
| 230 | * @return boolean |
||
| 231 | */ |
||
| 232 | public function in($input, $expected) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * The input value is not in the expected value array |
||
| 243 | * |
||
| 244 | * @param string $input The input value |
||
| 245 | * @param array $expected The expected value array |
||
| 246 | * |
||
| 247 | * @return boolean |
||
| 248 | */ |
||
| 249 | public function notIn($input, $expected) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * The input value is between expected values |
||
| 256 | * |
||
| 257 | * @param string $input The input value |
||
| 258 | * @param array $expected The expected values |
||
| 259 | * |
||
| 260 | * @return boolean |
||
| 261 | */ |
||
| 262 | public function between($input, $expected) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * The input value is not between the expected values |
||
| 273 | * |
||
| 274 | * @param string $input The input value |
||
| 275 | * @param array $expected The expected values |
||
| 276 | * |
||
| 277 | * @return boolean |
||
| 278 | */ |
||
| 279 | public function notBetween($input, $expected) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * The input value is null |
||
| 286 | * |
||
| 287 | * @param string $input The input value |
||
| 288 | * |
||
| 289 | * @return boolean |
||
| 290 | */ |
||
| 291 | public function null($input) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * The input value is not null |
||
| 302 | * |
||
| 303 | * @param string $input The input value |
||
| 304 | * |
||
| 305 | * @return boolean |
||
| 306 | */ |
||
| 307 | public function notNull($input) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * The input value is less than the expected value |
||
| 314 | * |
||
| 315 | * @param string $input The input value |
||
| 316 | * @param string $expected The expected value |
||
| 317 | * |
||
| 318 | * @return boolean |
||
| 319 | */ |
||
| 320 | public function lessThan($input, $expected) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * The input value isn't less than the expected value |
||
| 331 | * |
||
| 332 | * @param string $input The input value |
||
| 333 | * @param string $expected The expected value |
||
| 334 | * |
||
| 335 | * @return boolean |
||
| 336 | */ |
||
| 337 | public function notLessThan($input, $expected) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * The input value is greater than the expected value |
||
| 344 | * |
||
| 345 | * @param string $input The input value |
||
| 346 | * @param string $expected The expected value |
||
| 347 | * |
||
| 348 | * @return boolean |
||
| 349 | */ |
||
| 350 | public function greaterThan($input, $expected) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * The input value isn't greater than the expected value |
||
| 361 | * |
||
| 362 | * @param string $input The input value |
||
| 363 | * @param string $expected The expected value |
||
| 364 | * |
||
| 365 | * @return boolean |
||
| 366 | */ |
||
| 367 | public function notGreaterThan($input, $expected) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * The input value is less than or equal to the expected value |
||
| 374 | * |
||
| 375 | * @param string $input The input value |
||
| 376 | * @param string $expected The expected value |
||
| 377 | * |
||
| 378 | * @return boolean |
||
| 379 | */ |
||
| 380 | public function lessThanOrEqual($input, $expected) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * The input value isn't less than or equal to the expected value |
||
| 391 | * |
||
| 392 | * @param string $input The input value |
||
| 393 | * @param string $expected The expected value |
||
| 394 | * |
||
| 395 | * @return boolean |
||
| 396 | */ |
||
| 397 | public function notLessThanOrEqual($input, $expected) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * The input value is greater than or equal to the expected value |
||
| 404 | * |
||
| 405 | * @param string $input The input value |
||
| 406 | * @param string $expected The expected value |
||
| 407 | * |
||
| 408 | * @return boolean |
||
| 409 | */ |
||
| 410 | public function greaterThanOrEqual($input, $expected) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * The input value isn't greater than or equal to the expected value |
||
| 421 | * |
||
| 422 | * @param string $input The input value |
||
| 423 | * @param string $expected The expected value |
||
| 424 | * |
||
| 425 | * @return boolean |
||
| 426 | */ |
||
| 427 | public function notGreaterThanOrEqual($input, $expected) |
||
| 431 | |||
| 432 | } |