ObjectOperatorWithoutWhitespaceFixer.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 Fabien Potencier <fabien@symfony.com>
  16. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  17. */
  18. final class ObjectOperatorWithoutWhitespaceFixer extends AbstractFixer
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function isCandidate(Tokens $tokens)
  24. {
  25. return $tokens->isTokenKindFound(T_OBJECT_OPERATOR);
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function fix(\SplFileInfo $file, Tokens $tokens)
  31. {
  32. // [Structure] there should not be space before or after T_OBJECT_OPERATOR
  33. foreach ($tokens as $index => $token) {
  34. if (!$token->isGivenKind(T_OBJECT_OPERATOR)) {
  35. continue;
  36. }
  37. // clear whitespace before ->
  38. if ($tokens[$index - 1]->isWhitespace(" \t") && !$tokens[$index - 2]->isComment()) {
  39. $tokens[$index - 1]->clear();
  40. }
  41. // clear whitespace after ->
  42. if ($tokens[$index + 1]->isWhitespace(" \t") && !$tokens[$index + 2]->isComment()) {
  43. $tokens[$index + 1]->clear();
  44. }
  45. }
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function getDescription()
  51. {
  52. return 'There should not be space before or after object T_OBJECT_OPERATOR.';
  53. }
  54. }