Complex classes like ClassChecker 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 ClassChecker, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class ClassChecker extends AbstractChecker |
||
| 18 | { |
||
| 19 | |||
| 20 | const CACHE_DURATION = 3600; |
||
| 21 | |||
| 22 | const TYPE_CLASS_DELETED = 'class.deleted'; |
||
| 23 | const TYPE_CLASS_MADE_ABSTRACT = 'class.made_abstract'; |
||
| 24 | const TYPE_CLASS_MADE_FINAL = 'class.made_final'; |
||
| 25 | const TYPE_CLASS_CONSTANT_DELETED = 'class.constant.deleted'; |
||
| 26 | const TYPE_PROPERTY_DELETED = 'property.deleted'; |
||
| 27 | const TYPE_PROPERTY_SCOPE_REDUCED = 'property.scope_reduced'; |
||
| 28 | const TYPE_METHOD_DELETED = 'method.deleted'; |
||
| 29 | const TYPE_METHOD_MADE_ABSTRACT = 'method.made_abstract'; |
||
| 30 | const TYPE_METHOD_MADE_FINAL = 'method.made_final'; |
||
| 31 | const TYPE_METHOD_SCOPE_REDUCED = 'method.scope_reduced'; |
||
| 32 | const TYPE_METHOD_SIGNATURE_CHANGED = 'method.signature_changed'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Source class data. |
||
| 36 | * |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $sourceClassData = array(); |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Target class data. |
||
| 43 | * |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | protected $targetClassData = array(); |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Source property data. |
||
| 50 | * |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | protected $sourcePropertyData = array(); |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Target property data. |
||
| 57 | * |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | protected $targetPropertyData = array(); |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Source method data. |
||
| 64 | * |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | protected $sourceMethodData = array(); |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Target method data. |
||
| 71 | * |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | protected $targetMethodData = array(); |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Returns backwards compatibility checker name. |
||
| 78 | * |
||
| 79 | * @return string |
||
| 80 | */ |
||
| 81 | public function getName() |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Collects backwards compatibility violations. |
||
| 88 | * |
||
| 89 | * @return void |
||
| 90 | */ |
||
| 91 | 3 | protected function doCheck() |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Checks constants. |
||
| 123 | * |
||
| 124 | * @return void |
||
| 125 | */ |
||
| 126 | 3 | protected function processConstants() |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Returns class constants. |
||
| 152 | * |
||
| 153 | * @param ExtendedPdoInterface $db Database. |
||
| 154 | * @param integer $class_id Class ID. |
||
| 155 | * |
||
| 156 | * @return array |
||
| 157 | */ |
||
| 158 | 3 | protected function getConstantsRecursively(ExtendedPdoInterface $db, $class_id) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Checks properties. |
||
| 188 | * |
||
| 189 | * @return void |
||
| 190 | */ |
||
| 191 | 3 | protected function processProperties() |
|
| 192 | { |
||
| 193 | 3 | $class_name = $this->sourceClassData['Name']; |
|
| 194 | 3 | $source_properties = $this->getPropertiesRecursively( |
|
| 195 | 3 | $this->sourceDatabase, |
|
| 196 | 3 | $this->sourceClassData['Id'], |
|
| 197 | 3 | $this->coveredScopes() |
|
| 198 | 3 | ); |
|
| 199 | 3 | $target_properties = $this->getPropertiesRecursively($this->targetDatabase, $this->targetClassData['Id'], ''); |
|
| 200 | |||
| 201 | 3 | foreach ( $source_properties as $source_property_name => $source_property_data ) { |
|
| 202 | 3 | $full_property_name = $class_name . '::$' . $source_property_name; |
|
| 203 | |||
| 204 | // Report incidents for processed (not inherited) properties only. |
||
| 205 | 3 | if ( $source_property_data['ClassId'] !== $this->sourceClassData['Id'] ) { |
|
| 206 | continue; |
||
| 207 | } |
||
| 208 | |||
| 209 | 3 | if ( !isset($target_properties[$source_property_name]) ) { |
|
| 210 | 1 | $this->addIncident(self::TYPE_PROPERTY_DELETED, $full_property_name); |
|
| 211 | 1 | continue; |
|
| 212 | } |
||
| 213 | |||
| 214 | 3 | $this->sourcePropertyData = $source_property_data; |
|
| 215 | 3 | $this->targetPropertyData = $target_properties[$source_property_name]; |
|
| 216 | |||
| 217 | 3 | $this->processProperty(); |
|
| 218 | 3 | } |
|
| 219 | 3 | } |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Returns class properties. |
||
| 223 | * |
||
| 224 | * @param ExtendedPdoInterface $db Database. |
||
| 225 | * @param integer $class_id Class ID. |
||
| 226 | * @param string $scopes Scopes. |
||
| 227 | * |
||
| 228 | * @return array |
||
| 229 | */ |
||
| 230 | 3 | protected function getPropertiesRecursively(ExtendedPdoInterface $db, $class_id, $scopes) |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Processes property. |
||
| 263 | * |
||
| 264 | * @return void |
||
| 265 | */ |
||
| 266 | 3 | protected function processProperty() |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Checks methods. |
||
| 285 | * |
||
| 286 | * @return void |
||
| 287 | */ |
||
| 288 | 3 | protected function processMethods() |
|
| 289 | { |
||
| 290 | 3 | $class_name = $this->sourceClassData['Name']; |
|
| 291 | 3 | $source_methods = $this->getMethodsRecursively( |
|
| 292 | 3 | $this->sourceDatabase, |
|
| 293 | 3 | $this->sourceClassData['Id'], |
|
| 294 | 3 | $this->coveredScopes() |
|
| 295 | 3 | ); |
|
| 296 | 3 | $target_methods = $this->getMethodsRecursively($this->targetDatabase, $this->targetClassData['Id'], ''); |
|
| 297 | |||
| 298 | 3 | foreach ( $source_methods as $source_method_name => $source_method_data ) { |
|
| 299 | 3 | $target_method_name = $source_method_name; |
|
| 300 | 3 | $full_method_name = $class_name . '::' . $source_method_name; |
|
| 301 | |||
| 302 | // Ignore PHP4 constructor rename into PHP5 constructor. |
||
| 303 | 3 | if ( !isset($target_methods[$target_method_name]) && $target_method_name === $class_name ) { |
|
| 304 | 1 | $target_method_name = '__construct'; |
|
| 305 | 1 | } |
|
| 306 | |||
| 307 | // Report incidents for processed (not inherited) methods only. |
||
| 308 | 3 | if ( $source_method_data['ClassId'] !== $this->sourceClassData['Id'] ) { |
|
| 309 | continue; |
||
| 310 | } |
||
| 311 | |||
| 312 | 3 | if ( !isset($target_methods[$target_method_name]) ) { |
|
| 313 | 1 | $this->addIncident(self::TYPE_METHOD_DELETED, $full_method_name); |
|
| 314 | 1 | continue; |
|
| 315 | } |
||
| 316 | |||
| 317 | 3 | $this->sourceMethodData = $source_method_data; |
|
| 318 | 3 | $this->sourceMethodData['ParameterSignature'] = $this->getMethodParameterSignature( |
|
| 319 | 3 | $this->sourceDatabase, |
|
| 320 | 3 | $this->sourceMethodData['Id'] |
|
| 321 | 3 | ); |
|
| 322 | |||
| 323 | 3 | $this->targetMethodData = $target_methods[$target_method_name]; |
|
| 324 | 3 | $this->targetMethodData['ParameterSignature'] = $this->getMethodParameterSignature( |
|
| 325 | 3 | $this->targetDatabase, |
|
| 326 | 3 | $this->targetMethodData['Id'] |
|
| 327 | 3 | ); |
|
| 328 | |||
| 329 | 3 | $this->processMethod(); |
|
| 330 | 3 | } |
|
| 331 | 3 | } |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Returns class methods. |
||
| 335 | * |
||
| 336 | * @param ExtendedPdoInterface $db Database. |
||
| 337 | * @param integer $class_id Class ID. |
||
| 338 | * @param string $scopes Scopes. |
||
| 339 | * |
||
| 340 | * @return array |
||
| 341 | */ |
||
| 342 | 3 | protected function getMethodsRecursively(ExtendedPdoInterface $db, $class_id, $scopes) |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Calculates method parameter signature. |
||
| 375 | * |
||
| 376 | * @param ExtendedPdoInterface $db Database. |
||
| 377 | * @param integer $method_id Method ID. |
||
| 378 | * |
||
| 379 | * @return integer |
||
| 380 | */ |
||
| 381 | 3 | protected function getMethodParameterSignature(ExtendedPdoInterface $db, $method_id) |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Processes method. |
||
| 400 | * |
||
| 401 | * @return void |
||
| 402 | */ |
||
| 403 | 3 | protected function processMethod() |
|
| 404 | { |
||
| 405 | 3 | $class_name = $this->sourceClassData['Name']; |
|
| 406 | 3 | $method_name = $this->sourceMethodData['Name']; |
|
| 407 | |||
| 408 | 3 | $full_method_name = $class_name . '::' . $method_name; |
|
| 409 | |||
| 410 | 3 | if ( !$this->sourceMethodData['IsAbstract'] && $this->targetMethodData['IsAbstract'] ) { |
|
| 411 | 1 | $this->addIncident(self::TYPE_METHOD_MADE_ABSTRACT, $full_method_name); |
|
| 412 | 1 | } |
|
| 413 | |||
| 414 | 3 | if ( !$this->sourceMethodData['IsFinal'] && $this->targetMethodData['IsFinal'] ) { |
|
| 415 | 1 | $this->addIncident(self::TYPE_METHOD_MADE_FINAL, $full_method_name); |
|
| 416 | 1 | } |
|
| 417 | |||
| 418 | 3 | if ( $this->sourceMethodData['ParameterSignature'] !== $this->targetMethodData['ParameterSignature'] ) { |
|
| 419 | 1 | $this->addIncident( |
|
| 420 | 1 | self::TYPE_METHOD_SIGNATURE_CHANGED, |
|
| 421 | 1 | $full_method_name, |
|
| 422 | 1 | $this->sourceMethodData['ParameterSignature'], |
|
| 423 | 1 | $this->targetMethodData['ParameterSignature'] |
|
| 424 | 1 | ); |
|
| 425 | 1 | } |
|
| 426 | |||
| 427 | 3 | if ( $this->sourceMethodData['Scope'] > $this->targetMethodData['Scope'] ) { |
|
| 428 | 1 | $this->addIncident( |
|
| 429 | 1 | self::TYPE_METHOD_SCOPE_REDUCED, |
|
| 430 | 1 | $full_method_name, |
|
| 431 | 1 | $this->getScopeName($this->sourceMethodData['Scope']), |
|
| 432 | 1 | $this->getScopeName($this->targetMethodData['Scope']) |
|
| 433 | 1 | ); |
|
| 434 | 1 | } |
|
| 435 | 3 | } |
|
| 436 | |||
| 437 | /** |
||
| 438 | * Returns scope name. |
||
| 439 | * |
||
| 440 | * @param integer $scope Scope. |
||
| 441 | * |
||
| 442 | * @return string |
||
| 443 | */ |
||
| 444 | 1 | protected function getScopeName($scope) |
|
| 454 | |||
| 455 | /** |
||
| 456 | * Scopes covered by backwards compatibility checks. |
||
| 457 | * |
||
| 458 | * @return string |
||
| 459 | */ |
||
| 460 | 3 | protected function coveredScopes() |
|
| 464 | |||
| 465 | /** |
||
| 466 | * Returns class constants. |
||
| 467 | * |
||
| 468 | * @param ExtendedPdoInterface $db Database. |
||
| 469 | * @param integer $class_id Class ID. |
||
| 470 | * |
||
| 471 | * @return array |
||
| 472 | */ |
||
| 473 | 3 | protected function getClassRelations(ExtendedPdoInterface $db, $class_id) |
|
| 490 | |||
| 491 | } |
||
| 492 |