ProjectCodeTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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\AutoReview;
  12. use PhpCsFixer\DocBlock\DocBlock;
  13. use PhpCsFixer\Event\Event;
  14. use PhpCsFixer\FixerFactory;
  15. use PhpCsFixer\Preg;
  16. use PhpCsFixer\Tests\TestCase;
  17. use PhpCsFixer\Tokenizer\Token;
  18. use PhpCsFixer\Tokenizer\Tokens;
  19. use Symfony\Component\Finder\Finder;
  20. use Symfony\Component\Finder\SplFileInfo;
  21. /**
  22. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  23. *
  24. * @internal
  25. *
  26. * @coversNothing
  27. * @group auto-review
  28. * @group covers-nothing
  29. */
  30. final class ProjectCodeTest extends TestCase
  31. {
  32. /**
  33. * This structure contains older classes that are not yet covered by tests.
  34. *
  35. * It may only shrink, never add anything to it.
  36. *
  37. * @var string[]
  38. */
  39. private static $classesWithoutTests = [
  40. \PhpCsFixer\Console\Command\DocumentationCommand::class,
  41. \PhpCsFixer\Console\SelfUpdate\GithubClient::class,
  42. \PhpCsFixer\Doctrine\Annotation\Tokens::class,
  43. \PhpCsFixer\Documentation\DocumentationGenerator::class,
  44. \PhpCsFixer\Runner\FileCachingLintingIterator::class,
  45. ];
  46. public function testThatClassesWithoutTestsVarIsProper()
  47. {
  48. $unknownClasses = array_filter(
  49. self::$classesWithoutTests,
  50. static function ($class) { return !class_exists($class) && !trait_exists($class); }
  51. );
  52. static::assertSame([], $unknownClasses);
  53. }
  54. /**
  55. * @param string $className
  56. *
  57. * @dataProvider provideSrcConcreteClassCases
  58. */
  59. public function testThatSrcClassHaveTestClass($className)
  60. {
  61. $testClassName = 'PhpCsFixer\\Tests'.substr($className, 10).'Test';
  62. if (\in_array($className, self::$classesWithoutTests, true)) {
  63. static::assertFalse(class_exists($testClassName), sprintf('Class "%s" already has tests, so it should be removed from "%s::$classesWithoutTests".', $className, __CLASS__));
  64. static::markTestIncomplete(sprintf('Class "%s" has no tests yet, please help and add it.', $className));
  65. }
  66. static::assertTrue(class_exists($testClassName), sprintf('Expected test class "%s" for "%s" not found.', $testClassName, $className));
  67. static::assertTrue(is_subclass_of($testClassName, TestCase::class), sprintf('Expected test class "%s" to be a subclass of "\PhpCsFixer\Tests\TestCase".', $testClassName));
  68. }
  69. /**
  70. * @param string $className
  71. *
  72. * @dataProvider provideSrcClassesNotAbuseInterfacesCases
  73. */
  74. public function testThatSrcClassesNotAbuseInterfaces($className)
  75. {
  76. $rc = new \ReflectionClass($className);
  77. $allowedMethods = array_map(
  78. function (\ReflectionClass $interface) {
  79. return $this->getPublicMethodNames($interface);
  80. },
  81. $rc->getInterfaces()
  82. );
  83. if (\count($allowedMethods)) {
  84. $allowedMethods = array_unique(array_merge(...array_values($allowedMethods)));
  85. }
  86. $allowedMethods[] = '__construct';
  87. $allowedMethods[] = '__destruct';
  88. $allowedMethods[] = '__wakeup';
  89. $exceptionMethods = [
  90. 'configure', // due to AbstractFixer::configure
  91. 'getConfigurationDefinition', // due to AbstractFixer::getConfigurationDefinition
  92. 'getDefaultConfiguration', // due to AbstractFixer::getDefaultConfiguration
  93. 'setWhitespacesConfig', // due to AbstractFixer::setWhitespacesConfig
  94. ];
  95. // @TODO: 3.0 should be removed
  96. $exceptionMethodsPerClass = [
  97. \PhpCsFixer\Event\Event::class => ['stopPropagation'],
  98. ];
  99. $definedMethods = $this->getPublicMethodNames($rc);
  100. $extraMethods = array_diff(
  101. $definedMethods,
  102. $allowedMethods,
  103. $exceptionMethods,
  104. isset($exceptionMethodsPerClass[$className]) ? $exceptionMethodsPerClass[$className] : []
  105. );
  106. sort($extraMethods);
  107. static::assertEmpty(
  108. $extraMethods,
  109. sprintf(
  110. "Class '%s' should not have public methods that are not part of implemented interfaces.\nViolations:\n%s",
  111. $className,
  112. implode("\n", array_map(static function ($item) {
  113. return " * {$item}";
  114. }, $extraMethods))
  115. )
  116. );
  117. }
  118. /**
  119. * @param string $className
  120. *
  121. * @dataProvider provideSrcClassCases
  122. */
  123. public function testThatSrcClassesNotExposeProperties($className)
  124. {
  125. $rc = new \ReflectionClass($className);
  126. static::assertEmpty(
  127. $rc->getProperties(\ReflectionProperty::IS_PUBLIC),
  128. sprintf('Class \'%s\' should not have public properties.', $className)
  129. );
  130. if ($rc->isFinal()) {
  131. return;
  132. }
  133. $allowedProps = [];
  134. $definedProps = $rc->getProperties(\ReflectionProperty::IS_PROTECTED);
  135. if (false !== $rc->getParentClass()) {
  136. $allowedProps = $rc->getParentClass()->getProperties(\ReflectionProperty::IS_PROTECTED);
  137. }
  138. $allowedProps = array_map(static function (\ReflectionProperty $item) {
  139. return $item->getName();
  140. }, $allowedProps);
  141. $definedProps = array_map(static function (\ReflectionProperty $item) {
  142. return $item->getName();
  143. }, $definedProps);
  144. $exceptionPropsPerClass = [
  145. \PhpCsFixer\AbstractPhpdocTypesFixer::class => ['tags'],
  146. \PhpCsFixer\AbstractFixer::class => ['configuration', 'configurationDefinition', 'whitespacesConfig'],
  147. \PhpCsFixer\AbstractProxyFixer::class => ['proxyFixers'],
  148. ];
  149. $extraProps = array_diff(
  150. $definedProps,
  151. $allowedProps,
  152. isset($exceptionPropsPerClass[$className]) ? $exceptionPropsPerClass[$className] : []
  153. );
  154. sort($extraProps);
  155. static::assertEmpty(
  156. $extraProps,
  157. sprintf(
  158. "Class '%s' should not have protected properties.\nViolations:\n%s",
  159. $className,
  160. implode("\n", array_map(static function ($item) {
  161. return " * {$item}";
  162. }, $extraProps))
  163. )
  164. );
  165. }
  166. /**
  167. * @dataProvider provideTestClassCases
  168. *
  169. * @param string $testClassName
  170. */
  171. public function testThatTestClassesAreTraitOrAbstractOrFinal($testClassName)
  172. {
  173. $rc = new \ReflectionClass($testClassName);
  174. static::assertTrue(
  175. $rc->isTrait() || $rc->isAbstract() || $rc->isFinal(),
  176. sprintf('Test class %s should be trait, abstract or final.', $testClassName)
  177. );
  178. }
  179. /**
  180. * @dataProvider provideTestClassCases
  181. *
  182. * @param string $testClassName
  183. */
  184. public function testThatTestClassesAreInternal($testClassName)
  185. {
  186. $rc = new \ReflectionClass($testClassName);
  187. $doc = new DocBlock($rc->getDocComment());
  188. static::assertNotEmpty(
  189. $doc->getAnnotationsOfType('internal'),
  190. sprintf('Test class %s should have internal annotation.', $testClassName)
  191. );
  192. }
  193. /**
  194. * @dataProvider provideTestClassCases
  195. *
  196. * @param string $testClassName
  197. */
  198. public function testThatPublicMethodsAreCorrectlyNamed($testClassName)
  199. {
  200. $reflectionClass = new \ReflectionClass($testClassName);
  201. $publicMethods = array_filter(
  202. $reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC),
  203. static function (\ReflectionMethod $reflectionMethod) use ($reflectionClass) {
  204. return $reflectionMethod->getDeclaringClass()->getName() === $reflectionClass->getName();
  205. }
  206. );
  207. if ([] === $publicMethods) {
  208. $this->addToAssertionCount(1); // no methods to test, all good!
  209. }
  210. foreach ($publicMethods as $method) {
  211. static::assertMatchesRegularExpression(
  212. '/^(test|expect|provide|doSetUpBeforeClass$|doTearDownAfterClass$)/',
  213. $method->getName(),
  214. sprintf('Public method "%s::%s" is not properly named.', $reflectionClass->getName(), $method->getName())
  215. );
  216. }
  217. }
  218. /**
  219. * @dataProvider provideTestClassCases
  220. *
  221. * @param string $testClassName
  222. */
  223. public function testThatDataProvidersAreCorrectlyNamed($testClassName)
  224. {
  225. $usedDataProviderMethodNames = $this->getUsedDataProviderMethodNames($testClassName);
  226. if (empty($usedDataProviderMethodNames)) {
  227. $this->addToAssertionCount(1); // no data providers to test, all good!
  228. return;
  229. }
  230. foreach ($usedDataProviderMethodNames as $dataProviderMethodName) {
  231. static::assertMatchesRegularExpression('/^provide[A-Z]\S+Cases$/', $dataProviderMethodName, sprintf(
  232. 'Data provider in "%s" with name "%s" is not correctly named.',
  233. $testClassName,
  234. $dataProviderMethodName
  235. ));
  236. }
  237. }
  238. /**
  239. * @dataProvider provideTestClassCases
  240. *
  241. * @param string $testClassName
  242. */
  243. public function testThatDataProvidersAreUsed($testClassName)
  244. {
  245. $reflectionClass = new \ReflectionClass($testClassName);
  246. $definedDataProviders = array_filter(
  247. $reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC),
  248. static function (\ReflectionMethod $reflectionMethod) use ($reflectionClass) {
  249. return $reflectionMethod->getDeclaringClass()->getName() === $reflectionClass->getName()
  250. && 'provide' === substr($reflectionMethod->getName(), 0, 7);
  251. }
  252. );
  253. if ([] === $definedDataProviders) {
  254. $this->addToAssertionCount(1); // no methods to test, all good!
  255. }
  256. $usedDataProviderMethodNames = $this->getUsedDataProviderMethodNames($testClassName);
  257. foreach ($definedDataProviders as $definedDataProvider) {
  258. static::assertContains(
  259. $definedDataProvider->getName(),
  260. $usedDataProviderMethodNames,
  261. sprintf('Data provider in "%s" with name "%s" is not used.', $definedDataProvider->getDeclaringClass()->getName(), $definedDataProvider->getName())
  262. );
  263. }
  264. }
  265. /**
  266. * @dataProvider provideTestClassCases
  267. *
  268. * @param string $testClassName
  269. */
  270. public function testThatTestClassCoversAreCorrect($testClassName)
  271. {
  272. $reflectionClass = new \ReflectionClass($testClassName);
  273. if ($reflectionClass->isAbstract() || $reflectionClass->isInterface()) {
  274. self::addToAssertionCount(1);
  275. return;
  276. }
  277. $doc = $reflectionClass->getDocComment();
  278. static::assertNotFalse($doc);
  279. if (1 === Preg::match('/@coversNothing/', $doc, $matches)) {
  280. self::addToAssertionCount(1);
  281. return;
  282. }
  283. $covers = Preg::match('/@covers (\S*)/', $doc, $matches);
  284. static::assertNotFalse($covers, sprintf('Missing @covers in PHPDoc of test class "%s".', $testClassName));
  285. array_shift($matches);
  286. $class = '\\'.str_replace('PhpCsFixer\Tests\\', 'PhpCsFixer\\', substr($testClassName, 0, -4));
  287. $parentClass = (new \ReflectionClass($class))->getParentClass();
  288. $parentClassName = false === $parentClass ? null : '\\'.$parentClass->getName();
  289. foreach ($matches as $match) {
  290. if ($match === $class || $parentClassName === $match) {
  291. $this->addToAssertionCount(1);
  292. continue;
  293. }
  294. static::fail(sprintf('Unexpected @covers "%s" for "%s".', $match, $testClassName));
  295. }
  296. }
  297. /**
  298. * @dataProvider provideClassesWherePregFunctionsAreForbiddenCases
  299. *
  300. * @param string $className
  301. */
  302. public function testThereIsNoPregFunctionUsedDirectly($className)
  303. {
  304. $rc = new \ReflectionClass($className);
  305. $tokens = Tokens::fromCode(file_get_contents($rc->getFileName()));
  306. $stringTokens = array_filter(
  307. $tokens->toArray(),
  308. static function (Token $token) {
  309. return $token->isGivenKind(T_STRING);
  310. }
  311. );
  312. $strings = array_map(
  313. static function (Token $token) {
  314. return $token->getContent();
  315. },
  316. $stringTokens
  317. );
  318. $strings = array_unique($strings);
  319. $message = sprintf('Class %s must not use preg_*, it shall use Preg::* instead.', $className);
  320. static::assertNotContains('preg_filter', $strings, $message);
  321. static::assertNotContains('preg_grep', $strings, $message);
  322. static::assertNotContains('preg_match', $strings, $message);
  323. static::assertNotContains('preg_match_all', $strings, $message);
  324. static::assertNotContains('preg_replace', $strings, $message);
  325. static::assertNotContains('preg_replace_callback', $strings, $message);
  326. static::assertNotContains('preg_split', $strings, $message);
  327. }
  328. /**
  329. * @dataProvider provideTestClassCases
  330. *
  331. * @param string $testClassName
  332. */
  333. public function testExpectedInputOrder($testClassName)
  334. {
  335. $reflectionClass = new \ReflectionClass($testClassName);
  336. $publicMethods = array_filter(
  337. $reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC),
  338. static function (\ReflectionMethod $reflectionMethod) use ($reflectionClass) {
  339. return $reflectionMethod->getDeclaringClass()->getName() === $reflectionClass->getName();
  340. }
  341. );
  342. if ([] === $publicMethods) {
  343. $this->addToAssertionCount(1); // no methods to test, all good!
  344. return;
  345. }
  346. /** @var \ReflectionMethod $method */
  347. foreach ($publicMethods as $method) {
  348. $parameters = $method->getParameters();
  349. if (\count($parameters) < 2) {
  350. $this->addToAssertionCount(1); // not enough parameters to test, all good!
  351. continue;
  352. }
  353. $expected = [
  354. 'expected' => false,
  355. 'input' => false,
  356. ];
  357. for ($i = \count($parameters) - 1; $i >= 0; --$i) {
  358. $name = $parameters[$i]->getName();
  359. if (isset($expected[$name])) {
  360. $expected[$name] = $i;
  361. }
  362. }
  363. $expected = array_filter($expected);
  364. if (\count($expected) < 2) {
  365. $this->addToAssertionCount(1); // not enough parameters to test, all good!
  366. continue;
  367. }
  368. static::assertLessThan(
  369. $expected['input'],
  370. $expected['expected'],
  371. sprintf('Public method "%s::%s" has parameter \'input\' before \'expected\'.', $reflectionClass->getName(), $method->getName())
  372. );
  373. }
  374. }
  375. /**
  376. * @dataProvider provideSrcClassCases
  377. * @dataProvider provideTestClassCases
  378. *
  379. * @param string $className
  380. */
  381. public function testAllCodeContainSingleClassy($className)
  382. {
  383. $headerTypes = [
  384. T_ABSTRACT,
  385. T_AS,
  386. T_COMMENT,
  387. T_DECLARE,
  388. T_DOC_COMMENT,
  389. T_FINAL,
  390. T_LNUMBER,
  391. T_NAMESPACE,
  392. T_NS_SEPARATOR,
  393. T_OPEN_TAG,
  394. T_STRING,
  395. T_USE,
  396. T_WHITESPACE,
  397. ];
  398. $rc = new \ReflectionClass($className);
  399. $file = $rc->getFileName();
  400. $tokens = Tokens::fromCode(file_get_contents($file));
  401. $isEvent = Event::class === $rc->getName(); // remove this exception when no longer needed
  402. $classyIndex = null;
  403. static::assertTrue($tokens->isAnyTokenKindsFound(Token::getClassyTokenKinds()), sprintf('File "%s" should contains a classy.', $file));
  404. foreach ($tokens as $index => $token) {
  405. if ($token->isClassy()) {
  406. $classyIndex = $index;
  407. break;
  408. }
  409. if (!$token->isGivenKind($headerTypes) && !$token->equalsAny([';', '=', '(', ')']) && !$isEvent) {
  410. static::fail(sprintf('File "%s" should only contains single classy, found "%s" @ %d.', $file, $token->toJson(), $index));
  411. }
  412. }
  413. static::assertNotNull($classyIndex, sprintf('File "%s" does not contain a classy.', $file));
  414. $nextTokenOfKind = $tokens->getNextTokenOfKind($classyIndex, ['{']);
  415. if (!\is_int($nextTokenOfKind)) {
  416. throw new \UnexpectedValueException('Classy without {} - braces.');
  417. }
  418. $classyEndIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $nextTokenOfKind);
  419. if ($isEvent) {
  420. static::assertNotNull($tokens->getNextNonWhitespace($classyEndIndex), sprintf('File "%s" should not only contains a single classy.', $file));
  421. } else {
  422. static::assertNull($tokens->getNextNonWhitespace($classyEndIndex), sprintf('File "%s" should only contains a single classy.', $file));
  423. }
  424. }
  425. public function provideSrcClassCases()
  426. {
  427. return array_map(
  428. static function ($item) {
  429. return [$item];
  430. },
  431. $this->getSrcClasses()
  432. );
  433. }
  434. public function provideSrcClassesNotAbuseInterfacesCases()
  435. {
  436. return array_map(
  437. static function ($item) {
  438. return [$item];
  439. },
  440. array_filter($this->getSrcClasses(), static function ($className) {
  441. $rc = new \ReflectionClass($className);
  442. $doc = false !== $rc->getDocComment()
  443. ? new DocBlock($rc->getDocComment())
  444. : null;
  445. if (
  446. $rc->isInterface()
  447. || ($doc && \count($doc->getAnnotationsOfType('internal')))
  448. || \in_array($className, [
  449. \PhpCsFixer\Finder::class,
  450. \PhpCsFixer\Tests\Test\AbstractFixerTestCase::class,
  451. \PhpCsFixer\Tests\Test\AbstractIntegrationTestCase::class,
  452. \PhpCsFixer\Tokenizer\Tokens::class,
  453. ], true)
  454. ) {
  455. return false;
  456. }
  457. $interfaces = $rc->getInterfaces();
  458. $interfacesCount = \count($interfaces);
  459. if (0 === $interfacesCount) {
  460. return false;
  461. }
  462. if (1 === $interfacesCount) {
  463. $interface = reset($interfaces);
  464. if ('Stringable' === $interface->getName()) {
  465. return false;
  466. }
  467. }
  468. return true;
  469. })
  470. );
  471. }
  472. public function provideSrcConcreteClassCases()
  473. {
  474. return array_map(
  475. static function ($item) { return [$item]; },
  476. array_filter(
  477. $this->getSrcClasses(),
  478. static function ($className) {
  479. $rc = new \ReflectionClass($className);
  480. return !$rc->isAbstract() && !$rc->isInterface();
  481. }
  482. )
  483. );
  484. }
  485. public function provideTestClassCases()
  486. {
  487. return array_map(
  488. static function ($item) {
  489. return [$item];
  490. },
  491. $this->getTestClasses()
  492. );
  493. }
  494. public function provideClassesWherePregFunctionsAreForbiddenCases()
  495. {
  496. return array_map(
  497. static function ($item) {
  498. return [$item];
  499. },
  500. array_filter(
  501. $this->getSrcClasses(),
  502. static function ($className) {
  503. return Preg::class !== $className;
  504. }
  505. )
  506. );
  507. }
  508. /**
  509. * @param string $className
  510. *
  511. * @dataProvider providePhpUnitFixerExtendsAbstractPhpUnitFixerCases
  512. */
  513. public function testPhpUnitFixerExtendsAbstractPhpUnitFixer($className)
  514. {
  515. $reflection = new \ReflectionClass($className);
  516. static::assertTrue($reflection->isSubclassOf(\PhpCsFixer\Fixer\AbstractPhpUnitFixer::class));
  517. }
  518. public function providePhpUnitFixerExtendsAbstractPhpUnitFixerCases()
  519. {
  520. $factory = new FixerFactory();
  521. $factory->registerBuiltInFixers();
  522. foreach ($factory->getFixers() as $fixer) {
  523. if (0 !== strpos($fixer->getName(), 'php_unit_')) {
  524. continue;
  525. }
  526. // this one fixes usage of PHPUnit classes
  527. if ($fixer instanceof \PhpCsFixer\Fixer\PhpUnit\PhpUnitNamespacedFixer) {
  528. continue;
  529. }
  530. if ($fixer instanceof \PhpCsFixer\AbstractProxyFixer) {
  531. continue;
  532. }
  533. yield [\get_class($fixer)];
  534. }
  535. }
  536. private function getUsedDataProviderMethodNames($testClassName)
  537. {
  538. $dataProviderMethodNames = [];
  539. $tokens = Tokens::fromCode(file_get_contents(
  540. str_replace('\\', \DIRECTORY_SEPARATOR, preg_replace('#^PhpCsFixer\\\Tests#', 'tests', $testClassName)).'.php'
  541. ));
  542. foreach ($tokens as $token) {
  543. if ($token->isGivenKind(T_DOC_COMMENT)) {
  544. $docBlock = new DocBlock($token->getContent());
  545. $dataProviderAnnotations = $docBlock->getAnnotationsOfType('dataProvider');
  546. foreach ($dataProviderAnnotations as $dataProviderAnnotation) {
  547. if (1 === preg_match('/@dataProvider\s+(?P<methodName>\w+)/', $dataProviderAnnotation->getContent(), $matches)) {
  548. $dataProviderMethodNames[] = $matches['methodName'];
  549. }
  550. }
  551. }
  552. }
  553. return array_unique($dataProviderMethodNames);
  554. }
  555. private function getSrcClasses()
  556. {
  557. static $classes;
  558. if (null !== $classes) {
  559. return $classes;
  560. }
  561. $finder = Finder::create()
  562. ->files()
  563. ->name('*.php')
  564. ->in(__DIR__.'/../../src')
  565. ->exclude([
  566. 'Resources',
  567. ])
  568. ;
  569. $classes = array_map(
  570. static function (SplFileInfo $file) {
  571. return sprintf(
  572. '%s\\%s%s%s',
  573. 'PhpCsFixer',
  574. strtr($file->getRelativePath(), \DIRECTORY_SEPARATOR, '\\'),
  575. $file->getRelativePath() ? '\\' : '',
  576. $file->getBasename('.'.$file->getExtension())
  577. );
  578. },
  579. iterator_to_array($finder, false)
  580. );
  581. sort($classes);
  582. return $classes;
  583. }
  584. private function getTestClasses()
  585. {
  586. static $classes;
  587. if (null !== $classes) {
  588. return $classes;
  589. }
  590. $finder = Finder::create()
  591. ->files()
  592. ->name('*.php')
  593. ->in(__DIR__.'/..')
  594. ->exclude([
  595. 'Fixtures',
  596. ])
  597. ;
  598. $classes = array_map(
  599. static function (SplFileInfo $file) {
  600. return sprintf(
  601. 'PhpCsFixer\\Tests\\%s%s%s',
  602. strtr($file->getRelativePath(), \DIRECTORY_SEPARATOR, '\\'),
  603. $file->getRelativePath() ? '\\' : '',
  604. $file->getBasename('.'.$file->getExtension())
  605. );
  606. },
  607. iterator_to_array($finder, false)
  608. );
  609. $classes = array_filter($classes, static function ($class) {
  610. return is_subclass_of($class, TestCase::class);
  611. });
  612. sort($classes);
  613. return $classes;
  614. }
  615. /**
  616. * @return string[]
  617. */
  618. private function getPublicMethodNames(\ReflectionClass $rc)
  619. {
  620. return array_map(
  621. static function (\ReflectionMethod $rm) {
  622. return $rm->getName();
  623. },
  624. $rc->getMethods(\ReflectionMethod::IS_PUBLIC)
  625. );
  626. }
  627. }