|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of PHP CS Fixer. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * Dariusz Rumiński <dariusz.ruminski@gmail.com> |
| 8 | + * |
| 9 | + * This source file is subject to the MIT license that is bundled |
| 10 | + * with this source code in the file LICENSE. |
| 11 | + */ |
| 12 | + |
| 13 | +namespace PhpCsFixer\Fixer\PhpUnit; |
| 14 | + |
| 15 | +use PhpCsFixer\AbstractFixer; |
| 16 | +use PhpCsFixer\DocBlock\Annotation; |
| 17 | +use PhpCsFixer\DocBlock\DocBlock; |
| 18 | +use PhpCsFixer\DocBlock\Line; |
| 19 | +use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface; |
| 20 | +use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface; |
| 21 | +use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver; |
| 22 | +use PhpCsFixer\FixerConfiguration\FixerOptionBuilder; |
| 23 | +use PhpCsFixer\FixerDefinition\CodeSample; |
| 24 | +use PhpCsFixer\FixerDefinition\FixerDefinition; |
| 25 | +use PhpCsFixer\Indicator\PhpUnitTestCaseIndicator; |
| 26 | +use PhpCsFixer\Tokenizer\Token; |
| 27 | +use PhpCsFixer\Tokenizer\Tokens; |
| 28 | +use SplFileInfo; |
| 29 | + |
| 30 | +/** |
| 31 | + * @author Jefersson Nathan <malukenho.dev@gmail.com> |
| 32 | + */ |
| 33 | +final class PhpUnitSizeClassFixer extends AbstractFixer implements WhitespacesAwareFixerInterface, ConfigurationDefinitionFixerInterface |
| 34 | +{ |
| 35 | + /** |
| 36 | + * {@inheritdoc} |
| 37 | + */ |
| 38 | + public function getDefinition() |
| 39 | + { |
| 40 | + return new FixerDefinition( |
| 41 | + 'All PHPUnit test cases should have `@small`, `@medium` or `@large` annotation to enable run time limits.', |
| 42 | + [new CodeSample("<?php\nclass MyTest extends TestCase {}\n")], |
| 43 | + 'The special groups [small, medium, large] provides a way to identify tests that are taking long to be executed.' |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * {@inheritdoc} |
| 49 | + */ |
| 50 | + public function isCandidate(Tokens $tokens) |
| 51 | + { |
| 52 | + return $tokens->isAllTokenKindsFound([T_CLASS, T_STRING]); |
| 53 | + } |
| 54 | + |
| 55 | + protected function applyFix(SplFileInfo $file, Tokens $tokens) |
| 56 | + { |
| 57 | + $phpUnitTestCaseIndicator = new PhpUnitTestCaseIndicator(); |
| 58 | + |
| 59 | + foreach ($phpUnitTestCaseIndicator->findPhpUnitClasses($tokens, true) as $indexes) { |
| 60 | + $this->markClassSize($tokens, $indexes[0]); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * {@inheritdoc} |
| 66 | + */ |
| 67 | + protected function createConfigurationDefinition() |
| 68 | + { |
| 69 | + return new FixerConfigurationResolver([ |
| 70 | + (new FixerOptionBuilder('group', 'Define a specific group to be used in case no group is already in use')) |
| 71 | + ->setAllowedValues(['small', 'medium', 'large']) |
| 72 | + ->setDefault('small') |
| 73 | + ->getOption(), |
| 74 | + ]); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @param Tokens $tokens |
| 79 | + * @param int $startIndex |
| 80 | + */ |
| 81 | + private function markClassSize(Tokens $tokens, $startIndex) |
| 82 | + { |
| 83 | + $classIndex = $tokens->getPrevTokenOfKind($startIndex, [[T_CLASS]]); |
| 84 | + |
| 85 | + if ($this->isAbstractClass($tokens, $classIndex)) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + $docBlockIndex = $this->getDocBlockIndex($tokens, $classIndex); |
| 90 | + |
| 91 | + if ($this->hasDocBlock($tokens, $classIndex)) { |
| 92 | + $this->updateDocBlockIfNeeded($tokens, $docBlockIndex); |
| 93 | + |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + $this->createDocBlock($tokens, $docBlockIndex); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * @param Tokens $tokens |
| 102 | + * @param int $i |
| 103 | + * |
| 104 | + * @return bool |
| 105 | + */ |
| 106 | + private function isAbstractClass(Tokens $tokens, $i) |
| 107 | + { |
| 108 | + $typeIndex = $tokens->getPrevMeaningfulToken($i); |
| 109 | + |
| 110 | + return $tokens[$typeIndex]->isGivenKind([T_ABSTRACT]); |
| 111 | + } |
| 112 | + |
| 113 | + private function createDocBlock(Tokens $tokens, $docBlockIndex) |
| 114 | + { |
| 115 | + $lineEnd = $this->whitespacesConfig->getLineEnding(); |
| 116 | + $originalIndent = $this->detectIndent($tokens, $tokens->getNextNonWhitespace($docBlockIndex)); |
| 117 | + $group = $this->configuration['group']; |
| 118 | + $toInsert = [ |
| 119 | + new Token([T_DOC_COMMENT, '/**'.$lineEnd."${originalIndent} * @".$group.$lineEnd."${originalIndent} */"]), |
| 120 | + new Token([T_WHITESPACE, $lineEnd.$originalIndent]), |
| 121 | + ]; |
| 122 | + $index = $tokens->getNextMeaningfulToken($docBlockIndex); |
| 123 | + $tokens->insertAt($index, $toInsert); |
| 124 | + } |
| 125 | + |
| 126 | + private function updateDocBlockIfNeeded(Tokens $tokens, $docBlockIndex) |
| 127 | + { |
| 128 | + $doc = new DocBlock($tokens[$docBlockIndex]->getContent()); |
| 129 | + if (!empty($this->filterDocBlock($doc))) { |
| 130 | + return; |
| 131 | + } |
| 132 | + $doc = $this->makeDocBlockMultiLineIfNeeded($doc, $tokens, $docBlockIndex); |
| 133 | + $lines = $this->addSizeAnnotation($doc, $tokens, $docBlockIndex); |
| 134 | + $lines = implode('', $lines); |
| 135 | + |
| 136 | + $tokens[$docBlockIndex] = new Token([T_DOC_COMMENT, $lines]); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * @param Tokens $tokens |
| 141 | + * @param int $index |
| 142 | + * |
| 143 | + * @return bool |
| 144 | + */ |
| 145 | + private function hasDocBlock(Tokens $tokens, $index) |
| 146 | + { |
| 147 | + $docBlockIndex = $this->getDocBlockIndex($tokens, $index); |
| 148 | + |
| 149 | + return $tokens[$docBlockIndex]->isGivenKind(T_DOC_COMMENT); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * @param Tokens $tokens |
| 154 | + * @param int $index |
| 155 | + * |
| 156 | + * @return int |
| 157 | + */ |
| 158 | + private function getDocBlockIndex(Tokens $tokens, $index) |
| 159 | + { |
| 160 | + do { |
| 161 | + $index = $tokens->getPrevNonWhitespace($index); |
| 162 | + } while ($tokens[$index]->isGivenKind([T_PUBLIC, T_PROTECTED, T_PRIVATE, T_FINAL, T_ABSTRACT, T_COMMENT])); |
| 163 | + |
| 164 | + return $index; |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * @param Tokens $tokens |
| 169 | + * @param int $index |
| 170 | + * |
| 171 | + * @return string |
| 172 | + */ |
| 173 | + private function detectIndent(Tokens $tokens, $index) |
| 174 | + { |
| 175 | + if (!$tokens[$index - 1]->isWhitespace()) { |
| 176 | + return ''; // cannot detect indent |
| 177 | + } |
| 178 | + |
| 179 | + $explodedContent = explode($this->whitespacesConfig->getLineEnding(), $tokens[$index - 1]->getContent()); |
| 180 | + |
| 181 | + return end($explodedContent); |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * @param DocBlock $docBlock |
| 186 | + * @param Tokens $tokens |
| 187 | + * @param int $docBlockIndex |
| 188 | + * |
| 189 | + * @return Line[] |
| 190 | + */ |
| 191 | + private function addSizeAnnotation(DocBlock $docBlock, Tokens $tokens, $docBlockIndex) |
| 192 | + { |
| 193 | + $lines = $docBlock->getLines(); |
| 194 | + $originalIndent = $this->detectIndent($tokens, $docBlockIndex); |
| 195 | + $lineEnd = $this->whitespacesConfig->getLineEnding(); |
| 196 | + $group = $this->configuration['group']; |
| 197 | + array_splice($lines, -1, 0, $originalIndent.' *'.$lineEnd.$originalIndent.' * @'.$group.$lineEnd); |
| 198 | + |
| 199 | + return $lines; |
| 200 | + } |
| 201 | + |
| 202 | + /** |
| 203 | + * @param DocBlock $doc |
| 204 | + * @param Tokens $tokens |
| 205 | + * @param int $docBlockIndex |
| 206 | + * |
| 207 | + * @return DocBlock |
| 208 | + */ |
| 209 | + private function makeDocBlockMultiLineIfNeeded(DocBlock $doc, Tokens $tokens, $docBlockIndex) |
| 210 | + { |
| 211 | + $lines = $doc->getLines(); |
| 212 | + if (1 === \count($lines) && empty($this->filterDocBlock($doc))) { |
| 213 | + $lines = $this->splitUpDocBlock($lines, $tokens, $docBlockIndex); |
| 214 | + |
| 215 | + return new DocBlock(implode('', $lines)); |
| 216 | + } |
| 217 | + |
| 218 | + return $doc; |
| 219 | + } |
| 220 | + |
| 221 | + /** |
| 222 | + * Take a one line doc block, and turn it into a multi line doc block. |
| 223 | + * |
| 224 | + * @param Line[] $lines |
| 225 | + * @param Tokens $tokens |
| 226 | + * @param int $docBlockIndex |
| 227 | + * |
| 228 | + * @return Line[] |
| 229 | + */ |
| 230 | + private function splitUpDocBlock($lines, Tokens $tokens, $docBlockIndex) |
| 231 | + { |
| 232 | + $lineContent = $this->getSingleLineDocBlockEntry($lines); |
| 233 | + $lineEnd = $this->whitespacesConfig->getLineEnding(); |
| 234 | + $originalIndent = $this->detectIndent($tokens, $tokens->getNextNonWhitespace($docBlockIndex)); |
| 235 | + |
| 236 | + return [ |
| 237 | + new Line('/**'.$lineEnd), |
| 238 | + new Line($originalIndent.' * '.$lineContent.$lineEnd), |
| 239 | + new Line($originalIndent.' */'), |
| 240 | + ]; |
| 241 | + } |
| 242 | + |
| 243 | + /** |
| 244 | + * @param Line|Line[]|string $line |
| 245 | + * |
| 246 | + * @return string |
| 247 | + */ |
| 248 | + private function getSingleLineDocBlockEntry($line) |
| 249 | + { |
| 250 | + $line = $line[0]; |
| 251 | + $line = str_replace('*/', '', $line); |
| 252 | + $line = trim($line); |
| 253 | + $line = str_split($line); |
| 254 | + $i = \count($line); |
| 255 | + do { |
| 256 | + --$i; |
| 257 | + } while ('*' !== $line[$i] && '*' !== $line[$i - 1] && '/' !== $line[$i - 2]); |
| 258 | + if (' ' === $line[$i]) { |
| 259 | + ++$i; |
| 260 | + } |
| 261 | + $line = \array_slice($line, $i); |
| 262 | + |
| 263 | + return implode('', $line); |
| 264 | + } |
| 265 | + |
| 266 | + /** |
| 267 | + * @param DocBlock $doc |
| 268 | + * |
| 269 | + * @return Annotation[] |
| 270 | + */ |
| 271 | + private function filterDocBlock(DocBlock $doc) |
| 272 | + { |
| 273 | + return array_filter([ |
| 274 | + $doc->getAnnotationsOfType('small'), |
| 275 | + $doc->getAnnotationsOfType('large'), |
| 276 | + $doc->getAnnotationsOfType('medium'), |
| 277 | + ]); |
| 278 | + } |
| 279 | +} |
0 commit comments