Complex classes like SuiteLoader 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 SuiteLoader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class SuiteLoader |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * The collection of loaded files |
||
| 13 | * |
||
| 14 | * @var array |
||
| 15 | */ |
||
| 16 | protected $files = []; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * The collection of parsed test classes |
||
| 20 | * |
||
| 21 | * @var array |
||
| 22 | */ |
||
| 23 | protected $loadedSuites = []; |
||
| 24 | |||
| 25 | 26 | public function __construct($options = null) |
|
| 33 | |||
| 34 | /** |
||
| 35 | * Returns all parsed suite objects as ExecutableTest |
||
| 36 | * instances |
||
| 37 | * |
||
| 38 | * @return array |
||
| 39 | */ |
||
| 40 | 2 | public function getSuites() |
|
| 44 | |||
| 45 | /** |
||
| 46 | * Returns a collection of TestMethod objects |
||
| 47 | * for all loaded ExecutableTest instances |
||
| 48 | * |
||
| 49 | * @return array |
||
| 50 | */ |
||
| 51 | 3 | public function getTestMethods() |
|
| 60 | |||
| 61 | /** |
||
| 62 | * Populates the loaded suite collection. Will load suites |
||
| 63 | * based off a phpunit xml configuration or a specified path |
||
| 64 | * |
||
| 65 | * @param string $path |
||
| 66 | * @throws \RuntimeException |
||
| 67 | */ |
||
| 68 | 23 | public function load($path = '') |
|
| 104 | |||
| 105 | |||
| 106 | /** |
||
| 107 | * Called after all files are loaded. Parses loaded files into |
||
| 108 | * ExecutableTest objects - either Suite or TestMethod |
||
| 109 | */ |
||
| 110 | 20 | private function initSuites() |
|
| 123 | |||
| 124 | 19 | private function executableTests($path, $class) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Get method batches. |
||
| 137 | * |
||
| 138 | * Identify method dependencies, and group dependents and dependees on a single methodBatch. |
||
| 139 | * Use max batch size to fill batches. |
||
| 140 | * |
||
| 141 | * @param ParsedClass $class |
||
| 142 | * @return array of MethodBatches. Each MethodBatch has an array of method names |
||
| 143 | */ |
||
| 144 | 19 | private function getMethodBatches($class) |
|
| 164 | |||
| 165 | 1 | private function addDependentTestsToBatchSet(&$batches, $dependsOn, $tests) |
|
| 176 | |||
| 177 | 19 | private function addTestsToBatchSet(&$batches, $tests, $maxBatchSize) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Get method all available tests. |
||
| 193 | * |
||
| 194 | * With empty filter this method returns single test if doesnt' have data provider or |
||
| 195 | * data provider is not used and return all test if has data provider and data provider is used. |
||
| 196 | * |
||
| 197 | * @param ParsedClass $class Parsed class. |
||
| 198 | * @param ParsedObject $method Parsed method. |
||
| 199 | * @param bool $useDataProvider Try to use data provider or not. |
||
| 200 | * @return string[] Array of test names. |
||
| 201 | */ |
||
| 202 | 19 | private function getMethodTests($class, $method, $useDataProvider = false) |
|
| 230 | |||
| 231 | 19 | private function testMatchGroupOptions($groups) |
|
| 232 | { |
||
| 233 | 19 | if (empty($groups)) { |
|
| 234 | 17 | return true; |
|
| 235 | } |
||
| 236 | |||
| 237 | 15 | if (!empty($this->options->groups) |
|
| 238 | 1 | && !array_intersect($groups, $this->options->groups) |
|
| 239 | ) { |
||
| 240 | return false; |
||
| 241 | } |
||
| 242 | |||
| 243 | 15 | if (!empty($this->options->excludeGroups) |
|
| 244 | && array_intersect($groups, $this->options->excludeGroups) |
||
| 245 | ) { |
||
| 246 | return false; |
||
| 247 | } |
||
| 248 | |||
| 249 | 15 | return true; |
|
| 250 | } |
||
| 251 | |||
| 252 | 19 | private function testMatchFilterOptions($className, $name, $group) |
|
| 266 | |||
| 267 | 19 | private function testMatchOptions($className, $name, $group) |
|
| 274 | |||
| 275 | 19 | private function methodDataProvider($method) |
|
| 282 | |||
| 283 | 19 | private function methodDependency($method) |
|
| 290 | |||
| 291 | 19 | private function methodGroups($method) |
|
| 298 | |||
| 299 | 19 | private function createSuite($path, ParsedClass $class) |
|
| 310 | } |
||
| 311 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: