PhpdocScalarFixer.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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\Phpdoc;
  13. use PhpCsFixer\AbstractPhpdocTypesFixer;
  14. use PhpCsFixer\Fixer\ConfigurableFixerInterface;
  15. use PhpCsFixer\FixerConfiguration\AllowedValueSubset;
  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. /**
  23. * @author Graham Campbell <hello@gjcampbell.co.uk>
  24. */
  25. final class PhpdocScalarFixer extends AbstractPhpdocTypesFixer implements ConfigurableFixerInterface
  26. {
  27. /**
  28. * The types to fix.
  29. *
  30. * @var array<string, string>
  31. */
  32. private static array $types = [
  33. 'boolean' => 'bool',
  34. 'callback' => 'callable',
  35. 'double' => 'float',
  36. 'integer' => 'int',
  37. 'real' => 'float',
  38. 'str' => 'string',
  39. ];
  40. public function getDefinition(): FixerDefinitionInterface
  41. {
  42. return new FixerDefinition(
  43. 'Scalar types should always be written in the same form. `int` not `integer`, `bool` not `boolean`, `float` not `real` or `double`.',
  44. [
  45. new CodeSample('<?php
  46. /**
  47. * @param integer $a
  48. * @param boolean $b
  49. * @param real $c
  50. *
  51. * @return double
  52. */
  53. function sample($a, $b, $c)
  54. {
  55. return sample2($a, $b, $c);
  56. }
  57. '),
  58. new CodeSample(
  59. '<?php
  60. /**
  61. * @param integer $a
  62. * @param boolean $b
  63. * @param real $c
  64. */
  65. function sample($a, $b, $c)
  66. {
  67. return sample2($a, $b, $c);
  68. }
  69. ',
  70. ['types' => ['boolean']]
  71. ),
  72. ]
  73. );
  74. }
  75. /**
  76. * {@inheritdoc}
  77. *
  78. * Must run before GeneralPhpdocAnnotationRemoveFixer, GeneralPhpdocTagRenameFixer, NoBlankLinesAfterPhpdocFixer, NoEmptyPhpdocFixer, NoSuperfluousPhpdocTagsFixer, PhpdocAddMissingParamAnnotationFixer, PhpdocAlignFixer, PhpdocArrayTypeFixer, PhpdocInlineTagNormalizerFixer, PhpdocLineSpanFixer, PhpdocListTypeFixer, PhpdocNoAccessFixer, PhpdocNoAliasTagFixer, PhpdocNoEmptyReturnFixer, PhpdocNoPackageFixer, PhpdocNoUselessInheritdocFixer, PhpdocOrderByValueFixer, PhpdocOrderFixer, PhpdocParamOrderFixer, PhpdocReadonlyClassCommentToKeywordFixer, PhpdocReturnSelfReferenceFixer, PhpdocSeparationFixer, PhpdocSingleLineVarSpacingFixer, PhpdocSummaryFixer, PhpdocTagCasingFixer, PhpdocTagTypeFixer, PhpdocToParamTypeFixer, PhpdocToPropertyTypeFixer, PhpdocToReturnTypeFixer, PhpdocTrimConsecutiveBlankLineSeparationFixer, PhpdocTrimFixer, PhpdocTypesOrderFixer, PhpdocVarAnnotationCorrectOrderFixer, PhpdocVarWithoutNameFixer.
  79. * Must run after PhpdocTypesFixer.
  80. */
  81. public function getPriority(): int
  82. {
  83. /*
  84. * Should be run before all other docblock fixers apart from the
  85. * phpdoc_to_comment and phpdoc_indent fixer to make sure all fixers
  86. * apply correct indentation to new code they add. This should run
  87. * before alignment of params is done since this fixer might change
  88. * the type and thereby un-aligning the params. We also must run after
  89. * the phpdoc_types_fixer because it can convert types to things that
  90. * we can fix.
  91. */
  92. return 15;
  93. }
  94. protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
  95. {
  96. $types = array_keys(self::$types);
  97. return new FixerConfigurationResolver([
  98. (new FixerOptionBuilder('types', 'A list of types to fix.'))
  99. ->setAllowedValues([new AllowedValueSubset($types)])
  100. ->setDefault($types)
  101. ->getOption(),
  102. ]);
  103. }
  104. protected function normalize(string $type): string
  105. {
  106. $suffix = '';
  107. while (str_ends_with($type, '[]')) {
  108. $type = substr($type, 0, -2);
  109. $suffix .= '[]';
  110. }
  111. if (\in_array($type, $this->configuration['types'], true)) {
  112. $type = self::$types[$type];
  113. }
  114. return $type.$suffix;
  115. }
  116. }