UnaryOperatorSpacesFixer.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. use PhpCsFixer\Tokenizer\TokensAnalyzer;
  15. /**
  16. * @author Gregor Harlan <gharlan@web.de>
  17. */
  18. final class UnaryOperatorSpacesFixer extends AbstractFixer
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function isCandidate(Tokens $tokens)
  24. {
  25. return true;
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function fix(\SplFileInfo $file, Tokens $tokens)
  31. {
  32. $tokensAnalyzer = new TokensAnalyzer($tokens);
  33. for ($index = $tokens->count() - 1; $index >= 0; --$index) {
  34. if ($tokensAnalyzer->isUnarySuccessorOperator($index)) {
  35. $tokens->removeLeadingWhitespace($index);
  36. continue;
  37. }
  38. if ($tokensAnalyzer->isUnaryPredecessorOperator($index)) {
  39. $tokens->removeTrailingWhitespace($index);
  40. continue;
  41. }
  42. }
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function getDescription()
  48. {
  49. return 'Unary operators should be placed adjacent to their operands.';
  50. }
  51. }