1313namespace PhpCsFixer \Fixer \Phpdoc ;
1414
1515use PhpCsFixer \AbstractFixer ;
16+ use PhpCsFixer \Fixer \ConfigurationDefinitionFixerInterface ;
1617use PhpCsFixer \Fixer \WhitespacesAwareFixerInterface ;
18+ use PhpCsFixer \FixerConfiguration \FixerConfigurationResolver ;
19+ use PhpCsFixer \FixerConfiguration \FixerOptionBuilder ;
20+ use PhpCsFixer \FixerConfiguration \FixerOptionValidatorGenerator ;
1721use PhpCsFixer \FixerDefinition \CodeSample ;
1822use PhpCsFixer \FixerDefinition \FixerDefinition ;
1923use PhpCsFixer \Tokenizer \Tokens ;
2630 * @author Graham Campbell <[email protected] > 2731 * @author Dariusz Rumiński <[email protected] > 2832 */
29- final class PhpdocAlignFixer extends AbstractFixer implements WhitespacesAwareFixerInterface
33+ final class PhpdocAlignFixer extends AbstractFixer implements ConfigurationDefinitionFixerInterface, WhitespacesAwareFixerInterface
3034{
3135 private $ regex ;
3236 private $ regexCommentLine ;
3337
34- public function __construct ()
38+ private static $ alignableTags = [
39+ 'param ' ,
40+ 'property ' ,
41+ 'return ' ,
42+ 'throws ' ,
43+ 'type ' ,
44+ 'var ' ,
45+ ];
46+
47+ private static $ tagsWithName = [
48+ 'param ' ,
49+ 'property ' ,
50+ ];
51+
52+ /**
53+ * {@inheritdoc}
54+ */
55+ public function configure (array $ configuration = null )
3556 {
36- parent ::__construct ();
57+ parent ::configure ($ configuration );
58+
59+ $ tagsWithNameToAlign = array_intersect ($ this ->configuration ['tags ' ], self ::$ tagsWithName );
60+ $ tagsWithoutNameToAlign = array_diff ($ this ->configuration ['tags ' ], $ tagsWithNameToAlign );
3761
3862 $ indent = '(?P<indent>(?: {2}|\t)*) ' ;
3963 // e.g. @param <hint> <$var>
40- $ paramTag = '(?P<tag>param )\s+(?P<hint>[^$]+?)\s+(?P<var>(?:&|\.{3})?\$[^\s]+) ' ;
64+ $ tagsWithName = '(?P<tag> ' . implode ( ' | ' , $ tagsWithNameToAlign ). ' )\s+(?P<hint>[^$]+?)\s+(?P<var>(?:&|\.{3})?\$[^\s]+) ' ;
4165 // e.g. @return <hint>
42- $ otherTags = '(?P<tag2>return|throws|var|type )\s+(?P<hint2>[^\s]+?) ' ;
66+ $ tagsWithoutName = '(?P<tag2> ' . implode ( ' | ' , $ tagsWithoutNameToAlign ). ' )\s+(?P<hint2>[^\s]+?) ' ;
4367 // optional <desc>
4468 $ desc = '(?:\s+(?P<desc>\V*)) ' ;
4569
46- $ this ->regex = '/^ ' .$ indent .' \* @(?: ' .$ paramTag .'| ' .$ otherTags .') ' .$ desc .'\s*$/u ' ;
70+ $ this ->regex = '/^ ' .$ indent .' \* @(?: ' .$ tagsWithName .'| ' .$ tagsWithoutName .') ' .$ desc .'\s*$/u ' ;
4771 $ this ->regexCommentLine = '/^ ' .$ indent .' \*(?! @)(?:\s+(?P<desc>\V+))(?<!\*\/)$/u ' ;
4872 }
4973
@@ -53,7 +77,7 @@ public function __construct()
5377 public function getDefinition ()
5478 {
5579 return new FixerDefinition (
56- 'All items of the @param, @throws, @return, @var, and @type phpdoc tags must be aligned vertically. ' ,
80+ 'All items of the given phpdoc tags must be aligned vertically. ' ,
5781 [new CodeSample ('<?php
5882/**
5983 * @param EngineInterface $templating
@@ -101,6 +125,32 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
101125 }
102126 }
103127
128+ /**
129+ * {@inheritdoc}
130+ */
131+ protected function createConfigurationDefinition ()
132+ {
133+ $ generator = new FixerOptionValidatorGenerator ();
134+
135+ $ tags = new FixerOptionBuilder ('tags ' , 'The tags that should be aligned. ' );
136+ $ tags
137+ ->setAllowedTypes (['array ' ])
138+ ->setAllowedValues ([
139+ $ generator ->allowedValueIsSubsetOf (self ::$ alignableTags ),
140+ ])
141+ // By default, all tags apart from @property will be aligned for backwards compatibility
142+ ->setDefault ([
143+ 'param ' ,
144+ 'return ' ,
145+ 'throws ' ,
146+ 'type ' ,
147+ 'var ' ,
148+ ])
149+ ;
150+
151+ return new FixerConfigurationResolver ([$ tags ->getOption ()]);
152+ }
153+
104154 /**
105155 * Fix a given docblock.
106156 *
@@ -168,7 +218,10 @@ private function fixDocBlock($content)
168218 $ line =
169219 $ item ['indent ' ]
170220 .' * '
171- .str_repeat (' ' , $ tagMax + $ hintMax + $ varMax + ('param ' === $ currTag ? 3 : 2 ))
221+ .str_repeat (
222+ ' ' ,
223+ $ tagMax + $ hintMax + $ varMax + (in_array ($ currTag , self ::$ tagsWithName , true ) ? 3 : 2 )
224+ )
172225 .$ item ['desc ' ]
173226 .$ lineEnding ;
174227
0 commit comments