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 \Token ;
2731 * @author Graham Campbell <[email protected] > 2832 * @author Dariusz Rumiński <[email protected] > 2933 */
30- final class PhpdocAlignFixer extends AbstractFixer implements WhitespacesAwareFixerInterface
34+ final class PhpdocAlignFixer extends AbstractFixer implements ConfigurationDefinitionFixerInterface, WhitespacesAwareFixerInterface
3135{
3236 private $ regex ;
3337 private $ regexCommentLine ;
3438
35- public function __construct ()
39+ private static $ alignableTags = [
40+ 'param ' ,
41+ 'property ' ,
42+ 'return ' ,
43+ 'throws ' ,
44+ 'type ' ,
45+ 'var ' ,
46+ ];
47+
48+ private static $ tagsWithName = [
49+ 'param ' ,
50+ 'property ' ,
51+ ];
52+
53+ /**
54+ * {@inheritdoc}
55+ */
56+ public function configure (array $ configuration = null )
3657 {
37- parent ::__construct ();
58+ parent ::configure ($ configuration );
59+
60+ $ tagsWithNameToAlign = array_intersect ($ this ->configuration ['tags ' ], self ::$ tagsWithName );
61+ $ tagsWithoutNameToAlign = array_diff ($ this ->configuration ['tags ' ], $ tagsWithNameToAlign );
3862
3963 $ indent = '(?P<indent>(?: {2}|\t)*) ' ;
4064 // e.g. @param <hint> <$var>
41- $ paramTag = '(?P<tag>param )\s+(?P<hint>[^$]+?)\s+(?P<var>(?:&|\.{3})?\$[^\s]+) ' ;
65+ $ tagsWithName = '(?P<tag> ' . implode ( ' | ' , $ tagsWithNameToAlign ). ' )\s+(?P<hint>[^$]+?)\s+(?P<var>(?:&|\.{3})?\$[^\s]+) ' ;
4266 // e.g. @return <hint>
43- $ otherTags = '(?P<tag2>return|throws|var|type )\s+(?P<hint2>[^\s]+?) ' ;
67+ $ tagsWithoutName = '(?P<tag2> ' . implode ( ' | ' , $ tagsWithoutNameToAlign ). ' )\s+(?P<hint2>[^\s]+?) ' ;
4468 // optional <desc>
4569 $ desc = '(?:\s+(?P<desc>\V*)) ' ;
4670
47- $ this ->regex = '/^ ' .$ indent .' \* @(?: ' .$ paramTag .'| ' .$ otherTags .') ' .$ desc .'\s*$/u ' ;
71+ $ this ->regex = '/^ ' .$ indent .' \* @(?: ' .$ tagsWithName .'| ' .$ tagsWithoutName .') ' .$ desc .'\s*$/u ' ;
4872 $ this ->regexCommentLine = '/^ ' .$ indent .' \*(?! @)(?:\s+(?P<desc>\V+))(?<!\*\/)$/u ' ;
4973 }
5074
@@ -54,7 +78,7 @@ public function __construct()
5478 public function getDefinition ()
5579 {
5680 return new FixerDefinition (
57- 'All items of the @param, @throws, @return, @var, and @type phpdoc tags must be aligned vertically. ' ,
81+ 'All items of the given phpdoc tags must be aligned vertically. ' ,
5882 [new CodeSample ('<?php
5983/**
6084 * @param EngineInterface $templating
@@ -102,6 +126,32 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
102126 }
103127 }
104128
129+ /**
130+ * {@inheritdoc}
131+ */
132+ protected function createConfigurationDefinition ()
133+ {
134+ $ generator = new FixerOptionValidatorGenerator ();
135+
136+ $ tags = new FixerOptionBuilder ('tags ' , 'The tags that should be aligned. ' );
137+ $ tags
138+ ->setAllowedTypes (['array ' ])
139+ ->setAllowedValues ([
140+ $ generator ->allowedValueIsSubsetOf (self ::$ alignableTags ),
141+ ])
142+ // By default, all tags apart from @property will be aligned for backwards compatibility
143+ ->setDefault ([
144+ 'param ' ,
145+ 'return ' ,
146+ 'throws ' ,
147+ 'type ' ,
148+ 'var ' ,
149+ ])
150+ ;
151+
152+ return new FixerConfigurationResolver ([$ tags ->getOption ()]);
153+ }
154+
105155 /**
106156 * @param string $content
107157 *
@@ -167,7 +217,10 @@ private function fixDocBlock($content)
167217 $ line =
168218 $ item ['indent ' ]
169219 .' * '
170- .str_repeat (' ' , $ tagMax + $ hintMax + $ varMax + ('param ' === $ currTag ? 3 : 2 ))
220+ .str_repeat (
221+ ' ' ,
222+ $ tagMax + $ hintMax + $ varMax + (in_array ($ currTag , self ::$ tagsWithName , true ) ? 3 : 2 )
223+ )
171224 .$ item ['desc ' ]
172225 .$ lineEnding ;
173226
0 commit comments