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 = array(); |
||
17 | |||
18 | /** |
||
19 | * The collection of parsed test classes |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $loadedSuites = array(); |
||
24 | |||
25 | /** |
||
26 | * @var Options |
||
27 | */ |
||
28 | private $options; |
||
29 | |||
30 | 26 | public function __construct($options = null) |
|
38 | |||
39 | /** |
||
40 | * Returns all parsed suite objects as ExecutableTest |
||
41 | * instances |
||
42 | * |
||
43 | * @return array |
||
44 | */ |
||
45 | 2 | public function getSuites() |
|
49 | |||
50 | /** |
||
51 | * Returns a collection of TestMethod objects |
||
52 | * for all loaded ExecutableTest instances |
||
53 | * |
||
54 | * @return array |
||
55 | */ |
||
56 | 2 | public function getTestMethods() |
|
65 | |||
66 | /** |
||
67 | * Populates the loaded suite collection. Will load suites |
||
68 | * based off a phpunit xml configuration or a specified path |
||
69 | * |
||
70 | * @param string $path |
||
71 | * @throws \RuntimeException |
||
72 | */ |
||
73 | 23 | public function load($path = '') |
|
109 | |||
110 | |||
111 | /** |
||
112 | * Called after all files are loaded. Parses loaded files into |
||
113 | * ExecutableTest objects - either Suite or TestMethod |
||
114 | */ |
||
115 | 20 | private function initSuites() |
|
128 | |||
129 | 19 | private function executableTests($path, $class) |
|
139 | |||
140 | /** |
||
141 | * Get method batches. |
||
142 | * |
||
143 | * Identify method dependencies, and group dependents and dependees on a single methodBatch. |
||
144 | * Use max batch size to fill batches. |
||
145 | * |
||
146 | * @param ParsedClass $class |
||
147 | * @return array of MethodBatches. Each MethodBatch has an array of method names |
||
148 | */ |
||
149 | 19 | private function getMethodBatches($class) |
|
169 | |||
170 | 1 | private function addDependentTestsToBatchSet(&$batches, $dependsOn, $tests) |
|
181 | |||
182 | 19 | private function addTestsToBatchSet(&$batches, $tests, $maxBatchSize) |
|
195 | |||
196 | /** |
||
197 | * Get method all available tests. |
||
198 | * |
||
199 | * With empty filter this method returns single test if doesnt' have data provider or |
||
200 | * data provider is not used and return all test if has data provider and data provider is used. |
||
201 | * |
||
202 | * @param ParsedClass $class Parsed class. |
||
203 | * @param ParsedObject $method Parsed method. |
||
204 | * @param bool $useDataProvider Try to use data provider or not. |
||
205 | * @return string[] Array of test names. |
||
206 | */ |
||
207 | 19 | private function getMethodTests($class, $method, $useDataProvider = false) |
|
235 | |||
236 | 19 | private function testMatchGroupOptions($groups) |
|
256 | |||
257 | 19 | private function testMatchFilterOptions($className, $name, $group) |
|
271 | |||
272 | 19 | private function testMatchOptions($className, $name, $group) |
|
279 | |||
280 | 19 | private function methodDataProvider($method) |
|
287 | |||
288 | 19 | private function methodDependency($method) |
|
295 | |||
296 | 19 | private function methodGroups($method) |
|
303 | |||
304 | 19 | private function createSuite($path, ParsedClass $class) |
|
315 | } |
||
316 |
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.