Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dca89c0

Browse files
committed
feature #2649 PhpdocAlignFixer - make fixer configurable (ntzm)
This PR was merged into the 2.4-dev branch. Discussion ---------- PhpdocAlignFixer - make fixer configurable Leaves `@property` out by default for backwards compatibility. Addresses #1492 Commits ------- 085640a Make phpdoc_align configurable
2 parents ce3604a + 085640a commit dca89c0

3 files changed

Lines changed: 208 additions & 21 deletions

File tree

README.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -845,8 +845,12 @@ Choose from the list of available rules:
845845

846846
* **phpdoc_align** [@Symfony]
847847

848-
All items of the @param, @throws, @return, @var, and @type phpdoc tags
849-
must be aligned vertically.
848+
All items of the given phpdoc tags must be aligned vertically.
849+
850+
Configuration options:
851+
852+
- ``tags`` (``array``): the tags that should be aligned; defaults to ``['param',
853+
'return', 'throws', 'type', 'var']``
850854

851855
* **phpdoc_annotation_without_dot** [@Symfony]
852856

src/Fixer/Phpdoc/PhpdocAlignFixer.php

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
namespace PhpCsFixer\Fixer\Phpdoc;
1414

1515
use PhpCsFixer\AbstractFixer;
16+
use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
1617
use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
18+
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
19+
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
20+
use PhpCsFixer\FixerConfiguration\FixerOptionValidatorGenerator;
1721
use PhpCsFixer\FixerDefinition\CodeSample;
1822
use PhpCsFixer\FixerDefinition\FixerDefinition;
1923
use PhpCsFixer\Tokenizer\Token;
@@ -27,24 +31,44 @@
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

Comments
 (0)