Skip to content

Commit e513184

Browse files
authored
Remove @var without type at the beginning in PhpdocNoIncorrectVarAnnotationFixer (#21)
* Remove @var without type at the beginning in PhpdocNoIncorrectVarAnnotationFixer * Travis fix * Travis fix
1 parent 93365f7 commit e513184

File tree

6 files changed

+105
-26
lines changed

6 files changed

+105
-26
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ jobs:
6767
- cp tests/Fixer/* tmp/tests/Fixer/CustomFixers
6868
- rm tmp/tests/Fixer/CustomFixers/AbstractFixerTestCase.php
6969
- sed -i -- 's/namespace Tests\\Fixer;/namespace PhpCsFixer\\Tests\\Fixer\\CustomFixers;use PhpCsFixer\\Tests\\Test\\AbstractFixerTestCase;/g' tmp/tests/Fixer/CustomFixers/*
70-
- sed -i -- 's/@covers \\PhpCsFixerCustomFixers\\Fixer/@covers \\PhpCsFixer\\Fixer\CustomFixers/g' tmp/tests/Fixer/CustomFixers/*
70+
- sed -i -- 's/PhpCsFixerCustomFixers\\Fixer/PhpCsFixer\\Fixer\\CustomFixers/g' tmp/tests/Fixer/CustomFixers/*
7171
# prepare for testing
7272
- cd tmp
7373
- php php-cs-fixer fix src/Fixer/CustomFixers

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## [Unreleased]
44
- Add PhpdocVarAnnotationCorrectOrderFixer
5+
- Remove @var without type at the beginning in PhpdocNoIncorrectVarAnnotationFixer
56

67
## v1.2.0 - 2018-06-03
78
- Add PhpdocNoIncorrectVarAnnotationFixer

src/Fixer/PhpdocNoIncorrectVarAnnotationFixer.php

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,13 @@ public function fix(\SplFileInfo $file, Tokens $tokens) : void
3939
continue;
4040
}
4141

42+
// remove ones not having type at the beginning
43+
$this->removeVarAnnotationNotMatchingPattern($tokens, $index, '/@var\s+[\\\\a-zA-Z_\x7f-\xff]/');
44+
4245
$nextIndex = $tokens->getNextMeaningfulToken($index);
4346

4447
if ($nextIndex === null) {
45-
$this->removeVarAnnotation($tokens, $index);
48+
$this->removeVarAnnotationNotMatchingPattern($tokens, $index, null);
4649
continue;
4750
}
4851

@@ -60,37 +63,32 @@ public function fix(\SplFileInfo $file, Tokens $tokens) : void
6063
continue;
6164
}
6265

63-
$this->removeVarAnnotation($tokens, $index);
66+
$this->removeVarAnnotationNotMatchingPattern($tokens, $index, null);
6467
}
6568
}
6669

6770
public function getPriority() : int
6871
{
69-
// should be run before NoEmptyCommentFixer, NoEmptyPhpdocFixer, NoExtraBlankLinesFixer, NoTrailingWhitespaceFixer, NoUnusedImportsFixer and NoWhitespaceInBlankLineFixer
72+
// must be run before NoEmptyCommentFixer, NoEmptyPhpdocFixer, NoExtraBlankLinesFixer, NoTrailingWhitespaceFixer, NoUnusedImportsFixer and NoWhitespaceInBlankLineFixer
73+
// must be after PhpdocVarAnnotationCorrectOrderFixer
7074
return 6;
7175
}
7276

73-
private function removeVarAnnotation(Tokens $tokens, int $index, array $allowedVariables = []) : void
77+
private function removeVarAnnotation(Tokens $tokens, int $index, array $allowedVariables) : void
7478
{
75-
$doc = new DocBlock($tokens[$index]->getContent());
76-
77-
foreach ($doc->getAnnotationsOfType(['var']) as $annotation) {
78-
$allowedVariablesQuoted = \array_map(
79-
static function (string $variable) : string {
80-
return \preg_quote($variable, '/') . '\b';
81-
},
82-
$allowedVariables
83-
);
84-
if ($allowedVariables === [] || \preg_match(\sprintf('/%s/i', \implode('|', $allowedVariablesQuoted)), $annotation->getContent()) !== 1) {
85-
$annotation->remove();
86-
}
87-
}
88-
89-
if ($doc->getContent() === '') {
90-
$tokens->clearTokenAndMergeSurroundingWhitespace($index);
91-
} else {
92-
$tokens[$index] = new Token([$tokens[$index]->getId(), $doc->getContent()]);
93-
}
79+
$this->removeVarAnnotationNotMatchingPattern(
80+
$tokens,
81+
$index,
82+
'/' . \implode(
83+
'|',
84+
\array_map(
85+
static function (string $variable) : string {
86+
return \preg_quote($variable, '/') . '\b';
87+
},
88+
$allowedVariables
89+
)
90+
) . '/i'
91+
);
9492
}
9593

9694
private function removeVarAnnotationForControl(Tokens $tokens, int $commentIndex, int $controlIndex) : void
@@ -110,4 +108,21 @@ private function removeVarAnnotationForControl(Tokens $tokens, int $commentIndex
110108

111109
$this->removeVarAnnotation($tokens, $commentIndex, $variables);
112110
}
111+
112+
private function removeVarAnnotationNotMatchingPattern(Tokens $tokens, int $index, ?string $pattern) : void
113+
{
114+
$doc = new DocBlock($tokens[$index]->getContent());
115+
116+
foreach ($doc->getAnnotationsOfType(['var']) as $annotation) {
117+
if ($pattern === null || \preg_match($pattern, $annotation->getContent()) !== 1) {
118+
$annotation->remove();
119+
}
120+
}
121+
122+
if ($doc->getContent() === '') {
123+
$tokens->clearTokenAndMergeSurroundingWhitespace($index);
124+
} else {
125+
$tokens[$index] = new Token([$tokens[$index]->getId(), $doc->getContent()]);
126+
}
127+
}
113128
}

src/Fixer/PhpdocVarAnnotationCorrectOrderFixer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public function fix(\SplFileInfo $file, Tokens $tokens) : void
5454

5555
public function getPriority() : int
5656
{
57-
return 0;
57+
// must be before PhpdocNoIncorrectVarAnnotationFixer
58+
return 7;
5859
}
5960
}

tests/Fixer/PhpdocNoIncorrectVarAnnotationFixerTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PhpCsFixer\Fixer\Whitespace\NoExtraBlankLinesFixer;
1111
use PhpCsFixer\Fixer\Whitespace\NoTrailingWhitespaceFixer;
1212
use PhpCsFixer\Fixer\Whitespace\NoWhitespaceInBlankLineFixer;
13+
use PhpCsFixerCustomFixers\Fixer\PhpdocVarAnnotationCorrectOrderFixer;
1314

1415
/**
1516
* @internal
@@ -26,6 +27,7 @@ public function testPriority() : void
2627
static::assertGreaterThan((new NoTrailingWhitespaceFixer())->getPriority(), $this->fixer->getPriority());
2728
static::assertGreaterThan((new NoUnusedImportsFixer())->getPriority(), $this->fixer->getPriority());
2829
static::assertGreaterThan((new NoWhitespaceInBlankLineFixer())->getPriority(), $this->fixer->getPriority());
30+
static::assertLessThan((new PhpdocVarAnnotationCorrectOrderFixer())->getPriority(), $this->fixer->getPriority());
2931
}
3032

3133
/**
@@ -48,6 +50,13 @@ public function provideFixCases() : \Iterator
4850
',
4951
];
5052

53+
yield [
54+
'<?php
55+
/** @var \Foo $foo */
56+
$foo = new Foo();
57+
',
58+
];
59+
5160
yield [
5261
'<?php
5362
@@ -59,6 +68,16 @@ public function provideFixCases() : \Iterator
5968
',
6069
];
6170

71+
yield [
72+
'<?php
73+
$bar = new Logger();
74+
',
75+
'<?php
76+
/** @var $bar */
77+
$bar = new Logger();
78+
',
79+
];
80+
6281
yield [
6382
'<?php
6483
/**
@@ -143,6 +162,47 @@ class Foo
143162
*/
144163
private $d;
145164
}
165+
',
166+
];
167+
168+
yield [
169+
'<?php
170+
class Foo
171+
{
172+
173+
static $a;
174+
175+
/*
176+
*/
177+
public $b;
178+
179+
180+
protected $c;
181+
182+
/**
183+
*/
184+
private $d;
185+
}
186+
',
187+
'<?php
188+
class Foo
189+
{
190+
/* @var */
191+
static $a;
192+
193+
/*
194+
* @var
195+
*/
196+
public $b;
197+
198+
/** @var */
199+
protected $c;
200+
201+
/**
202+
* @var
203+
*/
204+
private $d;
205+
}
146206
',
147207
];
148208
}

tests/Fixer/PhpdocVarAnnotationCorrectOrderFixerTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Tests\Fixer;
66

7+
use PhpCsFixerCustomFixers\Fixer\PhpdocNoIncorrectVarAnnotationFixer;
8+
79
/**
810
* @internal
911
*
@@ -13,7 +15,7 @@ final class PhpdocVarAnnotationCorrectOrderFixerTest extends AbstractFixerTestCa
1315
{
1416
public function testPriority() : void
1517
{
16-
static::assertSame(0, $this->fixer->getPriority());
18+
static::assertGreaterThan((new PhpdocNoIncorrectVarAnnotationFixer())->getPriority(), $this->fixer->getPriority());
1719
}
1820

1921
/**

0 commit comments

Comments
 (0)