-
-
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
Add PhpdocFullyQualifiedTypesFixer #3644
Changes from all commits
a9f0ad9
99c4865
52ffca5
d72e310
5227508
076f5bb
bd09a2c
a2788c9
723e87a
1bddabf
2f8ca5a
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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of PHP CS Fixer. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* Dariusz Rumiński <dariusz.ruminski@gmail.com> | ||
* | ||
* 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\FixerDefinition\CodeSample; | ||
use PhpCsFixer\FixerDefinition\FixerDefinition; | ||
use PhpCsFixer\Tokenizer\Analyzer\Analysis\TypeAnalysis; | ||
use PhpCsFixer\Tokenizer\Resolver\TypeShortNameResolver; | ||
use PhpCsFixer\Tokenizer\Tokens; | ||
|
||
/** | ||
* @author Filippo Tessarotto <zoeslam@gmail.com> | ||
*/ | ||
final class PhpdocFullyQualifiedTypesFixer extends AbstractPhpdocTypesFixer | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getDefinition() | ||
{ | ||
return new FixerDefinition( | ||
'Transforms imported FQCN in docblocks to short version.', | ||
[new CodeSample('<?php | ||
|
||
use Foo\Bar; | ||
|
||
/** | ||
* @param \Foo\Bar $foo | ||
*/ | ||
function foo($foo) {} | ||
')] | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* Must run before GeneralPhpdocAnnotationRemoveFixer, NoBlankLinesAfterPhpdocFixer, NoEmptyPhpdocFixer, NoSuperfluousPhpdocTagsFixer, PhpdocAddMissingParamAnnotationFixer, PhpdocAlignFixer, PhpdocAlignFixer, PhpdocAlignFixer, PhpdocAnnotationWithoutDotFixer, PhpdocInlineTagFixer, PhpdocLineSpanFixer, PhpdocNoAccessFixer, PhpdocNoAliasTagFixer, PhpdocNoEmptyReturnFixer, PhpdocNoPackageFixer, PhpdocNoUselessInheritdocFixer, PhpdocOrderByValueFixer, PhpdocOrderFixer, PhpdocReturnSelfReferenceFixer, PhpdocSeparationFixer, PhpdocSingleLineVarSpacingFixer, PhpdocSummaryFixer, PhpdocToParamTypeFixer, PhpdocToReturnTypeFixer, PhpdocTrimConsecutiveBlankLineSeparationFixer, PhpdocTrimFixer, PhpdocTypesOrderFixer, PhpdocVarAnnotationCorrectOrderFixer, PhpdocVarWithoutNameFixer. | ||
* Must run after CommentToPhpdocFixer, PhpdocScalarFixer. | ||
*/ | ||
public function getPriority() | ||
{ | ||
/* | ||
* 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. | ||
*/ | ||
return 14; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function normalize(Tokens $tokens, $type) | ||
{ | ||
if (0 !== strpos($type, '\\')) { | ||
return $type; | ||
} | ||
|
||
// Dummy start and end indexes, we only need isReservedType result | ||
if ((new TypeAnalysis(substr($type, 1), 1, 2))->isReservedType()) { | ||
Slamdunk marked this conversation as resolved.
Show resolved
Hide resolved
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. since https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/pull/7188/files#diff-9eb8d1884736f293d0a3ea5127a6713fea7a744e293f41b48b263e910297a4bdR66 the |
||
return $type; | ||
} | ||
|
||
return (new TypeShortNameResolver())->resolve($tokens, $type); | ||
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. is there nice way to prevent creating a new instance of 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.
Moreover, 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. this is no longer true |
||
} | ||
} |
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.
is this a copy-paste-comment?
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 , I've started from
PhpdocScalarFixer
and it seemed to me a good idea to retain part of it.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.
ok, than we should back up this comments claim with priority test fixtures so we no longer need this comment 👍
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.
So, I've added all the priority tests that I've could imaging, and then had to delete most of them except two cause of a test allowing only meaningful priority tests 😃
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.
That is my concern 😃 so a lot PHPDoc fixer claim that they must run before a whole bunch of other fixers, yet creating a priority test to prove it seems not possible.