AbstractIncrementOperatorFixer.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Fixer;
  13. use PhpCsFixer\AbstractFixer;
  14. use PhpCsFixer\Tokenizer\Tokens;
  15. abstract class AbstractIncrementOperatorFixer extends AbstractFixer
  16. {
  17. final protected function findStart(Tokens $tokens, int $index): int
  18. {
  19. do {
  20. $index = $tokens->getPrevMeaningfulToken($index);
  21. $token = $tokens[$index];
  22. $blockType = Tokens::detectBlockType($token);
  23. if (null !== $blockType && !$blockType['isStart']) {
  24. $index = $tokens->findBlockStart($blockType['type'], $index);
  25. $token = $tokens[$index];
  26. }
  27. } while (!$token->equalsAny(['$', [T_VARIABLE]]));
  28. $prevIndex = $tokens->getPrevMeaningfulToken($index);
  29. $prevToken = $tokens[$prevIndex];
  30. if ($prevToken->equals('$')) {
  31. return $this->findStart($tokens, $index);
  32. }
  33. if ($prevToken->isObjectOperator()) {
  34. return $this->findStart($tokens, $prevIndex);
  35. }
  36. if ($prevToken->isGivenKind(T_PAAMAYIM_NEKUDOTAYIM)) {
  37. $prevPrevIndex = $tokens->getPrevMeaningfulToken($prevIndex);
  38. if (!$tokens[$prevPrevIndex]->isGivenKind([T_STATIC, T_STRING])) {
  39. return $this->findStart($tokens, $prevIndex);
  40. }
  41. $index = $tokens->getTokenNotOfKindsSibling($prevIndex, -1, [T_NS_SEPARATOR, T_STATIC, T_STRING]);
  42. $index = $tokens->getNextMeaningfulToken($index);
  43. }
  44. return $index;
  45. }
  46. }