Skip to content

Commit dab69e0

Browse files
authored
Merge pull request #79 from kubawerlos/upgrades
Deprecate PhpdocVarAnnotationCorrectOrderFixer
2 parents 6fe0053 + d4b90bf commit dab69e0

8 files changed

+32
-49
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ jobs:
2929
- php: '7.2'
3030
env: COMPOSER_FLAGS="--prefer-lowest"
3131
- php: '7.2'
32+
- php: '7.3'
3233
install: composer require --dev php-coveralls/php-coveralls
3334
script: composer phpunit -- --coverage-clover=build/logs/clover.xml
3435
after_success: vendor/bin/php-coveralls

CHANGELOG.md

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

33
## [Unreleased]
4+
- Update PHP CS Fixer to v2.14
45
- OperatorLinebreakFixer - respect no whitespace around operator
6+
- Deprecate PhpdocVarAnnotationCorrectOrderFixer
57

68
## v1.12.0 - *2018-12-02*
79
- Add NoCommentedOutCodeFixer

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,8 @@ In your PHP CS Fixer configuration register fixers and use them:
296296
}
297297
```
298298

299-
- **PhpdocVarAnnotationCorrectOrderFixer** - `@var` annotation must have type and name in the correct order.
300-
*To be deprecated after [this](https://github.com/FriendsOfPHP/PHP-CS-Fixer/pull/3881) is merged and released.*
299+
- **PhpdocVarAnnotationCorrectOrderFixer** - `@var` and `@type` annotations must have type and name in the correct order.
300+
DEPRECATED: use `phpdoc_var_annotation_correct_order` instead.
301301
```diff
302302
<?php
303303
-/** @var $foo int */

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"php": "^7.1",
1818
"ext-json": "*",
1919
"ext-tokenizer": "*",
20-
"friendsofphp/php-cs-fixer": "^2.13"
20+
"friendsofphp/php-cs-fixer": "^2.14"
2121
},
2222
"require-dev": {
2323
"johnkary/phpunit-speedtrap": "^3.0",

src/Fixer/PhpdocNoIncorrectVarAnnotationFixer.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
7777
public function getPriority(): int
7878
{
7979
// must be run before NoEmptyCommentFixer, NoEmptyPhpdocFixer, NoExtraBlankLinesFixer, NoTrailingWhitespaceFixer, NoUnusedImportsFixer and NoWhitespaceInBlankLineFixer
80-
// must be after PhpdocVarAnnotationCorrectOrderFixer
8180
return 6;
8281
}
8382

src/Fixer/PhpdocVarAnnotationCorrectOrderFixer.php

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,68 +4,53 @@
44

55
namespace PhpCsFixerCustomFixers\Fixer;
66

7-
use PhpCsFixer\FixerDefinition\CodeSample;
7+
use PhpCsFixer\Fixer\DeprecatedFixerInterface;
88
use PhpCsFixer\FixerDefinition\FixerDefinition;
9-
use PhpCsFixer\Preg;
10-
use PhpCsFixer\Tokenizer\Token;
119
use PhpCsFixer\Tokenizer\Tokens;
1210

13-
final class PhpdocVarAnnotationCorrectOrderFixer extends AbstractFixer implements DeprecatingFixerInterface
11+
/**
12+
* @deprecated use "phpdoc_var_annotation_correct_order" instead
13+
*/
14+
final class PhpdocVarAnnotationCorrectOrderFixer extends AbstractFixer implements DeprecatedFixerInterface
1415
{
16+
/** @var \PhpCsFixer\Fixer\Phpdoc\PhpdocVarAnnotationCorrectOrderFixer */
17+
private $fixer;
18+
19+
public function __construct()
20+
{
21+
$this->fixer = new \PhpCsFixer\Fixer\Phpdoc\PhpdocVarAnnotationCorrectOrderFixer();
22+
}
23+
1524
public function getDefinition(): FixerDefinition
1625
{
17-
return new FixerDefinition(
18-
'`@var` annotation must have type and name in the correct order.',
19-
[new CodeSample('<?php
20-
/** @var $foo int */
21-
$foo = 2 + 2;
22-
')]
23-
);
26+
return $this->fixer->getDefinition();
2427
}
2528

2629
public function isCandidate(Tokens $tokens): bool
2730
{
28-
return $tokens->isTokenKindFound(T_DOC_COMMENT);
31+
return $this->fixer->isCandidate($tokens);
2932
}
3033

3134
public function isRisky(): bool
3235
{
33-
return false;
36+
return $this->fixer->isRisky();
3437
}
3538

3639
public function fix(\SplFileInfo $file, Tokens $tokens): void
3740
{
38-
foreach ($tokens as $index => $token) {
39-
if (!$token->isGivenKind(T_DOC_COMMENT)) {
40-
continue;
41-
}
42-
43-
if (\stripos($token->getContent(), '@var') === false) {
44-
continue;
45-
}
46-
47-
$newContent = Preg::replace(
48-
'/(@var\s*)(\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(\s+)([^\$](?:[^<\s]|<[^>]*>)*)(\s|\*)/i',
49-
'$1$4$3$2$5',
50-
$token->getContent()
51-
);
52-
53-
if ($newContent === $token->getContent()) {
54-
continue;
55-
}
56-
57-
$tokens[$index] = new Token([$token->getId(), $newContent]);
58-
}
41+
$this->fixer->fix($file, $tokens);
5942
}
6043

6144
public function getPriority(): int
6245
{
63-
// must be before PhpdocNoIncorrectVarAnnotationFixer
64-
return 7;
46+
return $this->fixer->getPriority();
6547
}
6648

67-
public function getPullRequestId(): int
49+
/**
50+
* @return string[]
51+
*/
52+
public function getSuccessorsNames(): array
6853
{
69-
return 3881;
54+
return [$this->fixer->getName()];
7055
}
7156
}

tests/Fixer/PhpdocNoIncorrectVarAnnotationFixerTest.php

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

1514
/**
1615
* @internal
@@ -27,7 +26,6 @@ public function testPriority(): void
2726
static::assertGreaterThan((new NoTrailingWhitespaceFixer())->getPriority(), $this->fixer->getPriority());
2827
static::assertGreaterThan((new NoUnusedImportsFixer())->getPriority(), $this->fixer->getPriority());
2928
static::assertGreaterThan((new NoWhitespaceInBlankLineFixer())->getPriority(), $this->fixer->getPriority());
30-
static::assertLessThan((new PhpdocVarAnnotationCorrectOrderFixer())->getPriority(), $this->fixer->getPriority());
3129
}
3230

3331
public function testIsRisky(): void

tests/Fixer/PhpdocVarAnnotationCorrectOrderFixerTest.php

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

55
namespace Tests\Fixer;
66

7-
use PhpCsFixerCustomFixers\Fixer\PhpdocNoIncorrectVarAnnotationFixer;
8-
97
/**
108
* @internal
119
*
@@ -15,17 +13,17 @@ final class PhpdocVarAnnotationCorrectOrderFixerTest extends AbstractFixerTestCa
1513
{
1614
public function testPriority(): void
1715
{
18-
static::assertGreaterThan((new PhpdocNoIncorrectVarAnnotationFixer())->getPriority(), $this->fixer->getPriority());
16+
static::assertSame(0, $this->fixer->getPriority());
1917
}
2018

2119
public function testIsRisky(): void
2220
{
2321
static::assertFalse($this->fixer->isRisky());
2422
}
2523

26-
public function testDeprecatingPullRequest(): void
24+
public function testSuccessorName(): void
2725
{
28-
static::assertSame(3881, $this->fixer->getPullRequestId());
26+
static::assertContains('phpdoc_var_annotation_correct_order', $this->fixer->getSuccessorsNames());
2927
}
3028

3129
/**

0 commit comments

Comments
 (0)