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 pattern used for grabbing test files. Uses the *Test.php convention |
||
13 | * that PHPUnit defaults to. |
||
14 | */ |
||
15 | const TEST_PATTERN = '/.+Test\.php$/'; |
||
16 | |||
17 | /** |
||
18 | * Matches php files |
||
19 | */ |
||
20 | const FILE_PATTERN = '/.+\.php$/'; |
||
21 | |||
22 | /** |
||
23 | * The collection of loaded files |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $files = array(); |
||
28 | |||
29 | /** |
||
30 | * The collection of parsed test classes |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $loadedSuites = array(); |
||
35 | |||
36 | /** |
||
37 | * Used to ignore directory paths '.' and '..' |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | private static $dotPattern = '/([.]+)$/'; |
||
42 | |||
43 | 23 | public function __construct($options = null) |
|
51 | |||
52 | /** |
||
53 | * Returns all parsed suite objects as ExecutableTest |
||
54 | * instances |
||
55 | * |
||
56 | * @return array |
||
57 | */ |
||
58 | 2 | public function getSuites() |
|
62 | |||
63 | /** |
||
64 | * Returns a collection of TestMethod objects |
||
65 | * for all loaded ExecutableTest instances |
||
66 | * |
||
67 | * @return array |
||
68 | */ |
||
69 | 1 | public function getTestMethods() |
|
78 | |||
79 | /** |
||
80 | * Populates the loaded suite collection. Will load suites |
||
81 | * based off a phpunit xml configuration or a specified path |
||
82 | * |
||
83 | * @param string $path |
||
84 | * @throws \RuntimeException |
||
85 | */ |
||
86 | 20 | public function load($path = '') |
|
121 | |||
122 | /** |
||
123 | * Loads suites based on a specific path. |
||
124 | * A valid path can be a directory or file |
||
125 | * |
||
126 | * @param $path |
||
127 | * @throws \InvalidArgumentException |
||
128 | */ |
||
129 | 12 | private function loadPath($path) |
|
147 | |||
148 | /** |
||
149 | * Loads suites from a directory |
||
150 | * |
||
151 | * @param string $path |
||
152 | * @param string $pattern |
||
153 | */ |
||
154 | 9 | private function loadDir($path, $pattern = self::TEST_PATTERN) |
|
161 | |||
162 | /** |
||
163 | * Load a single suite file |
||
164 | * |
||
165 | * @param $path |
||
166 | */ |
||
167 | 6 | private function loadFile($path) |
|
171 | |||
172 | /** |
||
173 | * Attempts to load suites from a path. |
||
174 | * |
||
175 | * @param string $path |
||
176 | * @param string $pattern regular expression for matching file names |
||
177 | */ |
||
178 | 12 | private function tryLoadTests($path, $pattern = self::TEST_PATTERN) |
|
188 | |||
189 | /** |
||
190 | * Called after all files are loaded. Parses loaded files into |
||
191 | * ExecutableTest objects - either Suite or TestMethod |
||
192 | */ |
||
193 | 12 | private function initSuites() |
|
209 | |||
210 | 12 | private function executableTests($path, $class) |
|
220 | |||
221 | /** |
||
222 | * Get method batches. |
||
223 | * |
||
224 | * Identify method dependencies, and group dependents and dependees on a single methodBatch. |
||
225 | * Use max batch size to fill batches. |
||
226 | * |
||
227 | * @param ParsedClass $class |
||
228 | * @return array of MethodBatches. Each MethodBatch has an array of method names |
||
229 | */ |
||
230 | 12 | private function getMethodBatches(ParsedClass $class) |
|
253 | |||
254 | private function addDependentTestsToBatchSet(&$batches, $dependsOn, $tests) |
||
265 | |||
266 | 12 | private function addTestsToBatchSet(&$batches, $tests, $maxBatchSize) |
|
279 | |||
280 | /** |
||
281 | * Get method all available tests. |
||
282 | * |
||
283 | * With empty filter this method returns single test if doesnt' have data provider or |
||
284 | * data provider is not used and return all test if has data provider and data provider is used. |
||
285 | * |
||
286 | * @param ParsedClass $class Parsed class. |
||
287 | * @param array $classGroups Groups on the class. |
||
288 | * @param ParsedObject $method Parsed method. |
||
289 | * @param bool $useDataProvider Try to use data provider or not. |
||
290 | * @return string[] Array of test names. |
||
291 | */ |
||
292 | 12 | private function getMethodTests(ParsedClass $class, array $classGroups, ParsedObject $method, $useDataProvider = false) |
|
320 | |||
321 | 12 | private function testMatchGroupOptions($groups) |
|
341 | |||
342 | 12 | private function testMatchFilterOptions($className, $name, $group) |
|
356 | |||
357 | 12 | private function testMatchOptions($className, $name, $group) |
|
364 | |||
365 | 12 | private function methodDataProvider($method) |
|
372 | |||
373 | 12 | private function methodDependency($method) |
|
380 | |||
381 | 12 | private function classGroups(ParsedClass $class) |
|
385 | |||
386 | 12 | private function methodGroups(ParsedObject $method) |
|
390 | |||
391 | 12 | private function docBlockGroups($docBlock) |
|
398 | |||
399 | 12 | private function createSuite($path, ParsedClass $class) |
|
412 | } |
||
413 |
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: