Skip to content

Commit 8600801

Browse files
authored
Add PhpdocVarAnnotationCorrectOrderFixer
1 parent 3cf9dc0 commit 8600801

File tree

4 files changed

+166
-0
lines changed

4 files changed

+166
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# CHANGELOG for PHP CS Fixer: custom fixers
22

3+
## [Unreleased]
4+
- Add PhpdocVarAnnotationCorrectOrderFixer
5+
36
## v1.2.0 - 2018-06-03
47
- Add PhpdocNoIncorrectVarAnnotationFixer
58

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ In your PHP CS Fixer configuration register fixers and use them:
109109

110110
```
111111

112+
- **PhpdocVarAnnotationCorrectOrderFixer** - in `@var` type and variable must be in correct order.
113+
```diff
114+
<?php
115+
-/** @var $foo int */
116+
+/** @var int $foo */
117+
$foo = 2 + 2;
118+
119+
```
120+
112121

113122
## Contributing
114123
Request a feature or report a bug by creating [issue](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/issues).
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace PhpCsFixerCustomFixers\Fixer;
6+
7+
use PhpCsFixer\FixerDefinition\CodeSample;
8+
use PhpCsFixer\FixerDefinition\FixerDefinition;
9+
use PhpCsFixer\Tokenizer\Token;
10+
use PhpCsFixer\Tokenizer\Tokens;
11+
12+
final class PhpdocVarAnnotationCorrectOrderFixer extends AbstractFixer
13+
{
14+
public function getDefinition() : FixerDefinition
15+
{
16+
return new FixerDefinition(
17+
'In `@var` type and variable must be in correct order.',
18+
[new CodeSample('<?php
19+
/** @var $foo int */
20+
$foo = 2 + 2;
21+
')]
22+
);
23+
}
24+
25+
public function isCandidate(Tokens $tokens) : bool
26+
{
27+
return $tokens->isAnyTokenKindsFound([T_COMMENT, T_DOC_COMMENT]);
28+
}
29+
30+
public function fix(\SplFileInfo $file, Tokens $tokens) : void
31+
{
32+
foreach ($tokens as $index => $token) {
33+
if (!$token->isGivenKind([T_COMMENT, T_DOC_COMMENT])) {
34+
continue;
35+
}
36+
37+
if (\stripos($token->getContent(), '@var') === false) {
38+
continue;
39+
}
40+
41+
$newContent = \preg_replace(
42+
'/(@var\s*)(\$\S+)(\s+)(\S+)\b/i',
43+
'$1$4$3$2',
44+
$token->getContent()
45+
);
46+
47+
if ($newContent === $token->getContent()) {
48+
continue;
49+
}
50+
51+
$tokens[$index] = new Token([$token->getId(), $newContent]);
52+
}
53+
}
54+
55+
public function getPriority() : int
56+
{
57+
return 0;
58+
}
59+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Tests\Fixer;
6+
7+
/**
8+
* @internal
9+
*
10+
* @covers \PhpCsFixerCustomFixers\Fixer\PhpdocVarAnnotationCorrectOrderFixer
11+
*/
12+
final class PhpdocVarAnnotationCorrectOrderFixerTest extends AbstractFixerTestCase
13+
{
14+
public function testPriority() : void
15+
{
16+
static::assertSame(0, $this->fixer->getPriority());
17+
}
18+
19+
/**
20+
* @param string $expected
21+
* @param string|null $input
22+
*
23+
* @dataProvider provideFixCases
24+
*/
25+
public function testFix(string $expected, string $input = null) : void
26+
{
27+
$this->doTest($expected, $input);
28+
}
29+
30+
public function provideFixCases() : \Iterator
31+
{
32+
yield [
33+
'<?php
34+
/** @param $foo Foo */
35+
',
36+
];
37+
38+
yield [
39+
'<?php
40+
/** @var Foo $foo */
41+
',
42+
];
43+
44+
yield [
45+
'<?php
46+
/**
47+
* @var Foo $foo
48+
* @var Bar $bar
49+
*/
50+
',
51+
'<?php
52+
/**
53+
* @var $foo Foo
54+
* @var $bar Bar
55+
*/
56+
',
57+
];
58+
59+
yield [
60+
'<?php
61+
/** @var Foo $foo */
62+
',
63+
'<?php
64+
/** @var $foo Foo */
65+
',
66+
];
67+
68+
yield [
69+
'<?php
70+
/**@var Foo $foo*/
71+
',
72+
'<?php
73+
/**@var $foo Foo*/
74+
',
75+
];
76+
77+
yield [
78+
'<?php
79+
/** @Var Foo $foo */
80+
',
81+
'<?php
82+
/** @Var $foo Foo */
83+
',
84+
];
85+
86+
yield [
87+
'<?php
88+
/** @var Foo|Bar|mixed|int $someWeirdLongNAME__123 */
89+
',
90+
'<?php
91+
/** @var $someWeirdLongNAME__123 Foo|Bar|mixed|int */
92+
',
93+
];
94+
}
95+
}

0 commit comments

Comments
 (0)