ConstantCaseFixer.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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\Casing;
  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. * Fixer for constants case.
  26. *
  27. * @author Pol Dellaiera <pol.dellaiera@protonmail.com>
  28. *
  29. * @implements ConfigurableFixerInterface<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration>
  30. *
  31. * @phpstan-type _AutogeneratedInputConfiguration array{
  32. * case?: 'lower'|'upper'
  33. * }
  34. * @phpstan-type _AutogeneratedComputedConfiguration array{
  35. * case: 'lower'|'upper'
  36. * }
  37. */
  38. final class ConstantCaseFixer extends AbstractFixer implements ConfigurableFixerInterface
  39. {
  40. /** @use ConfigurableFixerTrait<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration> */
  41. use ConfigurableFixerTrait;
  42. /**
  43. * Hold the function that will be used to convert the constants.
  44. *
  45. * @var callable
  46. */
  47. private $fixFunction;
  48. public function getDefinition(): FixerDefinitionInterface
  49. {
  50. return new FixerDefinition(
  51. 'The PHP constants `true`, `false`, and `null` MUST be written using the correct casing.',
  52. [
  53. new CodeSample("<?php\n\$a = FALSE;\n\$b = True;\n\$c = nuLL;\n"),
  54. new CodeSample("<?php\n\$a = FALSE;\n\$b = True;\n\$c = nuLL;\n", ['case' => 'upper']),
  55. ]
  56. );
  57. }
  58. public function isCandidate(Tokens $tokens): bool
  59. {
  60. return $tokens->isTokenKindFound(T_STRING);
  61. }
  62. protected function configurePostNormalisation(): void
  63. {
  64. if ('lower' === $this->configuration['case']) {
  65. $this->fixFunction = static fn (string $content): string => strtolower($content);
  66. }
  67. if ('upper' === $this->configuration['case']) {
  68. $this->fixFunction = static fn (string $content): string => strtoupper($content);
  69. }
  70. }
  71. protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
  72. {
  73. return new FixerConfigurationResolver([
  74. (new FixerOptionBuilder('case', 'Whether to use the `upper` or `lower` case syntax.'))
  75. ->setAllowedValues(['upper', 'lower'])
  76. ->setDefault('lower')
  77. ->getOption(),
  78. ]);
  79. }
  80. protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
  81. {
  82. static $forbiddenPrevKinds = null;
  83. if (null === $forbiddenPrevKinds) {
  84. $forbiddenPrevKinds = [
  85. T_DOUBLE_COLON,
  86. T_EXTENDS,
  87. T_IMPLEMENTS,
  88. T_INSTANCEOF,
  89. T_NAMESPACE,
  90. T_NEW,
  91. T_NS_SEPARATOR,
  92. ...Token::getObjectOperatorKinds(),
  93. ];
  94. }
  95. foreach ($tokens as $index => $token) {
  96. if (!$token->equalsAny([[T_STRING, 'true'], [T_STRING, 'false'], [T_STRING, 'null']], false)) {
  97. continue;
  98. }
  99. $prevIndex = $tokens->getPrevMeaningfulToken($index);
  100. if ($tokens[$prevIndex]->isGivenKind($forbiddenPrevKinds)) {
  101. continue;
  102. }
  103. $nextIndex = $tokens->getNextMeaningfulToken($index);
  104. if ($tokens[$nextIndex]->isGivenKind(T_PAAMAYIM_NEKUDOTAYIM) || $tokens[$nextIndex]->equalsAny(['='], false)) {
  105. continue;
  106. }
  107. if ($tokens[$prevIndex]->isGivenKind(T_CASE) && $tokens[$nextIndex]->equals(';')) {
  108. continue;
  109. }
  110. $tokens[$index] = new Token([$token->getId(), ($this->fixFunction)($token->getContent())]);
  111. }
  112. }
  113. }