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 | 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() |
|
| 52 | { |
||
| 53 | 3 | $methods = array(); |
|
| 54 | 3 | foreach ($this->loadedSuites as $suite) { |
|
| 55 | 2 | $methods = array_merge($methods, $suite->getFunctions()); |
|
| 56 | } |
||
| 57 | |||
| 58 | 3 | return $methods; |
|
| 59 | } |
||
| 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 = '') |
|
| 69 | { |
||
| 70 | 23 | if (is_object($this->options) && isset($this->options->filtered['configuration'])) { |
|
| 71 | 16 | $configuration = $this->options->filtered['configuration']; |
|
| 72 | } else { |
||
| 73 | 7 | $configuration = new Configuration(''); |
|
| 74 | } |
||
| 75 | |||
| 76 | 23 | if ($path) { |
|
| 77 | 9 | $testFileLoader = new TestFileLoader($this->options); |
|
| 78 | 9 | $this->files = array_merge($this->files, $testFileLoader->loadPath($path)); |
|
| 79 | 14 | } elseif (isset($this->options->testsuite) && $this->options->testsuite) { |
|
| 80 | 10 | foreach ($configuration->getSuiteByName($this->options->testsuite) as $suite) { |
|
| 81 | 10 | foreach ($suite as $suitePath) { |
|
| 82 | 10 | $testFileLoader = new TestFileLoader($this->options); |
|
| 83 | 10 | $this->files = array_merge($this->files, $testFileLoader->loadSuitePath($suitePath)); |
|
| 84 | } |
||
| 85 | } |
||
| 86 | 4 | } elseif ($suites = $configuration->getSuites()) { |
|
| 87 | 2 | foreach ($suites as $suite) { |
|
| 88 | 2 | foreach ($suite as $suitePath) { |
|
| 89 | 2 | $testFileLoader = new TestFileLoader($this->options); |
|
| 90 | 2 | $this->files = array_merge($this->files, $testFileLoader->loadSuitePath($suitePath)); |
|
| 91 | } |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | |||
| 96 | 21 | if (!$this->files) { |
|
| 97 | 1 | throw new \RuntimeException("No path or configuration provided (tests must end with Test.php)"); |
|
| 98 | } |
||
| 99 | |||
| 100 | 20 | $this->files = array_unique($this->files); // remove duplicates |
|
| 101 | |||
| 102 | 20 | $this->initSuites(); |
|
| 103 | 20 | } |
|
| 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() |
|
| 111 | { |
||
| 112 | 20 | foreach ($this->files as $path) { |
|
| 113 | try { |
||
| 114 | 20 | $parser = new Parser($path); |
|
| 115 | 19 | if ($class = $parser->getClass()) { |
|
| 116 | 19 | $this->loadedSuites[$path] = $this->createSuite($path, $class); |
|
| 117 | } |
||
| 118 | 1 | } catch (NoClassInFileException $e) { |
|
| 119 | 20 | continue; |
|
| 120 | } |
||
| 121 | } |
||
| 122 | 20 | } |
|
| 123 | |||
| 124 | 19 | private function executableTests($path, $class) |
|
| 125 | { |
||
| 126 | 19 | $executableTests = array(); |
|
| 127 | 19 | $methodBatches = $this->getMethodBatches($class); |
|
| 128 | 19 | foreach ($methodBatches as $methodBatch) { |
|
| 129 | 19 | $executableTest = new TestMethod($path, $methodBatch); |
|
| 130 | 19 | $executableTests[] = $executableTest; |
|
| 131 | } |
||
| 132 | 19 | return $executableTests; |
|
| 133 | } |
||
| 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) |
|
| 145 | { |
||
| 146 | 19 | $classMethods = $class->getMethods($this->options ? $this->options->annotations : array()); |
|
| 147 | 19 | $maxBatchSize = $this->options && $this->options->functional ? $this->options->maxBatchSize : 0; |
|
| 148 | 19 | $batches = array(); |
|
| 149 | 19 | foreach ($classMethods as $method) { |
|
| 150 | 19 | $tests = $this->getMethodTests($class, $method, $maxBatchSize != 0); |
|
| 151 | // if filter passed to paratest then method tests can be blank if not match to filter |
||
| 152 | 19 | if (!$tests) { |
|
| 153 | continue; |
||
| 154 | } |
||
| 155 | |||
| 156 | 19 | if (($dependsOn = $this->methodDependency($method)) != null) { |
|
| 157 | 1 | $this->addDependentTestsToBatchSet($batches, $dependsOn, $tests); |
|
| 158 | } else { |
||
| 159 | 19 | $this->addTestsToBatchSet($batches, $tests, $maxBatchSize); |
|
| 160 | } |
||
| 161 | } |
||
| 162 | 19 | return $batches; |
|
| 163 | } |
||
| 164 | |||
| 165 | 1 | private function addDependentTestsToBatchSet(&$batches, $dependsOn, $tests) |
|
| 166 | { |
||
| 167 | 1 | foreach ($batches as $key => $batch) { |
|
| 168 | 1 | foreach ($batch as $methodName) { |
|
| 169 | 1 | if ($dependsOn === $methodName) { |
|
| 170 | 1 | $batches[$key] = array_merge($batches[$key], $tests); |
|
| 171 | 1 | continue; |
|
| 172 | } |
||
| 173 | } |
||
| 174 | } |
||
| 175 | 1 | } |
|
| 176 | |||
| 177 | 19 | private function addTestsToBatchSet(&$batches, $tests, $maxBatchSize) |
|
| 178 | { |
||
| 179 | 19 | foreach ($tests as $test) { |
|
| 180 | 19 | $lastIndex = count($batches) - 1; |
|
| 181 | 19 | if ($lastIndex != -1 |
|
| 182 | 19 | && count($batches[$lastIndex]) < $maxBatchSize |
|
| 183 | ) { |
||
| 184 | $batches[$lastIndex][] = $test; |
||
| 185 | } else { |
||
| 186 | 19 | $batches[] = array($test); |
|
| 187 | } |
||
| 188 | } |
||
| 189 | 19 | } |
|
| 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) |
|
| 203 | { |
||
| 204 | 19 | $result = array(); |
|
| 205 | |||
| 206 | 19 | $groups = $this->methodGroups($method); |
|
| 207 | |||
| 208 | 19 | $dataProvider = $this->methodDataProvider($method); |
|
| 209 | 19 | if ($useDataProvider && isset($dataProvider)) { |
|
| 210 | $testFullClassName = "\\" . $class->getName(); |
||
| 211 | $testClass = new $testFullClassName(); |
||
| 212 | $result = array(); |
||
| 213 | $datasetKeys = array_keys($testClass->$dataProvider()); |
||
| 214 | foreach ($datasetKeys as $key) { |
||
| 215 | $test = sprintf( |
||
| 216 | "%s with data set %s", |
||
| 217 | $method->getName(), |
||
| 218 | is_int($key) ? "#" . $key : "\"" . $key . "\"" |
||
| 219 | ); |
||
| 220 | if ($this->testMatchOptions($class->getName(), $test, $groups)) { |
||
| 221 | $result[] = $test; |
||
| 222 | } |
||
| 223 | } |
||
| 224 | 19 | } elseif ($this->testMatchOptions($class->getName(), $method->getName(), $groups)) { |
|
| 225 | 19 | $result = array($method->getName()); |
|
| 226 | } |
||
| 227 | |||
| 228 | 19 | return $result; |
|
| 229 | } |
||
| 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 | 15 | && !array_intersect($groups, $this->options->groups) |
|
| 239 | ) { |
||
| 240 | return false; |
||
| 241 | } |
||
| 242 | |||
| 243 | 15 | if (!empty($this->options->excludeGroups) |
|
| 244 | 15 | && 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) |
|
| 300 | { |
||
| 301 | 19 | return new Suite( |
|
| 302 | $path, |
||
| 303 | 19 | $this->executableTests( |
|
| 304 | $path, |
||
| 305 | $class |
||
| 306 | ), |
||
| 307 | 19 | $class->getName() |
|
| 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: