Complex classes like AbstractSniffUnitTest 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 AbstractSniffUnitTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | abstract class AbstractSniffUnitTest extends PHPUnit_Framework_TestCase |
||
| 33 | { |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The PHP_CodeSniffer object used for testing. |
||
| 37 | * |
||
| 38 | * @var PHP_CodeSniffer |
||
| 39 | */ |
||
| 40 | protected static $phpcs = null; |
||
| 41 | |||
| 42 | |||
| 43 | /** |
||
| 44 | * Sets up this unit test. |
||
| 45 | * |
||
| 46 | * @return void |
||
| 47 | */ |
||
| 48 | protected function setUp() |
||
| 49 | { |
||
| 50 | if (self::$phpcs === null) { |
||
| 51 | self::$phpcs = new PHP_CodeSniffer(); |
||
| 52 | |||
| 53 | // Conflicts with Composer AutoLoader. |
||
| 54 | spl_autoload_unregister(array('PHP_CodeSniffer', 'autoload')); |
||
| 55 | } |
||
| 56 | |||
| 57 | }//end setUp() |
||
| 58 | |||
| 59 | |||
| 60 | /** |
||
| 61 | * Tests the extending classes Sniff class. |
||
| 62 | * |
||
| 63 | * @return void |
||
| 64 | */ |
||
| 65 | public final function testStandard() |
||
|
|
|||
| 66 | { |
||
| 67 | // Skip this test if we can't run in this environment. |
||
| 68 | if ($this->shouldSkipTest() === true) { |
||
| 69 | $this->markTestSkipped(); |
||
| 70 | } |
||
| 71 | |||
| 72 | if (method_exists(self::$phpcs, 'initStandard') === true) { |
||
| 73 | // Only init standard without processing it's files, when possible. |
||
| 74 | self::$phpcs->initStandard($this->getStandardName(), array($this->getSniffCode())); |
||
| 75 | } else { |
||
| 76 | // Init standard and process all it's files. |
||
| 77 | self::$phpcs->process(array(), $this->getStandardName(), array($this->getSniffCode())); |
||
| 78 | } |
||
| 79 | |||
| 80 | self::$phpcs->setIgnorePatterns(array()); |
||
| 81 | |||
| 82 | $failureMessages = array(); |
||
| 83 | $fixingSupported = $this->isFixingSupported(); |
||
| 84 | foreach ($this->_getTestFiles() as $testFile) { |
||
| 85 | try { |
||
| 86 | $phpcsFile = self::$phpcs->processFile($testFile); |
||
| 87 | $failureMessages = array_merge($failureMessages, $this->generateFailureMessages($phpcsFile)); |
||
| 88 | |||
| 89 | if ($fixingSupported === true && $phpcsFile->getFixableCount() > 0) { |
||
| 90 | // Attempt to fix the errors. |
||
| 91 | $phpcsFile->fixer->fixFile(); |
||
| 92 | $fixable = $phpcsFile->getFixableCount(); |
||
| 93 | if ($fixable > 0) { |
||
| 94 | $filename = basename($testFile); |
||
| 95 | $failureMessages[] = "Failed to fix $fixable fixable violations in $filename."; |
||
| 96 | } else { |
||
| 97 | // Check for a .fixed file to check for accuracy of fixes. |
||
| 98 | $fixedFile = $testFile.'.fixed'; |
||
| 99 | if (file_exists($fixedFile) === true) { |
||
| 100 | $diff = $phpcsFile->fixer->generateDiff($fixedFile); |
||
| 101 | if (trim($diff) !== '') { |
||
| 102 | $filename = basename($testFile); |
||
| 103 | $fixedFilename = basename($fixedFile); |
||
| 104 | $failureMessages[] = "Fixed version of $filename does not match expected version in $fixedFilename; the diff is\n$diff"; |
||
| 105 | } |
||
| 106 | } |
||
| 107 | } |
||
| 108 | } |
||
| 109 | } catch (Exception $e) { |
||
| 110 | $this->fail('An unexpected exception has been caught: '.$e->getMessage()); |
||
| 111 | }//end try |
||
| 112 | }//end foreach |
||
| 113 | |||
| 114 | if (empty($failureMessages) === false) { |
||
| 115 | $this->fail(implode(PHP_EOL, $failureMessages)); |
||
| 116 | } |
||
| 117 | |||
| 118 | }//end testStandard() |
||
| 119 | |||
| 120 | |||
| 121 | /** |
||
| 122 | * Determines if used PHPCS version supports fixing. |
||
| 123 | * |
||
| 124 | * @return bool |
||
| 125 | */ |
||
| 126 | protected function isFixingSupported() |
||
| 127 | { |
||
| 128 | $classReflection = new \ReflectionClass('PHP_CodeSniffer_File'); |
||
| 129 | |||
| 130 | return $classReflection->hasProperty('fixer'); |
||
| 131 | |||
| 132 | }//end isFixingSupported() |
||
| 133 | |||
| 134 | |||
| 135 | /** |
||
| 136 | * Should this test be skipped for some reason. |
||
| 137 | * |
||
| 138 | * @return bool |
||
| 139 | */ |
||
| 140 | protected function shouldSkipTest() |
||
| 141 | { |
||
| 142 | return false; |
||
| 143 | |||
| 144 | }//end shouldSkipTest() |
||
| 145 | |||
| 146 | |||
| 147 | /** |
||
| 148 | * The name of the coding standard we are testing. |
||
| 149 | * |
||
| 150 | * @return string |
||
| 151 | */ |
||
| 152 | protected function getStandardName() |
||
| 153 | { |
||
| 154 | $basename = $this->getBaseName(); |
||
| 155 | |||
| 156 | return STANDARDS_PATH.DIRECTORY_SEPARATOR.substr($basename, 0, strpos($basename, '_')); |
||
| 157 | |||
| 158 | }//end getStandardName() |
||
| 159 | |||
| 160 | |||
| 161 | /** |
||
| 162 | * The code of the sniff we are testing. |
||
| 163 | * |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | protected function getSniffCode() |
||
| 167 | { |
||
| 168 | $parts = explode('_', $this->getBaseName()); |
||
| 169 | |||
| 170 | return $parts[0].'.'.$parts[2].'.'.$parts[3]; |
||
| 171 | |||
| 172 | }//end getSniffCode() |
||
| 173 | |||
| 174 | |||
| 175 | /** |
||
| 176 | * Get a list of all test files to check. These will have the same base name |
||
| 177 | * but different extensions. We ignore the .php file as it is the class. |
||
| 178 | * |
||
| 179 | * @return array |
||
| 180 | */ |
||
| 181 | private function _getTestFiles() |
||
| 182 | { |
||
| 183 | // The basis for determining file locations. |
||
| 184 | $basename = $this->getBaseName(); |
||
| 185 | |||
| 186 | // The name of the dummy file we are testing. |
||
| 187 | $testFileBase = STANDARDS_PATH.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $basename).'UnitTest.'; |
||
| 188 | |||
| 189 | $testFiles = array(); |
||
| 190 | |||
| 191 | /** @var SplFileInfo $file */ |
||
| 192 | $directoryIterator = new DirectoryIterator(dirname($testFileBase)); |
||
| 193 | |||
| 194 | foreach ($directoryIterator as $file) { |
||
| 195 | $path = $file->getPathname(); |
||
| 196 | |||
| 197 | if (substr($path, 0, strlen($testFileBase)) === $testFileBase && $path !== $testFileBase.'php' && $file->getExtension() !== 'fixed') { |
||
| 198 | $testFiles[] = $path; |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | // Get them in order. |
||
| 203 | sort($testFiles); |
||
| 204 | |||
| 205 | return $testFiles; |
||
| 206 | |||
| 207 | }//end _getTestFiles() |
||
| 208 | |||
| 209 | |||
| 210 | /** |
||
| 211 | * The basis for determining file locations. |
||
| 212 | * |
||
| 213 | * @return string |
||
| 214 | */ |
||
| 215 | protected function getBaseName() |
||
| 216 | { |
||
| 217 | return substr(get_class($this), 0, -8); |
||
| 218 | |||
| 219 | }//end getBaseName() |
||
| 220 | |||
| 221 | |||
| 222 | /** |
||
| 223 | * Generate a list of test failures for a given sniffed file. |
||
| 224 | * |
||
| 225 | * @param PHP_CodeSniffer_File $file The file being tested. |
||
| 226 | * |
||
| 227 | * @return array |
||
| 228 | * @throws PHP_CodeSniffer_Exception |
||
| 229 | */ |
||
| 230 | public function generateFailureMessages(PHP_CodeSniffer_File $file) |
||
| 410 | |||
| 411 | |||
| 412 | /** |
||
| 413 | * Returns the lines where errors should occur. |
||
| 414 | * |
||
| 415 | * The key of the array should represent the line number and the value |
||
| 416 | * should represent the number of errors that should occur on that line. |
||
| 417 | * |
||
| 418 | * @param string $testFile Name of the file with test data. |
||
| 419 | * |
||
| 420 | * @return array(int => int) |
||
| 421 | */ |
||
| 422 | protected abstract function getErrorList($testFile); |
||
| 423 | |||
| 424 | |||
| 425 | /** |
||
| 426 | * Returns the lines where warnings should occur. |
||
| 427 | * |
||
| 428 | * The key of the array should represent the line number and the value |
||
| 429 | * should represent the number of warnings that should occur on that line. |
||
| 430 | * |
||
| 431 | * @param string $testFile Name of the file with test data. |
||
| 432 | * |
||
| 433 | * @return array(int => int) |
||
| 434 | */ |
||
| 435 | protected abstract function getWarningList($testFile); |
||
| 436 | |||
| 437 | |||
| 438 | }//end class |
||
| 439 |