CastSpacesFixer.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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\CastNotation;
  13. use PhpCsFixer\AbstractFixer;
  14. use PhpCsFixer\Fixer\ConfigurableFixerInterface;
  15. use PhpCsFixer\Fixer\ConfigurableFixerTrait;
  16. use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
  17. use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
  18. use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
  19. use PhpCsFixer\FixerDefinition\CodeSample;
  20. use PhpCsFixer\FixerDefinition\FixerDefinition;
  21. use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
  22. use PhpCsFixer\Tokenizer\Token;
  23. use PhpCsFixer\Tokenizer\Tokens;
  24. /**
  25. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  26. *
  27. * @implements ConfigurableFixerInterface<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration>
  28. *
  29. * @phpstan-type _AutogeneratedInputConfiguration array{
  30. * space?: 'none'|'single'
  31. * }
  32. * @phpstan-type _AutogeneratedComputedConfiguration array{
  33. * space: 'none'|'single'
  34. * }
  35. */
  36. final class CastSpacesFixer extends AbstractFixer implements ConfigurableFixerInterface
  37. {
  38. /** @use ConfigurableFixerTrait<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration> */
  39. use ConfigurableFixerTrait;
  40. private const INSIDE_CAST_SPACE_REPLACE_MAP = [
  41. ' ' => '',
  42. "\t" => '',
  43. "\n" => '',
  44. "\r" => '',
  45. "\0" => '',
  46. "\x0B" => '',
  47. ];
  48. public function getDefinition(): FixerDefinitionInterface
  49. {
  50. return new FixerDefinition(
  51. 'A single space or none should be between cast and variable.',
  52. [
  53. new CodeSample(
  54. "<?php\n\$bar = ( string ) \$a;\n\$foo = (int)\$b;\n"
  55. ),
  56. new CodeSample(
  57. "<?php\n\$bar = ( string ) \$a;\n\$foo = (int)\$b;\n",
  58. ['space' => 'single']
  59. ),
  60. new CodeSample(
  61. "<?php\n\$bar = ( string ) \$a;\n\$foo = (int) \$b;\n",
  62. ['space' => 'none']
  63. ),
  64. ]
  65. );
  66. }
  67. /**
  68. * {@inheritdoc}
  69. *
  70. * Must run after NoShortBoolCastFixer.
  71. */
  72. public function getPriority(): int
  73. {
  74. return -10;
  75. }
  76. public function isCandidate(Tokens $tokens): bool
  77. {
  78. return $tokens->isAnyTokenKindsFound(Token::getCastTokenKinds());
  79. }
  80. protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
  81. {
  82. foreach ($tokens as $index => $token) {
  83. if (!$token->isCast()) {
  84. continue;
  85. }
  86. $tokens[$index] = new Token([
  87. $token->getId(),
  88. strtr($token->getContent(), self::INSIDE_CAST_SPACE_REPLACE_MAP),
  89. ]);
  90. if ('single' === $this->configuration['space']) {
  91. // force single whitespace after cast token:
  92. if ($tokens[$index + 1]->isWhitespace(" \t")) {
  93. // - if next token is whitespaces that contains only spaces and tabs - override next token with single space
  94. $tokens[$index + 1] = new Token([T_WHITESPACE, ' ']);
  95. } elseif (!$tokens[$index + 1]->isWhitespace()) {
  96. // - if next token is not whitespaces that contains spaces, tabs and new lines - append single space to current token
  97. $tokens->insertAt($index + 1, new Token([T_WHITESPACE, ' ']));
  98. }
  99. continue;
  100. }
  101. // force no whitespace after cast token:
  102. if ($tokens[$index + 1]->isWhitespace()) {
  103. $tokens->clearAt($index + 1);
  104. }
  105. }
  106. }
  107. protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
  108. {
  109. return new FixerConfigurationResolver([
  110. (new FixerOptionBuilder('space', 'Spacing to apply between cast and variable.'))
  111. ->setAllowedValues(['none', 'single'])
  112. ->setDefault('single')
  113. ->getOption(),
  114. ]);
  115. }
  116. }