UnalignDoubleArrowFixer.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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\Fixer\ArrayNotation;
  12. use PhpCsFixer\AbstractFixer;
  13. use PhpCsFixer\Tokenizer\Token;
  14. use PhpCsFixer\Tokenizer\Tokens;
  15. /**
  16. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  17. */
  18. final class UnalignDoubleArrowFixer extends AbstractFixer
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function isCandidate(Tokens $tokens)
  24. {
  25. return $tokens->isTokenKindFound(T_DOUBLE_ARROW);
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function getDescription()
  31. {
  32. return 'Unalign double arrow symbols.';
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function fix(\SplFileInfo $file, Tokens $tokens)
  38. {
  39. foreach ($tokens as $index => $token) {
  40. if (!$token->isGivenKind(T_DOUBLE_ARROW)) {
  41. continue;
  42. }
  43. $this->fixWhitespace($tokens[$index - 1]);
  44. $this->fixWhitespace($tokens[$index + 1]);
  45. }
  46. }
  47. /**
  48. * If given token is a single line whitespace then fix it to be a single space.
  49. *
  50. * @param Token $token
  51. */
  52. private function fixWhitespace(Token $token)
  53. {
  54. if ($token->isWhitespace(" \t")) {
  55. $token->setContent(' ');
  56. }
  57. }
  58. }