* Dariusz Rumiński * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Fixer\Phpdoc; use PhpCsFixer\AbstractPhpdocTypesFixer; use PhpCsFixer\Fixer\ConfigurableFixerInterface; use PhpCsFixer\FixerConfiguration\AllowedValueSubset; use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver; use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface; use PhpCsFixer\FixerConfiguration\FixerOptionBuilder; use PhpCsFixer\FixerDefinition\CodeSample; use PhpCsFixer\FixerDefinition\FixerDefinition; use PhpCsFixer\FixerDefinition\FixerDefinitionInterface; use PhpCsFixer\Preg; /** * @author Graham Campbell * @author Dariusz Rumiński */ final class PhpdocTypesFixer extends AbstractPhpdocTypesFixer implements ConfigurableFixerInterface { /** * Available types, grouped. * * @var array */ private const POSSIBLE_TYPES = [ 'simple' => [ 'array', 'bool', 'callable', 'float', 'int', 'iterable', 'null', 'object', 'string', ], 'alias' => [ 'boolean', 'double', 'integer', ], 'meta' => [ '$this', 'false', 'mixed', 'parent', 'resource', 'scalar', 'self', 'static', 'true', 'void', ], ]; /** @var array */ private array $typesSetToFix; public function configure(array $configuration): void { parent::configure($configuration); $typesToFix = array_merge(...array_map(static fn (string $group): array => self::POSSIBLE_TYPES[$group], $this->configuration['groups'])); $this->typesSetToFix = array_combine($typesToFix, array_fill(0, \count($typesToFix), true)); } public function getDefinition(): FixerDefinitionInterface { return new FixerDefinition( 'The correct case must be used for standard PHP types in PHPDoc.', [ new CodeSample( ' ['simple', 'alias']] ), ] ); } /** * {@inheritdoc} * * Must run before GeneralPhpdocAnnotationRemoveFixer, GeneralPhpdocTagRenameFixer, NoBlankLinesAfterPhpdocFixer, NoEmptyPhpdocFixer, NoSuperfluousPhpdocTagsFixer, PhpdocAddMissingParamAnnotationFixer, PhpdocAlignFixer, PhpdocArrayTypeFixer, PhpdocInlineTagNormalizerFixer, PhpdocLineSpanFixer, PhpdocListTypeFixer, PhpdocNoAccessFixer, PhpdocNoAliasTagFixer, PhpdocNoEmptyReturnFixer, PhpdocNoPackageFixer, PhpdocNoUselessInheritdocFixer, PhpdocOrderByValueFixer, PhpdocOrderFixer, PhpdocParamOrderFixer, PhpdocReadonlyClassCommentToKeywordFixer, PhpdocReturnSelfReferenceFixer, PhpdocScalarFixer, PhpdocSeparationFixer, PhpdocSingleLineVarSpacingFixer, PhpdocSummaryFixer, PhpdocTagCasingFixer, PhpdocTagTypeFixer, PhpdocToParamTypeFixer, PhpdocToPropertyTypeFixer, PhpdocToReturnTypeFixer, PhpdocTrimConsecutiveBlankLineSeparationFixer, PhpdocTrimFixer, PhpdocTypesOrderFixer, PhpdocVarAnnotationCorrectOrderFixer, PhpdocVarWithoutNameFixer. * Must run after PhpdocIndentFixer. */ public function getPriority(): int { /* * Should be run before all other docblock fixers apart from the * phpdoc_to_comment and phpdoc_indent fixer to make sure all fixers * apply correct indentation to new code they add. This should run * before alignment of params is done since this fixer might change * the type and thereby un-aligning the params. We also must run before * the phpdoc_scalar_fixer so that it can make changes after us. */ return 16; } protected function normalize(string $type): string { $typeLower = strtolower($type); if (isset($this->typesSetToFix[$typeLower])) { $type = $typeLower; } // normalize shape/callable/generic identifiers too // TODO parse them as inner types and this will be not needed then return Preg::replaceCallback( '/^(\??\s*)([^()[\]{}<>\'"]+)(?])/', fn ($matches) => $matches[1].$this->normalize($matches[2]).$matches[3], $type ); } protected function createConfigurationDefinition(): FixerConfigurationResolverInterface { $possibleGroups = array_keys(self::POSSIBLE_TYPES); return new FixerConfigurationResolver([ (new FixerOptionBuilder('groups', 'Type groups to fix.')) ->setAllowedTypes(['array']) ->setAllowedValues([new AllowedValueSubset($possibleGroups)]) ->setDefault($possibleGroups) ->getOption(), ]); } }