-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #3881 Add PhpdocVarAnnotationCorrectOrderFixer (kubawerlos)
This PR was merged into the 2.14-dev branch. Discussion ---------- Add PhpdocVarAnnotationCorrectOrderFixer Closes #2159 Commits ------- 8e031b2 Add PhpdocVarAnnotationCorrectOrderFixer
- Loading branch information
Showing
5 changed files
with
239 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?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\AbstractFixer; | ||
use PhpCsFixer\FixerDefinition\CodeSample; | ||
use PhpCsFixer\FixerDefinition\FixerDefinition; | ||
use PhpCsFixer\Preg; | ||
use PhpCsFixer\Tokenizer\Token; | ||
use PhpCsFixer\Tokenizer\Tokens; | ||
|
||
/** | ||
* @author Kuba Werłos <werlos@gmail.com> | ||
*/ | ||
final class PhpdocVarAnnotationCorrectOrderFixer extends AbstractFixer | ||
{ | ||
public function getDefinition() | ||
{ | ||
return new FixerDefinition( | ||
'`@var` and `@type` annotations must have type and name in the correct order.', | ||
[new CodeSample('<?php | ||
/** @var $foo int */ | ||
$foo = 2 + 2; | ||
')] | ||
); | ||
} | ||
|
||
public function isCandidate(Tokens $tokens) | ||
{ | ||
return $tokens->isTokenKindFound(T_DOC_COMMENT); | ||
} | ||
|
||
protected function applyFix(\SplFileInfo $file, Tokens $tokens) | ||
{ | ||
foreach ($tokens as $index => $token) { | ||
if (!$token->isGivenKind(T_DOC_COMMENT)) { | ||
continue; | ||
} | ||
|
||
if (false === stripos($token->getContent(), '@var') && false === stripos($token->getContent(), '@type')) { | ||
continue; | ||
} | ||
|
||
$newContent = Preg::replace( | ||
'/(@(?:type|var)\s*)(\$\S+)(\s+)([^\$](?:[^<\s]|<[^>]*>)*)(\s|\*)/i', | ||
'$1$4$3$2$5', | ||
$token->getContent() | ||
); | ||
|
||
if ($newContent === $token->getContent()) { | ||
continue; | ||
} | ||
|
||
$tokens[$index] = new Token([$token->getId(), $newContent]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
tests/Fixer/Phpdoc/PhpdocVarAnnotationCorrectOrderFixerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
<?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\Tests\Fixer\Phpdoc; | ||
|
||
use PhpCsFixer\Tests\Test\AbstractFixerTestCase; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @author Kuba Werłos <werlos@gmail.com> | ||
* | ||
* @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocVarAnnotationCorrectOrderFixer | ||
*/ | ||
final class PhpdocVarAnnotationCorrectOrderFixerTest extends AbstractFixerTestCase | ||
{ | ||
/** | ||
* @param string $expected | ||
* @param null|string $input | ||
* | ||
* @dataProvider provideFixCases | ||
*/ | ||
public function testFix($expected, $input = null) | ||
{ | ||
$this->doTest($expected, $input); | ||
} | ||
|
||
public function provideFixCases() | ||
{ | ||
yield [ // It's @param, we care only about @var | ||
'<?php /** @param $foo Foo */', | ||
]; | ||
|
||
yield [ // This is already fine | ||
'<?php /** @var Foo $foo */ ', | ||
]; | ||
|
||
yield [ // What? Two variables, I'm not touching this | ||
'<?php /** @var $foo $bar */', | ||
]; | ||
|
||
yield [ // Two classes are not to touch either | ||
'<?php /** @var Foo Bar */', | ||
]; | ||
|
||
yield ['<?php /** @var */']; | ||
|
||
yield ['<?php /** @var $foo */']; | ||
|
||
yield ['<?php /** @var Bar */']; | ||
|
||
yield [ | ||
'<?php | ||
/** | ||
* @var Foo $foo | ||
* @var Bar $bar | ||
*/ | ||
', | ||
'<?php | ||
/** | ||
* @var $foo Foo | ||
* @var $bar Bar | ||
*/ | ||
', | ||
]; | ||
|
||
yield [ | ||
'<?php | ||
/** | ||
* @var Foo $foo Some description | ||
*/ | ||
', | ||
'<?php | ||
/** | ||
* @var $foo Foo Some description | ||
*/ | ||
', | ||
]; | ||
|
||
yield [ | ||
'<?php /** @var Foo $foo */', | ||
'<?php /** @var $foo Foo */', | ||
]; | ||
|
||
yield [ | ||
'<?php /** @type Foo $foo */', | ||
'<?php /** @type $foo Foo */', | ||
]; | ||
|
||
yield [ | ||
'<?php /** @var Foo $foo*/', | ||
'<?php /** @var $foo Foo*/', | ||
]; | ||
|
||
yield [ | ||
'<?php /** @var Foo[] $foos */', | ||
'<?php /** @var $foos Foo[] */', | ||
]; | ||
|
||
yield [ | ||
'<?php /** @Var Foo $foo */', | ||
'<?php /** @Var $foo Foo */', | ||
]; | ||
|
||
yield [ | ||
'<?php | ||
/** @var Foo|Bar|mixed|int $someWeirdLongNAME__123 */ | ||
', | ||
'<?php | ||
/** @var $someWeirdLongNAME__123 Foo|Bar|mixed|int */ | ||
', | ||
]; | ||
|
||
yield [ | ||
'<?php | ||
/** | ||
* @var Foo $bar long description | ||
* goes here | ||
*/ | ||
', | ||
'<?php | ||
/** | ||
* @var $bar Foo long description | ||
* goes here | ||
*/ | ||
', | ||
]; | ||
|
||
yield [ | ||
'<?php | ||
/** @var array<int, int> $foo */ | ||
', | ||
'<?php | ||
/** @var $foo array<int, int> */ | ||
', | ||
]; | ||
|
||
yield [ | ||
'<?php | ||
/** @var array<int, int> $foo Array of something */ | ||
', | ||
'<?php | ||
/** @var $foo array<int, int> Array of something */ | ||
', | ||
]; | ||
|
||
yield [ | ||
'<?php | ||
/** @var Foo|array<int, int>|null $foo */ | ||
', | ||
'<?php | ||
/** @var $foo Foo|array<int, int>|null */ | ||
', | ||
]; | ||
} | ||
} |