Skip to content

Commit 5ae3d21

Browse files
authored
Add RemoveDeadVarThisFixer (#23)
1 parent ceabc1a commit 5ae3d21

7 files changed

Lines changed: 147 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symplify\CodingStandard\Fixer\Commenting;
6+
7+
use PhpCsFixer\DocBlock\DocBlock;
8+
use PhpCsFixer\FixerDefinition\FixerDefinition;
9+
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
10+
use PhpCsFixer\Tokenizer\Token;
11+
use PhpCsFixer\Tokenizer\Tokens;
12+
use Symplify\CodingStandard\Utils\Regex;
13+
14+
/**
15+
* Removes a dead inline var doc block that types the "$this" variable - it carries no
16+
* information, as the type of "$this" is already known from the surrounding class.
17+
*
18+
* @see \Symplify\CodingStandard\Tests\Fixer\Commenting\RemoveDeadVarThisFixer\RemoveDeadVarThisFixerTest
19+
*/
20+
final class RemoveDeadVarThisFixer extends AbstractDocBlockFixer
21+
{
22+
private const string ERROR_MESSAGE = 'Remove a dead inline "@var ... $this" doc block';
23+
24+
/**
25+
* @see https://regex101.com/r/Hk4lFc/2
26+
*/
27+
private const string VAR_THIS_REGEX = '#@(?:psalm-|phpstan-)?var\b[^\n]*\$this\b#';
28+
29+
public function getDefinition(): FixerDefinitionInterface
30+
{
31+
return new FixerDefinition(self::ERROR_MESSAGE, []);
32+
}
33+
34+
/**
35+
* @param Tokens<Token> $tokens
36+
*/
37+
protected function processDocContent(string $docContent, Tokens $tokens, int $position): string
38+
{
39+
$docBlock = new DocBlock($docContent);
40+
41+
foreach ($docBlock->getLines() as $line) {
42+
if (! Regex::match($line->getContent(), self::VAR_THIS_REGEX)) {
43+
continue;
44+
}
45+
46+
$line->remove();
47+
}
48+
49+
return $docBlock->getContent();
50+
}
51+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Symplify\CodingStandard\Tests\Fixer\Commenting\RemoveDeadVarThisFixer\Fixture;
4+
5+
function anotherFunction()
6+
{
7+
/**
8+
* @var SomeType $this
9+
*/
10+
$this->run();
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Symplify\CodingStandard\Tests\Fixer\Commenting\RemoveDeadVarThisFixer\Fixture;
18+
19+
function anotherFunction()
20+
{
21+
$this->run();
22+
}
23+
24+
?>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Symplify\CodingStandard\Tests\Fixer\Commenting\RemoveDeadVarThisFixer\Fixture;
4+
5+
function someFunction()
6+
{
7+
/** @var SomeType $this */
8+
$this->run();
9+
}
10+
11+
?>
12+
-----
13+
<?php
14+
15+
namespace Symplify\CodingStandard\Tests\Fixer\Commenting\RemoveDeadVarThisFixer\Fixture;
16+
17+
function someFunction()
18+
{
19+
$this->run();
20+
}
21+
22+
?>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Symplify\CodingStandard\Tests\Fixer\Commenting\RemoveDeadVarThisFixer\Fixture;
4+
5+
function skipFunction()
6+
{
7+
/** @var SomeType $value */
8+
$value = create();
9+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symplify\CodingStandard\Tests\Fixer\Commenting\RemoveDeadVarThisFixer;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Symplify\EasyCodingStandard\Testing\PHPUnit\AbstractCheckerTestCase;
10+
11+
final class RemoveDeadVarThisFixerTest extends AbstractCheckerTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFiles(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfig(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Symplify\CodingStandard\Fixer\Commenting\RemoveDeadVarThisFixer;
6+
use Symplify\EasyCodingStandard\Config\ECSConfig;
7+
8+
return static function (ECSConfig $ecsConfig): void {
9+
$ecsConfig->rule(RemoveDeadVarThisFixer::class);
10+
};

src/Config/Level/DocblockLevel.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symplify\CodingStandard\Fixer\Commenting\DoubleAsteriskInlineVarFixer;
2323
use Symplify\CodingStandard\Fixer\Commenting\FixParamNameTypoFixer;
2424
use Symplify\CodingStandard\Fixer\Commenting\RemoveDeadParamFixer;
25+
use Symplify\CodingStandard\Fixer\Commenting\RemoveDeadVarThisFixer;
2526
use Symplify\CodingStandard\Fixer\Commenting\RemoveParamNameReferenceFixer;
2627
use Symplify\CodingStandard\Fixer\Commenting\RemoveSuperfluousReturnNameFixer;
2728
use Symplify\CodingStandard\Fixer\Commenting\RemoveSuperfluousVarNameFixer;
@@ -44,7 +45,9 @@ final class DocblockLevel
4445
public const array RULES = [
4546
// inline @var
4647
DoubleAsteriskInlineVarFixer::class,
48+
RemoveDeadVarThisFixer::class,
4749
SingleLineInlineVarDocBlockFixer::class,
50+
4851
AddMissingVarNameFixer::class,
4952

5053
// @param

0 commit comments

Comments
 (0)