ConcatWithoutSpacesFixer.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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\Operator;
  12. use PhpCsFixer\AbstractFixer;
  13. use PhpCsFixer\Tokenizer\Tokens;
  14. /**
  15. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  16. */
  17. final class ConcatWithoutSpacesFixer extends AbstractFixer
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function isCandidate(Tokens $tokens)
  23. {
  24. return $tokens->isTokenKindFound('.');
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function fix(\SplFileInfo $file, Tokens $tokens)
  30. {
  31. foreach ($tokens as $index => $token) {
  32. if (!$token->equals('.')) {
  33. continue;
  34. }
  35. if (!$tokens[$tokens->getPrevNonWhitespace($index)]->isGivenKind(T_LNUMBER)) {
  36. $tokens->removeLeadingWhitespace($index, " \t");
  37. }
  38. if (!$tokens[$tokens->getNextNonWhitespace($index)]->isGivenKind(array(T_LNUMBER, T_COMMENT, T_DOC_COMMENT))) {
  39. $tokens->removeTrailingWhitespace($index, " \t");
  40. }
  41. }
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function getDescription()
  47. {
  48. return 'Concatenation should be used without spaces.';
  49. }
  50. }