AbstractFixerTestCase.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests\Test;
  12. use PhpCsFixer\AbstractFixer;
  13. use PhpCsFixer\AbstractProxyFixer;
  14. use PhpCsFixer\Fixer\Comment\HeaderCommentFixer;
  15. use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
  16. use PhpCsFixer\Fixer\DefinedFixerInterface;
  17. use PhpCsFixer\Fixer\DeprecatedFixerInterface;
  18. use PhpCsFixer\Fixer\Whitespace\SingleBlankLineAtEofFixer;
  19. use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
  20. use PhpCsFixer\FixerConfiguration\FixerOptionInterface;
  21. use PhpCsFixer\FixerDefinition\CodeSampleInterface;
  22. use PhpCsFixer\FixerDefinition\FileSpecificCodeSampleInterface;
  23. use PhpCsFixer\FixerDefinition\VersionSpecificCodeSampleInterface;
  24. use PhpCsFixer\Linter\CachingLinter;
  25. use PhpCsFixer\Linter\Linter;
  26. use PhpCsFixer\Linter\LinterInterface;
  27. use PhpCsFixer\Linter\ProcessLinter;
  28. use PhpCsFixer\Preg;
  29. use PhpCsFixer\StdinFileInfo;
  30. use PhpCsFixer\Tests\Test\Assert\AssertTokensTrait;
  31. use PhpCsFixer\Tests\TestCase;
  32. use PhpCsFixer\Tokenizer\Token;
  33. use PhpCsFixer\Tokenizer\Tokens;
  34. use Prophecy\Argument;
  35. /**
  36. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  37. *
  38. * @internal
  39. */
  40. abstract class AbstractFixerTestCase extends TestCase
  41. {
  42. use AssertTokensTrait;
  43. use IsIdenticalConstraint;
  44. /**
  45. * @var null|LinterInterface
  46. */
  47. protected $linter;
  48. /**
  49. * @var null|AbstractFixer
  50. */
  51. protected $fixer;
  52. // do not modify this structure without prior discussion
  53. private $allowedRequiredOptions = [
  54. 'header_comment' => ['header' => true],
  55. ];
  56. // do not modify this structure without prior discussion
  57. private $allowedFixersWithoutDefaultCodeSample = [
  58. 'general_phpdoc_annotation_remove' => true,
  59. 'general_phpdoc_tag_rename' => true,
  60. ];
  61. protected function doSetUp()
  62. {
  63. parent::doSetUp();
  64. $this->linter = $this->getLinter();
  65. $this->fixer = $this->createFixer();
  66. // @todo remove at 3.0 together with env var itself
  67. if (getenv('PHP_CS_FIXER_TEST_USE_LEGACY_TOKENIZER')) {
  68. Tokens::setLegacyMode(true);
  69. }
  70. }
  71. protected function doTearDown()
  72. {
  73. parent::doTearDown();
  74. $this->linter = null;
  75. $this->fixer = null;
  76. // @todo remove at 3.0
  77. Tokens::setLegacyMode(false);
  78. }
  79. final public function testIsRisky()
  80. {
  81. static::assertIsBool($this->fixer->isRisky(), sprintf('Return type for ::isRisky of "%s" is invalid.', $this->fixer->getName()));
  82. if ($this->fixer->isRisky()) {
  83. self::assertValidDescription($this->fixer->getName(), 'risky description', $this->fixer->getDefinition()->getRiskyDescription());
  84. } else {
  85. static::assertNull($this->fixer->getDefinition()->getRiskyDescription(), sprintf('[%s] Fixer is not risky so no description of it expected.', $this->fixer->getName()));
  86. }
  87. if ($this->fixer instanceof AbstractProxyFixer) {
  88. return;
  89. }
  90. $reflection = new \ReflectionMethod($this->fixer, 'isRisky');
  91. // If fixer is not risky then the method `isRisky` from `AbstractFixer` must be used
  92. static::assertSame(
  93. !$this->fixer->isRisky(),
  94. AbstractFixer::class === $reflection->getDeclaringClass()->getName()
  95. );
  96. }
  97. final public function testFixerDefinitions()
  98. {
  99. static::assertInstanceOf(DefinedFixerInterface::class, $this->fixer);
  100. $fixerName = $this->fixer->getName();
  101. $definition = $this->fixer->getDefinition();
  102. $fixerIsConfigurable = $this->fixer instanceof ConfigurationDefinitionFixerInterface;
  103. self::assertValidDescription($fixerName, 'summary', $definition->getSummary());
  104. $samples = $definition->getCodeSamples();
  105. static::assertNotEmpty($samples, sprintf('[%s] Code samples are required.', $fixerName));
  106. $configSamplesProvided = [];
  107. $dummyFileInfo = new StdinFileInfo();
  108. foreach ($samples as $sampleCounter => $sample) {
  109. static::assertInstanceOf(CodeSampleInterface::class, $sample, sprintf('[%s] Sample #%d', $fixerName, $sampleCounter));
  110. static::assertIsInt($sampleCounter);
  111. $code = $sample->getCode();
  112. static::assertIsString($code, sprintf('[%s] Sample #%d', $fixerName, $sampleCounter));
  113. static::assertNotEmpty($code, sprintf('[%s] Sample #%d', $fixerName, $sampleCounter));
  114. if (!($this->fixer instanceof SingleBlankLineAtEofFixer)) {
  115. static::assertSame("\n", substr($code, -1), sprintf('[%s] Sample #%d must end with linebreak', $fixerName, $sampleCounter));
  116. }
  117. $config = $sample->getConfiguration();
  118. if (null !== $config) {
  119. static::assertTrue($fixerIsConfigurable, sprintf('[%s] Sample #%d has configuration, but the fixer is not configurable.', $fixerName, $sampleCounter));
  120. static::assertIsArray($config, sprintf('[%s] Sample #%d configuration must be an array or null.', $fixerName, $sampleCounter));
  121. $configSamplesProvided[$sampleCounter] = $config;
  122. } elseif ($fixerIsConfigurable) {
  123. if (!$sample instanceof VersionSpecificCodeSampleInterface) {
  124. static::assertArrayNotHasKey('default', $configSamplesProvided, sprintf('[%s] Multiple non-versioned samples with default configuration.', $fixerName));
  125. }
  126. $configSamplesProvided['default'] = true;
  127. }
  128. if ($sample instanceof VersionSpecificCodeSampleInterface && !$sample->isSuitableFor(\PHP_VERSION_ID)) {
  129. continue;
  130. }
  131. if ($fixerIsConfigurable) {
  132. // always re-configure as the fixer might have been configured with diff. configuration form previous sample
  133. $this->fixer->configure(null === $config ? [] : $config);
  134. }
  135. Tokens::clearCache();
  136. $tokens = Tokens::fromCode($code);
  137. $this->fixer->fix(
  138. $sample instanceof FileSpecificCodeSampleInterface ? $sample->getSplFileInfo() : $dummyFileInfo,
  139. $tokens
  140. );
  141. static::assertTrue($tokens->isChanged(), sprintf('[%s] Sample #%d is not changed during fixing.', $fixerName, $sampleCounter));
  142. $duplicatedCodeSample = array_search(
  143. $sample,
  144. \array_slice($samples, 0, $sampleCounter),
  145. false
  146. );
  147. static::assertFalse(
  148. $duplicatedCodeSample,
  149. sprintf('[%s] Sample #%d duplicates #%d.', $fixerName, $sampleCounter, $duplicatedCodeSample)
  150. );
  151. }
  152. if ($fixerIsConfigurable) {
  153. if (isset($configSamplesProvided['default'])) {
  154. reset($configSamplesProvided);
  155. static::assertSame('default', key($configSamplesProvided), sprintf('[%s] First sample must be for the default configuration.', $fixerName));
  156. } elseif (!isset($this->allowedFixersWithoutDefaultCodeSample[$fixerName])) {
  157. static::assertArrayHasKey($fixerName, $this->allowedRequiredOptions, sprintf('[%s] Has no sample for default configuration.', $fixerName));
  158. }
  159. // It may only shrink, never add anything to it.
  160. $fixerNamesWithKnownMissingSamplesWithConfig = [ // @TODO 3.0 - remove this
  161. 'is_null', // has only one option which is deprecated
  162. ];
  163. if (\count($configSamplesProvided) < 2) {
  164. if (\in_array($fixerName, $fixerNamesWithKnownMissingSamplesWithConfig, true)) {
  165. static::markTestIncomplete(sprintf('[%s] Configurable fixer only provides a default configuration sample and none for its configuration options, please help and add it.', $fixerName));
  166. }
  167. static::fail(sprintf('[%s] Configurable fixer only provides a default configuration sample and none for its configuration options.', $fixerName));
  168. } elseif (\in_array($fixerName, $fixerNamesWithKnownMissingSamplesWithConfig, true)) {
  169. static::fail(sprintf('[%s] Invalid listed as missing code samples, please update the list.', $fixerName));
  170. }
  171. $options = $this->fixer->getConfigurationDefinition()->getOptions();
  172. foreach ($options as $option) {
  173. static::assertMatchesRegularExpression('/^[a-z_]+[a-z]$/', $option->getName(), sprintf('[%s] Option %s is not snake_case.', $fixerName, $option->getName()));
  174. }
  175. }
  176. }
  177. /**
  178. * @group legacy
  179. * @expectedDeprecation PhpCsFixer\FixerDefinition\FixerDefinition::getConfigurationDescription is deprecated and will be removed in 3.0.
  180. * @expectedDeprecation PhpCsFixer\FixerDefinition\FixerDefinition::getDefaultConfiguration is deprecated and will be removed in 3.0.
  181. */
  182. final public function testLegacyFixerDefinitions()
  183. {
  184. $definition = $this->fixer->getDefinition();
  185. static::assertNull($definition->getConfigurationDescription(), sprintf('[%s] No configuration description expected.', $this->fixer->getName()));
  186. static::assertNull($definition->getDefaultConfiguration(), sprintf('[%s] No default configuration expected.', $this->fixer->getName()));
  187. }
  188. final public function testFixersAreFinal()
  189. {
  190. $reflection = new \ReflectionClass($this->fixer);
  191. static::assertTrue(
  192. $reflection->isFinal(),
  193. sprintf('Fixer "%s" must be declared "final".', $this->fixer->getName())
  194. );
  195. }
  196. final public function testFixersAreDefined()
  197. {
  198. static::assertInstanceOf(DefinedFixerInterface::class, $this->fixer);
  199. }
  200. final public function testDeprecatedFixersHaveCorrectSummary()
  201. {
  202. $reflection = new \ReflectionClass($this->fixer);
  203. $comment = $reflection->getDocComment();
  204. static::assertStringNotContainsString(
  205. 'DEPRECATED',
  206. $this->fixer->getDefinition()->getSummary(),
  207. 'Fixer cannot contain word "DEPRECATED" in summary'
  208. );
  209. if ($this->fixer instanceof DeprecatedFixerInterface) {
  210. static::assertStringContainsString('@deprecated', $comment);
  211. } elseif (\is_string($comment)) {
  212. static::assertStringNotContainsString('@deprecated', $comment);
  213. }
  214. }
  215. /**
  216. * Blur filter that find candidate fixer for performance optimization to use only `insertSlices` instead of multiple `insertAt` if there is no other collection manipulation.
  217. */
  218. public function testFixerUseInsertSlicesWhenOnlyInsertionsArePerformed()
  219. {
  220. $reflection = new \ReflectionClass($this->fixer);
  221. $tokens = Tokens::fromCode(file_get_contents($reflection->getFileName()));
  222. $sequences = $this->findAllTokenSequences($tokens, [[T_VARIABLE, '$tokens'], [T_OBJECT_OPERATOR], [T_STRING]]);
  223. $usedMethods = array_unique(array_map(function (array $sequence) {
  224. $last = end($sequence);
  225. return $last->getContent();
  226. }, $sequences));
  227. // if there is no `insertAt`, it's not a candidate
  228. if (!\in_array('insertAt', $usedMethods, true)) {
  229. $this->addToAssertionCount(1);
  230. return;
  231. }
  232. $usedMethods = array_filter($usedMethods, function ($method) {
  233. return 0 === Preg::match('/^(count|find|generate|get|is|rewind)/', $method);
  234. });
  235. $allowedMethods = ['insertAt'];
  236. $nonAllowedMethods = array_diff($usedMethods, $allowedMethods);
  237. if ([] === $nonAllowedMethods) {
  238. $fixerName = $this->fixer->getName();
  239. if (\in_array($fixerName, [
  240. // DO NOT add anything to this list at ease, align with core contributors whether it makes sense to insert tokens individually or by bulk for your case.
  241. // The original list of the fixers being exceptions and insert tokens individually came from legacy reasons when it was the only available methods to insert tokens.
  242. 'blank_line_after_namespace',
  243. 'blank_line_after_opening_tag',
  244. 'blank_line_before_statement',
  245. 'class_attributes_separation',
  246. 'date_time_immutable',
  247. 'declare_strict_types',
  248. 'doctrine_annotation_braces',
  249. 'doctrine_annotation_spaces',
  250. 'final_internal_class',
  251. 'final_public_method_for_abstract_class',
  252. 'function_typehint_space',
  253. 'heredoc_indentation',
  254. 'method_chaining_indentation',
  255. 'native_constant_invocation',
  256. 'new_with_braces',
  257. 'no_short_echo_tag',
  258. 'not_operator_with_space',
  259. 'not_operator_with_successor_space',
  260. 'php_unit_internal_class',
  261. 'php_unit_no_expectation_annotation',
  262. 'php_unit_set_up_tear_down_visibility',
  263. 'php_unit_size_class',
  264. 'php_unit_test_annotation',
  265. 'php_unit_test_class_requires_covers',
  266. 'phpdoc_to_param_type',
  267. 'phpdoc_to_return_type',
  268. 'random_api_migration',
  269. 'semicolon_after_instruction',
  270. 'single_line_after_imports',
  271. 'static_lambda',
  272. 'strict_param',
  273. 'void_return',
  274. ], true)) {
  275. static::markTestIncomplete(sprintf('Fixer "%s" may be optimized to use `Tokens::insertSlices` instead of `%s`, please help and optimize it.', $fixerName, implode(', ', $allowedMethods)));
  276. }
  277. static::fail(sprintf('Fixer "%s" shall be optimized to use `Tokens::insertSlices` instead of `%s`.', $fixerName, implode(', ', $allowedMethods)));
  278. }
  279. $this->addToAssertionCount(1);
  280. }
  281. final public function testFixerConfigurationDefinitions()
  282. {
  283. if (!$this->fixer instanceof ConfigurationDefinitionFixerInterface) {
  284. $this->addToAssertionCount(1); // not applied to the fixer without configuration
  285. return;
  286. }
  287. $configurationDefinition = $this->fixer->getConfigurationDefinition();
  288. static::assertInstanceOf(FixerConfigurationResolverInterface::class, $configurationDefinition);
  289. foreach ($configurationDefinition->getOptions() as $option) {
  290. static::assertInstanceOf(FixerOptionInterface::class, $option);
  291. static::assertNotEmpty($option->getDescription());
  292. static::assertSame(
  293. !isset($this->allowedRequiredOptions[$this->fixer->getName()][$option->getName()]),
  294. $option->hasDefault(),
  295. sprintf(
  296. $option->hasDefault()
  297. ? 'Option `%s` of fixer `%s` is wrongly listed in `$allowedRequiredOptions` structure, as it is not required. If you just changed that option to not be required anymore, please adjust mentioned structure.'
  298. : 'Option `%s` of fixer `%s` shall not be required. If you want to introduce new required option please adjust `$allowedRequiredOptions` structure.',
  299. $option->getName(),
  300. $this->fixer->getName()
  301. )
  302. );
  303. static::assertStringNotContainsString(
  304. 'DEPRECATED',
  305. $option->getDescription(),
  306. 'Option description cannot contain word "DEPRECATED"'
  307. );
  308. }
  309. }
  310. final public function testFixersReturnTypes()
  311. {
  312. $tokens = Tokens::fromCode('<?php ');
  313. $emptyTokens = new Tokens();
  314. static::assertIsInt($this->fixer->getPriority(), sprintf('Return type for ::getPriority of "%s" is invalid.', $this->fixer->getName()));
  315. static::assertIsBool($this->fixer->supports(new \SplFileInfo(__FILE__)), sprintf('Return type for ::supports of "%s" is invalid.', $this->fixer->getName()));
  316. static::assertIsBool($this->fixer->isCandidate($emptyTokens), sprintf('Return type for ::isCandidate with empty tokens of "%s" is invalid.', $this->fixer->getName()));
  317. static::assertFalse($emptyTokens->isChanged());
  318. static::assertIsBool($this->fixer->isCandidate($tokens), sprintf('Return type for ::isCandidate of "%s" is invalid.', $this->fixer->getName()));
  319. static::assertFalse($tokens->isChanged());
  320. if ($this->fixer instanceof HeaderCommentFixer) {
  321. $this->fixer->configure(['header' => 'a']);
  322. }
  323. static::assertNull($this->fixer->fix(new \SplFileInfo(__FILE__), $emptyTokens), sprintf('Return type for ::fix with empty tokens of "%s" is invalid.', $this->fixer->getName()));
  324. static::assertFalse($emptyTokens->isChanged());
  325. static::assertNull($this->fixer->fix(new \SplFileInfo(__FILE__), $tokens), sprintf('Return type for ::fix of "%s" is invalid.', $this->fixer->getName()));
  326. }
  327. /**
  328. * @return AbstractFixer
  329. */
  330. protected function createFixer()
  331. {
  332. $fixerClassName = preg_replace('/^(PhpCsFixer)\\\\Tests(\\\\.+)Test$/', '$1$2', static::class);
  333. return new $fixerClassName();
  334. }
  335. /**
  336. * @param string $filename
  337. *
  338. * @return \SplFileInfo
  339. */
  340. protected function getTestFile($filename = __FILE__)
  341. {
  342. static $files = [];
  343. if (!isset($files[$filename])) {
  344. $files[$filename] = new \SplFileInfo($filename);
  345. }
  346. return $files[$filename];
  347. }
  348. /**
  349. * Tests if a fixer fixes a given string to match the expected result.
  350. *
  351. * It is used both if you want to test if something is fixed or if it is not touched by the fixer.
  352. * It also makes sure that the expected output does not change when run through the fixer. That means that you
  353. * do not need two test cases like [$expected] and [$expected, $input] (where $expected is the same in both cases)
  354. * as the latter covers both of them.
  355. * This method throws an exception if $expected and $input are equal to prevent test cases that accidentally do
  356. * not test anything.
  357. *
  358. * @param string $expected The expected fixer output
  359. * @param null|string $input The fixer input, or null if it should intentionally be equal to the output
  360. * @param null|\SplFileInfo $file The file to fix, or null if unneeded
  361. */
  362. protected function doTest($expected, $input = null, \SplFileInfo $file = null)
  363. {
  364. if ($expected === $input) {
  365. throw new \InvalidArgumentException('Input parameter must not be equal to expected parameter.');
  366. }
  367. $file = $file ?: $this->getTestFile();
  368. $fileIsSupported = $this->fixer->supports($file);
  369. if (null !== $input) {
  370. static::assertNull($this->lintSource($input));
  371. Tokens::clearCache();
  372. $tokens = Tokens::fromCode($input);
  373. if ($fileIsSupported) {
  374. static::assertTrue($this->fixer->isCandidate($tokens), 'Fixer must be a candidate for input code.');
  375. static::assertFalse($tokens->isChanged(), 'Fixer must not touch Tokens on candidate check.');
  376. $fixResult = $this->fixer->fix($file, $tokens);
  377. static::assertNull($fixResult, '->fix method must return null.');
  378. }
  379. static::assertThat(
  380. $tokens->generateCode(),
  381. self::createIsIdenticalStringConstraint($expected),
  382. 'Code build on input code must match expected code.'
  383. );
  384. static::assertTrue($tokens->isChanged(), 'Tokens collection built on input code must be marked as changed after fixing.');
  385. $tokens->clearEmptyTokens();
  386. static::assertSame(
  387. \count($tokens),
  388. \count(array_unique(array_map(static function (Token $token) {
  389. return spl_object_hash($token);
  390. }, $tokens->toArray()))),
  391. 'Token items inside Tokens collection must be unique.'
  392. );
  393. Tokens::clearCache();
  394. $expectedTokens = Tokens::fromCode($expected);
  395. static::assertTokens($expectedTokens, $tokens);
  396. }
  397. static::assertNull($this->lintSource($expected));
  398. Tokens::clearCache();
  399. $tokens = Tokens::fromCode($expected);
  400. if ($fileIsSupported) {
  401. $fixResult = $this->fixer->fix($file, $tokens);
  402. static::assertNull($fixResult, '->fix method must return null.');
  403. }
  404. static::assertThat(
  405. $tokens->generateCode(),
  406. self::createIsIdenticalStringConstraint($expected),
  407. 'Code build on expected code must not change.'
  408. );
  409. static::assertFalse($tokens->isChanged(), 'Tokens collection built on expected code must not be marked as changed after fixing.');
  410. }
  411. /**
  412. * @param string $source
  413. *
  414. * @return null|string
  415. */
  416. protected function lintSource($source)
  417. {
  418. try {
  419. $this->linter->lintSource($source)->check();
  420. } catch (\Exception $e) {
  421. return $e->getMessage()."\n\nSource:\n{$source}";
  422. }
  423. return null;
  424. }
  425. /**
  426. * @return LinterInterface
  427. */
  428. private function getLinter()
  429. {
  430. static $linter = null;
  431. if (null === $linter) {
  432. if (getenv('SKIP_LINT_TEST_CASES')) {
  433. $linterProphecy = $this->prophesize(\PhpCsFixer\Linter\LinterInterface::class);
  434. $linterProphecy
  435. ->lintSource(Argument::type('string'))
  436. ->willReturn($this->prophesize(\PhpCsFixer\Linter\LintingResultInterface::class)->reveal())
  437. ;
  438. $linter = $linterProphecy->reveal();
  439. } else {
  440. $linter = new CachingLinter(
  441. getenv('FAST_LINT_TEST_CASES') ? new Linter() : new ProcessLinter()
  442. );
  443. }
  444. }
  445. return $linter;
  446. }
  447. /**
  448. * @param string $fixerName
  449. * @param string $descriptionType
  450. * @param mixed $description
  451. */
  452. private static function assertValidDescription($fixerName, $descriptionType, $description)
  453. {
  454. static::assertIsString($description);
  455. static::assertMatchesRegularExpression('/^[A-Z`][^"]+\.$/', $description, sprintf('[%s] The %s must start with capital letter or a ` and end with dot.', $fixerName, $descriptionType));
  456. static::assertStringNotContainsString('phpdocs', $description, sprintf('[%s] `PHPDoc` must not be in the plural in %s.', $fixerName, $descriptionType));
  457. static::assertCorrectCasing($description, 'PHPDoc', sprintf('[%s] `PHPDoc` must be in correct casing in %s.', $fixerName, $descriptionType));
  458. static::assertCorrectCasing($description, 'PHPUnit', sprintf('[%s] `PHPUnit` must be in correct casing in %s.', $fixerName, $descriptionType));
  459. static::assertFalse(strpos($descriptionType, '``'), sprintf('[%s] The %s must no contain sequential backticks.', $fixerName, $descriptionType));
  460. }
  461. /**
  462. * @param string $needle
  463. * @param string $haystack
  464. * @param string $message
  465. */
  466. private static function assertCorrectCasing($needle, $haystack, $message)
  467. {
  468. static::assertSame(substr_count(strtolower($haystack), strtolower($needle)), substr_count($haystack, $needle), $message);
  469. }
  470. private function findAllTokenSequences($tokens, $sequence)
  471. {
  472. $lastIndex = 0;
  473. $sequences = [];
  474. while ($found = $tokens->findSequence($sequence, $lastIndex)) {
  475. $keys = array_keys($found);
  476. $sequences[] = $found;
  477. $lastIndex = $keys[2];
  478. }
  479. return $sequences;
  480. }
  481. }