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 |
||
13 | class SuiteLoader |
||
14 | { |
||
15 | /** |
||
16 | * The collection of loaded files. |
||
17 | * |
||
18 | * @var array |
||
19 | */ |
||
20 | protected $files = []; |
||
21 | |||
22 | /** |
||
23 | * The collection of parsed test classes. |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $loadedSuites = []; |
||
28 | |||
29 | /** |
||
30 | * @var Options |
||
31 | */ |
||
32 | public $options; |
||
33 | |||
34 | 25 | public function __construct(Options $options = null) |
|
35 | { |
||
36 | 25 | $this->options = $options; |
|
37 | 25 | } |
|
38 | |||
39 | /** |
||
40 | * Returns all parsed suite objects as ExecutableTest |
||
41 | * instances. |
||
42 | * |
||
43 | * @return array |
||
44 | */ |
||
45 | 2 | public function getSuites(): array |
|
49 | |||
50 | /** |
||
51 | * Returns a collection of TestMethod objects |
||
52 | * for all loaded ExecutableTest instances. |
||
53 | * |
||
54 | * @return array |
||
55 | */ |
||
56 | 3 | public function getTestMethods(): array |
|
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 | * |
||
72 | * @throws \RuntimeException |
||
73 | */ |
||
74 | 23 | public function load(string $path = '') |
|
109 | |||
110 | /** |
||
111 | * Called after all files are loaded. Parses loaded files into |
||
112 | * ExecutableTest objects - either Suite or TestMethod. |
||
113 | */ |
||
114 | 20 | private function initSuites() |
|
127 | |||
128 | 19 | private function executableTests(string $path, ParsedClass $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 | * |
||
148 | * @return array of MethodBatches. Each MethodBatch has an array of method names |
||
149 | */ |
||
150 | 19 | private function getMethodBatches(ParsedClass $class): array |
|
171 | |||
172 | 1 | private function addDependentTestsToBatchSet(array &$batches, string $dependsOn, array $tests) |
|
183 | |||
184 | 19 | private function addTestsToBatchSet(array &$batches, array $tests, int $maxBatchSize) |
|
197 | |||
198 | /** |
||
199 | * Get method all available tests. |
||
200 | * |
||
201 | * With empty filter this method returns single test if doesnt' have data provider or |
||
202 | * data provider is not used and return all test if has data provider and data provider is used. |
||
203 | * |
||
204 | * @param ParsedClass $class parsed class |
||
205 | * @param ParsedObject $method parsed method |
||
206 | * @param bool $useDataProvider try to use data provider or not |
||
207 | * |
||
208 | * @return string[] array of test names |
||
209 | */ |
||
210 | 19 | private function getMethodTests(ParsedClass $class, ParsedFunction $method, bool $useDataProvider = false): array |
|
238 | |||
239 | 19 | private function testMatchGroupOptions(array $groups): bool |
|
259 | |||
260 | 19 | private function testMatchFilterOptions(string $className, string $name): bool |
|
273 | |||
274 | 19 | private function testMatchOptions(string $className, string $name, array $group): bool |
|
281 | |||
282 | 19 | private function methodDataProvider(ParsedFunction $method) |
|
288 | |||
289 | 19 | private function methodDependency(ParsedFunction $method) |
|
295 | |||
296 | 19 | private function methodGroups(ParsedFunction $method) |
|
304 | |||
305 | 19 | private function createSuite(string $path, ParsedClass $class): Suite |
|
316 | } |
||
317 |
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.