AbstractFixer.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /*
  3. * This file is part of the PHP CS utility.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Symfony\CS;
  11. /**
  12. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  13. */
  14. abstract class AbstractFixer implements FixerInterface
  15. {
  16. protected static function camelCaseToUnderscore($string)
  17. {
  18. return preg_replace_callback(
  19. '/(^|[a-z])([A-Z])/',
  20. function (array $matches) {
  21. return strtolower(strlen($matches[1]) ? $matches[1].'_'.$matches[2] : $matches[2]);
  22. },
  23. $string
  24. );
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function getLevel()
  30. {
  31. static $map = array(
  32. 'PSR0' => FixerInterface::PSR0_LEVEL,
  33. 'PSR1' => FixerInterface::PSR1_LEVEL,
  34. 'PSR2' => FixerInterface::PSR2_LEVEL,
  35. 'All' => FixerInterface::ALL_LEVEL,
  36. 'Contrib' => FixerInterface::CONTRIB_LEVEL,
  37. );
  38. $level = current(explode('\\', substr(get_called_class(), strlen(__NAMESPACE__.'\\Fixer\\'))));
  39. if (!isset($map[$level])) {
  40. throw new \LogicException("Can not determine Fixer level");
  41. }
  42. return $map[$level];
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function getName()
  48. {
  49. $nameParts = explode('\\', get_called_class());
  50. $name = substr(end($nameParts), 0, -strlen('Fixer'));
  51. return self::camelCaseToUnderscore($name);
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function getPriority()
  57. {
  58. return 0;
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function supports(\SplFileInfo $file)
  64. {
  65. return true;
  66. }
  67. }