-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Configurable phpDoc tags for FQCN processing #7628
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -188,6 +188,34 @@ protected function createConfigurationDefinition(): FixerConfigurationResolverIn | |
->setAllowedTypes(['bool']) | ||
->setDefault(false) | ||
->getOption(), | ||
(new FixerOptionBuilder( | ||
'phpdoc_tags', | ||
'Collection of PHPDoc annotation tags where FQCNs should be processed. As of now only simple tags with `@tag \F\Q\C\N` format are supported (no complex types).' | ||
)) | ||
->setAllowedTypes(['array']) | ||
->setDefault([ | ||
'param', | ||
'phpstan-param', | ||
'phpstan-property', | ||
'phpstan-property-read', | ||
'phpstan-property-write', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what would be the case of imagine someone actually use the config, phpstan introducing new annotation - they need to manually add the annotation here. (same for others) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are more Maybe we can focus on regular phpDoc tags, and automatically add prefixed ones 🤔? Like when someone includes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. like, tbh I see only 2 cases myself - want to process annotations (any of them) or not. boolean config - do we have request to go granular and support some, but not all annotations? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For example |
||
'phpstan-return', | ||
'phpstan-var', | ||
'property', | ||
'property-read', | ||
'property-write', | ||
'psalm-param', | ||
'psalm-property', | ||
'psalm-property-read', | ||
'psalm-property-write', | ||
'psalm-return', | ||
'psalm-var', | ||
'return', | ||
'see', | ||
'throws', | ||
'var', | ||
]) | ||
->getOption(), | ||
]); | ||
} | ||
|
||
|
@@ -260,13 +288,19 @@ private function fixFunction(FunctionsAnalyzer $functionsAnalyzer, Tokens $token | |
*/ | ||
private function fixPhpDoc(Tokens $tokens, int $index, array $uses, string $namespaceName): void | ||
{ | ||
$allowedTags = $this->configuration['phpdoc_tags']; | ||
|
||
if ([] === $allowedTags) { | ||
return; | ||
} | ||
|
||
$phpDoc = $tokens[$index]; | ||
$phpDocContent = $phpDoc->getContent(); | ||
Preg::matchAll('#@([^\s]+)\s+([^\s]+)#', $phpDocContent, $matches); | ||
|
||
if ([] !== $matches) { | ||
foreach ($matches[2] as $i => $typeName) { | ||
if (!\in_array($matches[1][$i], ['param', 'return', 'see', 'throws', 'var'], true)) { | ||
if (!\in_array($matches[1][$i], $allowedTags, true)) { | ||
continue; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, I would personally remove
@see
which is required to be FQCN in some coding standards and very often is not a FQCN alone, it is very often a method name, which is not a "type" which this fixer is about.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but as a second condition fixer matches simple FQCNs with a regex, so URLs or methods won't be processed 🙂.