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 |
||
10 | class SuiteLoader |
||
11 | { |
||
12 | /** |
||
13 | * The collection of loaded files. |
||
14 | * |
||
15 | * @var array |
||
16 | */ |
||
17 | protected $files = []; |
||
18 | |||
19 | /** |
||
20 | * The collection of parsed test classes. |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $loadedSuites = []; |
||
25 | |||
26 | /** |
||
27 | * @var Options |
||
28 | */ |
||
29 | public $options; |
||
30 | |||
31 | 26 | public function __construct($options = null) |
|
32 | { |
||
33 | 26 | if ($options && !$options instanceof Options) { |
|
34 | 1 | throw new \InvalidArgumentException('SuiteLoader options must be null or of type Options'); |
|
35 | } |
||
36 | |||
37 | 25 | $this->options = $options; |
|
38 | 25 | } |
|
39 | |||
40 | /** |
||
41 | * Returns all parsed suite objects as ExecutableTest |
||
42 | * instances. |
||
43 | * |
||
44 | * @return array |
||
45 | */ |
||
46 | 2 | public function getSuites() |
|
47 | { |
||
48 | 2 | return $this->loadedSuites; |
|
49 | } |
||
50 | |||
51 | /** |
||
52 | * Returns a collection of TestMethod objects |
||
53 | * for all loaded ExecutableTest instances. |
||
54 | * |
||
55 | * @return array |
||
56 | */ |
||
57 | 3 | public function getTestMethods() |
|
58 | { |
||
59 | 3 | $methods = []; |
|
60 | 3 | foreach ($this->loadedSuites as $suite) { |
|
61 | 2 | $methods = array_merge($methods, $suite->getFunctions()); |
|
62 | } |
||
63 | |||
64 | 3 | return $methods; |
|
65 | } |
||
66 | |||
67 | /** |
||
68 | * Populates the loaded suite collection. Will load suites |
||
69 | * based off a phpunit xml configuration or a specified path. |
||
70 | * |
||
71 | * @param string $path |
||
72 | * |
||
73 | * @throws \RuntimeException |
||
74 | */ |
||
75 | 23 | public function load($path = '') |
|
76 | { |
||
77 | 23 | if (is_object($this->options) && isset($this->options->filtered['configuration'])) { |
|
|
|||
78 | 16 | $configuration = $this->options->filtered['configuration']; |
|
79 | } else { |
||
80 | 7 | $configuration = new Configuration(''); |
|
81 | } |
||
82 | |||
83 | 23 | if ($path) { |
|
84 | 9 | $testFileLoader = new TestFileLoader($this->options); |
|
85 | 9 | $this->files = array_merge($this->files, $testFileLoader->loadPath($path)); |
|
86 | 14 | } elseif (isset($this->options->testsuite) && $this->options->testsuite) { |
|
87 | 10 | foreach ($configuration->getSuiteByName($this->options->testsuite) as $suite) { |
|
88 | 10 | foreach ($suite as $suitePath) { |
|
89 | 10 | $testFileLoader = new TestFileLoader($this->options); |
|
90 | 10 | $this->files = array_merge($this->files, $testFileLoader->loadSuitePath($suitePath)); |
|
91 | } |
||
92 | } |
||
93 | 4 | } elseif ($suites = $configuration->getSuites()) { |
|
94 | 2 | foreach ($suites as $suite) { |
|
95 | 2 | foreach ($suite as $suitePath) { |
|
96 | 2 | $testFileLoader = new TestFileLoader($this->options); |
|
97 | 2 | $this->files = array_merge($this->files, $testFileLoader->loadSuitePath($suitePath)); |
|
98 | } |
||
99 | } |
||
100 | } |
||
101 | |||
102 | 21 | if (!$this->files) { |
|
103 | 1 | throw new \RuntimeException('No path or configuration provided (tests must end with Test.php)'); |
|
104 | } |
||
105 | |||
106 | 20 | $this->files = array_unique($this->files); // remove duplicates |
|
107 | |||
108 | 20 | $this->initSuites(); |
|
109 | 20 | } |
|
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) |
|
130 | { |
||
131 | 19 | $executableTests = []; |
|
132 | 19 | $methodBatches = $this->getMethodBatches($class); |
|
140 | |||
141 | /** |
||
142 | * Get method batches. |
||
143 | * |
||
144 | * Identify method dependencies, and group dependents and dependees on a single methodBatch. |
||
145 | * Use max batch size to fill batches. |
||
146 | * |
||
147 | * @param ParsedClass $class |
||
148 | * |
||
149 | * @return array of MethodBatches. Each MethodBatch has an array of method names |
||
150 | */ |
||
151 | 19 | private function getMethodBatches($class) |
|
172 | |||
173 | 1 | private function addDependentTestsToBatchSet(&$batches, $dependsOn, $tests) |
|
184 | |||
185 | 19 | private function addTestsToBatchSet(&$batches, $tests, $maxBatchSize) |
|
198 | |||
199 | /** |
||
200 | * Get method all available tests. |
||
201 | * |
||
202 | * With empty filter this method returns single test if doesnt' have data provider or |
||
203 | * data provider is not used and return all test if has data provider and data provider is used. |
||
204 | * |
||
205 | * @param ParsedClass $class parsed class |
||
206 | * @param ParsedObject $method parsed method |
||
207 | * @param bool $useDataProvider try to use data provider or not |
||
208 | * |
||
209 | * @return string[] array of test names |
||
210 | */ |
||
211 | 19 | private function getMethodTests($class, $method, $useDataProvider = false) |
|
239 | |||
240 | 19 | private function testMatchGroupOptions($groups) |
|
260 | |||
261 | 19 | private function testMatchFilterOptions($className, $name, $group) |
|
275 | |||
276 | 19 | private function testMatchOptions($className, $name, $group) |
|
283 | |||
284 | 19 | private function methodDataProvider($method) |
|
290 | |||
291 | 19 | private function methodDependency($method) |
|
297 | |||
298 | 19 | private function methodGroups($method) |
|
306 | |||
307 | 19 | private function createSuite($path, ParsedClass $class) |
|
318 | } |
||
319 |
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.