TestCaseUtils.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. declare(strict_types=1);
  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. namespace PhpCsFixer\Tests\Test;
  13. use PhpCsFixer\Fixer\FixerInterface;
  14. use PhpCsFixer\FixerFactory;
  15. /**
  16. * @internal
  17. */
  18. final class TestCaseUtils
  19. {
  20. /**
  21. * @param iterable<array{0: string, 1?: string}> $cases
  22. *
  23. * @return iterable<array{0: string, 1?: string}>
  24. */
  25. public static function swapExpectedInputTestCases(iterable $cases): iterable
  26. {
  27. foreach ($cases as $case) {
  28. if (1 === \count($case)) {
  29. yield $case;
  30. continue;
  31. }
  32. [$case[0], $case[1]] = [$case[1], $case[0]];
  33. yield $case;
  34. }
  35. }
  36. public static function getFixerByName(string $name): FixerInterface
  37. {
  38. static $fixers = null;
  39. if (null === $fixers) {
  40. $factory = new FixerFactory();
  41. $factory->registerBuiltInFixers();
  42. $fixers = [];
  43. foreach ($factory->getFixers() as $fixer) {
  44. $fixers[$fixer->getName()] = $fixer;
  45. }
  46. }
  47. if (!\array_key_exists($name, $fixers)) {
  48. throw new \InvalidArgumentException(\sprintf('Fixer "%s" does not exist.', $name));
  49. }
  50. return $fixers[$name];
  51. }
  52. }