Complex classes like File 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 File, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class File extends AbstractNode |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var array |
||
| 22 | */ |
||
| 23 | private $coverageData; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | private $testData; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var int |
||
| 32 | */ |
||
| 33 | private $numExecutableLines = 0; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var int |
||
| 37 | */ |
||
| 38 | private $numExecutedLines = 0; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | private $classes = []; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | private $traits = []; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | private $functions = []; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | private $linesOfCode = []; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var int |
||
| 62 | */ |
||
| 63 | private $numClasses = null; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var int |
||
| 67 | */ |
||
| 68 | private $numTestedClasses = 0; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var int |
||
| 72 | */ |
||
| 73 | private $numTraits = null; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var int |
||
| 77 | */ |
||
| 78 | private $numTestedTraits = 0; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var int |
||
| 82 | */ |
||
| 83 | private $numMethods = null; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var int |
||
| 87 | */ |
||
| 88 | private $numTestedMethods = null; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var int |
||
| 92 | */ |
||
| 93 | private $numTestedFunctions = null; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var array |
||
| 97 | */ |
||
| 98 | private $startLines = []; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var array |
||
| 102 | */ |
||
| 103 | private $endLines = []; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var bool |
||
| 107 | */ |
||
| 108 | private $cacheTokens; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Constructor. |
||
| 112 | * |
||
| 113 | * @param string $name |
||
| 114 | * @param AbstractNode $parent |
||
| 115 | * @param array $coverageData |
||
| 116 | * @param array $testData |
||
| 117 | * @param bool $cacheTokens |
||
| 118 | * |
||
| 119 | * @throws InvalidArgumentException |
||
| 120 | */ |
||
| 121 | public function __construct($name, AbstractNode $parent, array $coverageData, array $testData, $cacheTokens) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Returns the number of files in/under this node. |
||
| 141 | * |
||
| 142 | * @return int |
||
| 143 | */ |
||
| 144 | public function count() |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Returns the code coverage data of this node. |
||
| 151 | * |
||
| 152 | * @return array |
||
| 153 | */ |
||
| 154 | public function getCoverageData() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Returns the test data of this node. |
||
| 161 | * |
||
| 162 | * @return array |
||
| 163 | */ |
||
| 164 | public function getTestData() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Returns the classes of this node. |
||
| 171 | * |
||
| 172 | * @return array |
||
| 173 | */ |
||
| 174 | public function getClasses() |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Returns the traits of this node. |
||
| 181 | * |
||
| 182 | * @return array |
||
| 183 | */ |
||
| 184 | public function getTraits() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Returns the functions of this node. |
||
| 191 | * |
||
| 192 | * @return array |
||
| 193 | */ |
||
| 194 | public function getFunctions() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Returns the LOC/CLOC/NCLOC of this node. |
||
| 201 | * |
||
| 202 | * @return array |
||
| 203 | */ |
||
| 204 | public function getLinesOfCode() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Returns the number of executable lines. |
||
| 211 | * |
||
| 212 | * @return int |
||
| 213 | */ |
||
| 214 | public function getNumExecutableLines() |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Returns the number of executed lines. |
||
| 221 | * |
||
| 222 | * @return int |
||
| 223 | */ |
||
| 224 | public function getNumExecutedLines() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Returns the number of classes. |
||
| 231 | * |
||
| 232 | * @return int |
||
| 233 | */ |
||
| 234 | public function getNumClasses() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Returns the number of tested classes. |
||
| 255 | * |
||
| 256 | * @return int |
||
| 257 | */ |
||
| 258 | public function getNumTestedClasses() |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Returns the number of traits. |
||
| 265 | * |
||
| 266 | * @return int |
||
| 267 | */ |
||
| 268 | public function getNumTraits() |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Returns the number of tested traits. |
||
| 289 | * |
||
| 290 | * @return int |
||
| 291 | */ |
||
| 292 | public function getNumTestedTraits() |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Returns the number of methods. |
||
| 299 | * |
||
| 300 | * @return int |
||
| 301 | */ |
||
| 302 | public function getNumMethods() |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Returns the number of tested methods. |
||
| 329 | * |
||
| 330 | * @return int |
||
| 331 | */ |
||
| 332 | public function getNumTestedMethods() |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Returns the number of functions. |
||
| 361 | * |
||
| 362 | * @return int |
||
| 363 | */ |
||
| 364 | public function getNumFunctions() |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Returns the number of tested functions. |
||
| 371 | * |
||
| 372 | * @return int |
||
| 373 | */ |
||
| 374 | public function getNumTestedFunctions() |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Calculates coverage statistics for the file. |
||
| 392 | */ |
||
| 393 | protected function calculateStatistics() |
||
| 573 | |||
| 574 | /** |
||
| 575 | * @param \PHP_Token_Stream $tokens |
||
| 576 | */ |
||
| 577 | protected function processClasses(\PHP_Token_Stream $tokens) |
||
| 609 | |||
| 610 | /** |
||
| 611 | * @param \PHP_Token_Stream $tokens |
||
| 612 | */ |
||
| 613 | protected function processTraits(\PHP_Token_Stream $tokens) |
||
| 645 | |||
| 646 | /** |
||
| 647 | * @param \PHP_Token_Stream $tokens |
||
| 648 | */ |
||
| 649 | protected function processFunctions(\PHP_Token_Stream $tokens) |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Calculates the Change Risk Anti-Patterns (CRAP) index for a unit of code |
||
| 676 | * based on its cyclomatic complexity and percentage of code coverage. |
||
| 677 | * |
||
| 678 | * @param int $ccn |
||
| 679 | * @param float $coverage |
||
| 680 | * |
||
| 681 | * @return string |
||
| 682 | */ |
||
| 683 | protected function crap($ccn, $coverage) |
||
| 698 | |||
| 699 | /** |
||
| 700 | * @param string $methodName |
||
| 701 | * @param array $method |
||
| 702 | * @param string $link |
||
| 703 | * |
||
| 704 | * @return array |
||
| 705 | */ |
||
| 706 | private function newMethod($methodName, array $method, $link) |
||
| 722 | } |
||
| 723 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.