Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,25 @@ Fixes @param, @return, `@var` and inline `@var` annotations broken formats

<br>

## RemoveSetterGetterDocblockFixer

Remove docblock description from getter/setter methods, that only repeats the method name

- class: [`Symplify\CodingStandard\Fixer\Annotation\RemoveSetterGetterDocblockFixer`](../src/Fixer/Annotation/RemoveSetterGetterDocblockFixer.php)

```diff
/**
- * Get name
*
* @return string
*/
function getName()
{
}
```

<br>

## RemovePHPStormAnnotationFixer

Remove "Created by PhpStorm" annotations
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
"require": {
"php": ">=8.2",
"nette/utils": "^4.0",
"friendsofphp/php-cs-fixer": "^3.73.1"
"friendsofphp/php-cs-fixer": "^3.75.0"
},
"require-dev": {
"symplify/easy-coding-standard": "^12.5",
"squizlabs/php_codesniffer": "^3.12",
"phpunit/phpunit": "^11.5",
"phpstan/extension-installer": "^1.4",
"phpstan/phpstan": "^2.1",
"rector/rector": "^2.0",
"rector/rector": "^2.0.13",
"symplify/phpstan-extensions": "^12.0",
"tomasvotruba/class-leak": "^2.0",
"tracy/tracy": "^2.10"
Expand Down
2 changes: 2 additions & 0 deletions config/symplify.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhpCsFixer\Fixer\Phpdoc\GeneralPhpdocAnnotationRemoveFixer;
use Symplify\CodingStandard\Fixer\Annotation\RemovePHPStormAnnotationFixer;
use Symplify\CodingStandard\Fixer\Annotation\RemoveSetterGetterDocblockFixer;
use Symplify\CodingStandard\Fixer\ArrayNotation\ArrayListItemNewlineFixer;
use Symplify\CodingStandard\Fixer\ArrayNotation\ArrayOpenerAndCloserNewlineFixer;
use Symplify\CodingStandard\Fixer\Commenting\ParamReturnAndVarTagMalformsFixer;
Expand All @@ -21,6 +22,7 @@
RemovePHPStormAnnotationFixer::class,
ParamReturnAndVarTagMalformsFixer::class,
RemoveUselessDefaultCommentFixer::class,
RemoveSetterGetterDocblockFixer::class,

// arrays
ArrayListItemNewlineFixer::class,
Expand Down
4 changes: 2 additions & 2 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

return RectorConfig::configure()
->withPaths([__DIR__ . '/config', __DIR__ . '/src', __DIR__ . '/tests'])
->withPhpSets()
->withRootFiles()
->withPhpSets()
->withPreparedSets(codeQuality: true, codingStyle: true, naming: true, earlyReturn: true, privatization: true)
->withImportNames(removeUnusedImports: true)
->withImportNames()
->withSkip([
'*/Source/*',
'*/Fixture/*',
Expand Down
92 changes: 92 additions & 0 deletions src/Fixer/Annotation/RemoveSetterGetterDocblockFixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

declare(strict_types=1);

namespace Symplify\CodingStandard\Fixer\Annotation;

use Nette\Utils\Strings;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use SplFileInfo;
use Symplify\CodingStandard\Fixer\AbstractSymplifyFixer;
use Symplify\CodingStandard\Fixer\Naming\MethodNameResolver;
use Symplify\CodingStandard\TokenRunner\Traverser\TokenReverser;

/**
* @see \Symplify\CodingStandard\Tests\Fixer\Annotation\RemoveSetterGetterDocblockFixer\RemoveSetterGetterDocblockFixerTest
*/
final class RemoveSetterGetterDocblockFixer extends AbstractSymplifyFixer
{
/**
* @var string
*/
private const ERROR_MESSAGE = 'Remove setter and getter only docblocks';

private readonly MethodNameResolver $methodNameResolver;

public function __construct(
private readonly TokenReverser $tokenReverser
) {
$this->methodNameResolver = new MethodNameResolver();
}

public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(self::ERROR_MESSAGE, []);
}

/**
* @param Tokens<Token> $tokens
*/
public function isCandidate(Tokens $tokens): bool
{
if (! $tokens->isTokenKindFound(T_FUNCTION)) {
return false;
}

return $tokens->isAnyTokenKindsFound([T_DOC_COMMENT, T_COMMENT]);
}

/**
* @param Tokens<Token> $tokens
*/
public function fix(SplFileInfo $fileInfo, Tokens $tokens): void
{
$reversedTokens = $this->tokenReverser->reverse($tokens);

foreach ($reversedTokens as $index => $token) {
if (! $token->isGivenKind([T_DOC_COMMENT, T_COMMENT])) {
continue;
}

$methodName = $this->methodNameResolver->resolve($tokens, $index);
if (is_null($methodName)) {
continue;
}

// skip if not setter or getter
$originalDocContent = $token->getContent();

$hasChanged = false;

$docblockLines = explode("\n", $originalDocContent);
foreach ($docblockLines as $key => $docblockLine) {
$spacelessDocblockLine = Strings::replace($docblockLine, '#[\s\n]+#', '');
if (strtolower($spacelessDocblockLine) !== strtolower('*' . $methodName)) {
continue;
}

$hasChanged = true;
unset($docblockLines[$key]);
}

if (! $hasChanged) {
continue;
}

$tokens[$index] = new Token([T_DOC_COMMENT, implode("\n", $docblockLines)]);
}
}
}
40 changes: 40 additions & 0 deletions src/Fixer/Naming/MethodNameResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Symplify\CodingStandard\Fixer\Naming;

use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use SplFileInfo;

final class MethodNameResolver
{
/**
* @param Tokens<Token> $tokens
*/
public function resolve(Tokens $tokens, int $currentPosition): ?string
{
foreach ($tokens as $position => $token) {
if ($position <= $currentPosition) {
continue;
}

if (! $token->isGivenKind([T_FUNCTION])) {
continue;
}

$nextNextMeaningfulTokenIndex = $tokens->getNextMeaningfulToken($position + 1);
$nextNextMeaningfulToken = $tokens[$nextNextMeaningfulTokenIndex];

// skip anonymous functions
if (! $nextNextMeaningfulToken->isGivenKind(T_STRING)) {
continue;
}

return $nextNextMeaningfulToken->getContent();
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Symplify\CodingStandard\Tests\Fixer\Annotation\RemoveSetterGetterDocblockFixer\Fixture;

final class SimpleAnnotation
{
/**
* Set name
*/
public function setName()
{
}
}

?>
-----
<?php

namespace Symplify\CodingStandard\Tests\Fixer\Annotation\RemoveSetterGetterDocblockFixer\Fixture;

final class SimpleAnnotation
{
/**
*/
public function setName()
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Symplify\CodingStandard\Tests\Fixer\Annotation\RemoveSetterGetterDocblockFixer\Fixture;

final class WithReturnDateTime
{
/**
* Get now
*
* @return \DateTime
*/
public function getNow()
{
}
}

?>
-----
<?php

namespace Symplify\CodingStandard\Tests\Fixer\Annotation\RemoveSetterGetterDocblockFixer\Fixture;

final class WithReturnDateTime
{
/**
*
* @return \DateTime
*/
public function getNow()
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Symplify\CodingStandard\Tests\Fixer\Annotation\RemoveSetterGetterDocblockFixer;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Symplify\EasyCodingStandard\Testing\PHPUnit\AbstractCheckerTestCase;

final class RemoveSetterGetterDocblockFixerTest extends AbstractCheckerTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFiles(__DIR__ . '/Fixture');
}

public function provideConfig(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

use Symplify\CodingStandard\Fixer\Annotation\RemoveSetterGetterDocblockFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;

return static function (ECSConfig $ecsConfig): void {
$ecsConfig->rules([
RemoveSetterGetterDocblockFixer::class,
]);
};