Skip to content

Commit 389d924

Browse files
authored
[annotation] Add RemoveSetterGetterDocblockFixer (#64)
* [annotation] Add RemoveSetterGetterDocblockFixer * Add RemoveSetterGetterDocblockFixer
1 parent 0f1591e commit 389d924

File tree

10 files changed

+261
-4
lines changed

10 files changed

+261
-4
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,25 @@ Fixes @param, @return, `@var` and inline `@var` annotations broken formats
145145

146146
<br>
147147

148+
## RemoveSetterGetterDocblockFixer
149+
150+
Remove docblock description from getter/setter methods, that only repeats the method name
151+
152+
- class: [`Symplify\CodingStandard\Fixer\Annotation\RemoveSetterGetterDocblockFixer`](../src/Fixer/Annotation/RemoveSetterGetterDocblockFixer.php)
153+
154+
```diff
155+
/**
156+
- * Get name
157+
*
158+
* @return string
159+
*/
160+
function getName()
161+
{
162+
}
163+
```
164+
165+
<br>
166+
148167
## RemovePHPStormAnnotationFixer
149168

150169
Remove "Created by PhpStorm" annotations

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
"require": {
66
"php": ">=8.2",
77
"nette/utils": "^4.0",
8-
"friendsofphp/php-cs-fixer": "^3.73.1"
8+
"friendsofphp/php-cs-fixer": "^3.75.0"
99
},
1010
"require-dev": {
1111
"symplify/easy-coding-standard": "^12.5",
1212
"squizlabs/php_codesniffer": "^3.12",
1313
"phpunit/phpunit": "^11.5",
1414
"phpstan/extension-installer": "^1.4",
1515
"phpstan/phpstan": "^2.1",
16-
"rector/rector": "^2.0",
16+
"rector/rector": "^2.0.13",
1717
"symplify/phpstan-extensions": "^12.0",
1818
"tomasvotruba/class-leak": "^2.0",
1919
"tracy/tracy": "^2.10"

config/symplify.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpCsFixer\Fixer\Phpdoc\GeneralPhpdocAnnotationRemoveFixer;
66
use Symplify\CodingStandard\Fixer\Annotation\RemovePHPStormAnnotationFixer;
7+
use Symplify\CodingStandard\Fixer\Annotation\RemoveSetterGetterDocblockFixer;
78
use Symplify\CodingStandard\Fixer\ArrayNotation\ArrayListItemNewlineFixer;
89
use Symplify\CodingStandard\Fixer\ArrayNotation\ArrayOpenerAndCloserNewlineFixer;
910
use Symplify\CodingStandard\Fixer\Commenting\ParamReturnAndVarTagMalformsFixer;
@@ -21,6 +22,7 @@
2122
RemovePHPStormAnnotationFixer::class,
2223
ParamReturnAndVarTagMalformsFixer::class,
2324
RemoveUselessDefaultCommentFixer::class,
25+
RemoveSetterGetterDocblockFixer::class,
2426

2527
// arrays
2628
ArrayListItemNewlineFixer::class,

rector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
return RectorConfig::configure()
88
->withPaths([__DIR__ . '/config', __DIR__ . '/src', __DIR__ . '/tests'])
9-
->withPhpSets()
109
->withRootFiles()
10+
->withPhpSets()
1111
->withPreparedSets(codeQuality: true, codingStyle: true, naming: true, earlyReturn: true, privatization: true)
12-
->withImportNames(removeUnusedImports: true)
12+
->withImportNames()
1313
->withSkip([
1414
'*/Source/*',
1515
'*/Fixture/*',
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symplify\CodingStandard\Fixer\Annotation;
6+
7+
use Nette\Utils\Strings;
8+
use PhpCsFixer\FixerDefinition\FixerDefinition;
9+
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
10+
use PhpCsFixer\Tokenizer\Token;
11+
use PhpCsFixer\Tokenizer\Tokens;
12+
use SplFileInfo;
13+
use Symplify\CodingStandard\Fixer\AbstractSymplifyFixer;
14+
use Symplify\CodingStandard\Fixer\Naming\MethodNameResolver;
15+
use Symplify\CodingStandard\TokenRunner\Traverser\TokenReverser;
16+
17+
/**
18+
* @see \Symplify\CodingStandard\Tests\Fixer\Annotation\RemoveSetterGetterDocblockFixer\RemoveSetterGetterDocblockFixerTest
19+
*/
20+
final class RemoveSetterGetterDocblockFixer extends AbstractSymplifyFixer
21+
{
22+
/**
23+
* @var string
24+
*/
25+
private const ERROR_MESSAGE = 'Remove setter and getter only docblocks';
26+
27+
private readonly MethodNameResolver $methodNameResolver;
28+
29+
public function __construct(
30+
private readonly TokenReverser $tokenReverser
31+
) {
32+
$this->methodNameResolver = new MethodNameResolver();
33+
}
34+
35+
public function getDefinition(): FixerDefinitionInterface
36+
{
37+
return new FixerDefinition(self::ERROR_MESSAGE, []);
38+
}
39+
40+
/**
41+
* @param Tokens<Token> $tokens
42+
*/
43+
public function isCandidate(Tokens $tokens): bool
44+
{
45+
if (! $tokens->isTokenKindFound(T_FUNCTION)) {
46+
return false;
47+
}
48+
49+
return $tokens->isAnyTokenKindsFound([T_DOC_COMMENT, T_COMMENT]);
50+
}
51+
52+
/**
53+
* @param Tokens<Token> $tokens
54+
*/
55+
public function fix(SplFileInfo $fileInfo, Tokens $tokens): void
56+
{
57+
$reversedTokens = $this->tokenReverser->reverse($tokens);
58+
59+
foreach ($reversedTokens as $index => $token) {
60+
if (! $token->isGivenKind([T_DOC_COMMENT, T_COMMENT])) {
61+
continue;
62+
}
63+
64+
$methodName = $this->methodNameResolver->resolve($tokens, $index);
65+
if (is_null($methodName)) {
66+
continue;
67+
}
68+
69+
// skip if not setter or getter
70+
$originalDocContent = $token->getContent();
71+
72+
$hasChanged = false;
73+
74+
$docblockLines = explode("\n", $originalDocContent);
75+
foreach ($docblockLines as $key => $docblockLine) {
76+
$spacelessDocblockLine = Strings::replace($docblockLine, '#[\s\n]+#', '');
77+
if (strtolower($spacelessDocblockLine) !== strtolower('*' . $methodName)) {
78+
continue;
79+
}
80+
81+
$hasChanged = true;
82+
unset($docblockLines[$key]);
83+
}
84+
85+
if (! $hasChanged) {
86+
continue;
87+
}
88+
89+
$tokens[$index] = new Token([T_DOC_COMMENT, implode("\n", $docblockLines)]);
90+
}
91+
}
92+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symplify\CodingStandard\Fixer\Naming;
6+
7+
use PhpCsFixer\Tokenizer\Token;
8+
use PhpCsFixer\Tokenizer\Tokens;
9+
use SplFileInfo;
10+
11+
final class MethodNameResolver
12+
{
13+
/**
14+
* @param Tokens<Token> $tokens
15+
*/
16+
public function resolve(Tokens $tokens, int $currentPosition): ?string
17+
{
18+
foreach ($tokens as $position => $token) {
19+
if ($position <= $currentPosition) {
20+
continue;
21+
}
22+
23+
if (! $token->isGivenKind([T_FUNCTION])) {
24+
continue;
25+
}
26+
27+
$nextNextMeaningfulTokenIndex = $tokens->getNextMeaningfulToken($position + 1);
28+
$nextNextMeaningfulToken = $tokens[$nextNextMeaningfulTokenIndex];
29+
30+
// skip anonymous functions
31+
if (! $nextNextMeaningfulToken->isGivenKind(T_STRING)) {
32+
continue;
33+
}
34+
35+
return $nextNextMeaningfulToken->getContent();
36+
}
37+
38+
return null;
39+
}
40+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Symplify\CodingStandard\Tests\Fixer\Annotation\RemoveSetterGetterDocblockFixer\Fixture;
4+
5+
final class SimpleAnnotation
6+
{
7+
/**
8+
* Set name
9+
*/
10+
public function setName()
11+
{
12+
}
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
namespace Symplify\CodingStandard\Tests\Fixer\Annotation\RemoveSetterGetterDocblockFixer\Fixture;
20+
21+
final class SimpleAnnotation
22+
{
23+
/**
24+
*/
25+
public function setName()
26+
{
27+
}
28+
}
29+
30+
?>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Symplify\CodingStandard\Tests\Fixer\Annotation\RemoveSetterGetterDocblockFixer\Fixture;
4+
5+
final class WithReturnDateTime
6+
{
7+
/**
8+
* Get now
9+
*
10+
* @return \DateTime
11+
*/
12+
public function getNow()
13+
{
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Symplify\CodingStandard\Tests\Fixer\Annotation\RemoveSetterGetterDocblockFixer\Fixture;
22+
23+
final class WithReturnDateTime
24+
{
25+
/**
26+
*
27+
* @return \DateTime
28+
*/
29+
public function getNow()
30+
{
31+
}
32+
}
33+
34+
?>
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\Annotation\RemoveSetterGetterDocblockFixer;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Symplify\EasyCodingStandard\Testing\PHPUnit\AbstractCheckerTestCase;
10+
11+
final class RemoveSetterGetterDocblockFixerTest 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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Symplify\CodingStandard\Fixer\Annotation\RemoveSetterGetterDocblockFixer;
6+
use Symplify\EasyCodingStandard\Config\ECSConfig;
7+
8+
return static function (ECSConfig $ecsConfig): void {
9+
$ecsConfig->rules([
10+
RemoveSetterGetterDocblockFixer::class,
11+
]);
12+
};

0 commit comments

Comments
 (0)