Skip to content

Commit 65b51a9

Browse files
authored
Add NoUselessCommentFixer (#67)
1 parent 082cae8 commit 65b51a9

8 files changed

+559
-66
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
## [Unreleased]
44
- Add NoCommentedOutCodeFixer
5+
- Add NoUselessCommentFixer
56
- Add NullableParamStyleFixer
7+
- Deprecate NoUselessClassCommentFixer
8+
- Deprecate NoUselessConstructorCommentFixer
69
- Feature: OperatorLinebreakFixer - handle ternary operator
710
- Fix: NoImportFromGlobalNamespaceFixer - class without namespace
811
- Fix: NoUselessClassCommentFixer - comment detection

README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ In your PHP CS Fixer configuration register fixers and use them:
155155
+echo 'foobar';
156156
```
157157

158-
- **NoUselessClassCommentFixer** - there must be no comment like: "Class FooBar".
158+
- **NoUselessClassCommentFixer** - there must be no comment like: "Class FooBar".
159+
DEPRECATED: use `NoUselessCommentFixer` instead.
159160
```diff
160161
<?php
161162
/**
@@ -165,7 +166,23 @@ In your PHP CS Fixer configuration register fixers and use them:
165166
class FooBar {}
166167
```
167168

168-
- **NoUselessConstructorCommentFixer** - there must be no comment like: "Foo constructor".
169+
- **NoUselessCommentFixer** - there must be no comment like "Class Foo".
170+
```diff
171+
<?php
172+
/**
173+
- * Class Foo
174+
* Class to do something
175+
*/
176+
class Foo {
177+
/**
178+
- * Get bar
179+
*/
180+
function getBar() {}
181+
}
182+
```
183+
184+
- **NoUselessConstructorCommentFixer** - there must be no comment like: "Foo constructor".
185+
DEPRECATED: use `NoUselessCommentFixer` instead.
169186
```diff
170187
<?php
171188
class Foo {

src/Fixer/NoUselessClassCommentFixer.php

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,24 @@
44

55
namespace PhpCsFixerCustomFixers\Fixer;
66

7+
use PhpCsFixer\Fixer\DeprecatedFixerInterface;
78
use PhpCsFixer\FixerDefinition\CodeSample;
89
use PhpCsFixer\FixerDefinition\FixerDefinition;
9-
use PhpCsFixer\Preg;
10-
use PhpCsFixer\Tokenizer\Token;
1110
use PhpCsFixer\Tokenizer\Tokens;
1211

13-
final class NoUselessClassCommentFixer extends AbstractFixer
12+
/**
13+
* @deprecated use NoUselessCommentFixer instead
14+
*/
15+
final class NoUselessClassCommentFixer extends AbstractFixer implements DeprecatedFixerInterface
1416
{
17+
/** @var NoUselessCommentFixer */
18+
private $fixer;
19+
20+
public function __construct()
21+
{
22+
$this->fixer = new NoUselessCommentFixer();
23+
}
24+
1525
public function getDefinition(): FixerDefinition
1626
{
1727
return new FixerDefinition(
@@ -28,48 +38,29 @@ class FooBar {}
2838

2939
public function isCandidate(Tokens $tokens): bool
3040
{
31-
return $tokens->isAnyTokenKindsFound([T_COMMENT, T_DOC_COMMENT]);
41+
return $this->fixer->isCandidate($tokens);
3242
}
3343

3444
public function isRisky(): bool
3545
{
36-
return false;
46+
return $this->fixer->isRisky();
3747
}
3848

3949
public function fix(\SplFileInfo $file, Tokens $tokens): void
4050
{
41-
foreach ($tokens as $index => $token) {
42-
if (!$token->isGivenKind([T_COMMENT, T_DOC_COMMENT])) {
43-
continue;
44-
}
45-
46-
if (\strpos($token->getContent(), 'Class ') === false) {
47-
continue;
48-
}
49-
50-
$nextIndex = $tokens->getNextMeaningfulToken($index);
51-
if ($nextIndex === null || !$tokens[$nextIndex]->isGivenKind([T_ABSTRACT, T_CLASS, T_FINAL])) {
52-
continue;
53-
}
54-
55-
$newContent = Preg::replace(
56-
'/(\*)?\h*\bClass\h+[A-Za-z0-1\\\\_]+\.?(\h*\R\h*|\h*$)/i',
57-
'',
58-
$token->getContent()
59-
);
60-
61-
if ($newContent === $token->getContent()) {
62-
continue;
63-
}
64-
65-
$tokens[$index] = new Token([$token->getId(), $newContent]);
66-
}
51+
$this->fixer->fix($file, $tokens);
6752
}
6853

6954
public function getPriority(): int
7055
{
71-
// must be run before NoEmptyPhpdocFixer, NoEmptyCommentFixer
72-
// PhpdocSeparationFixer, PhpdocTrimConsecutiveBlankLineSeparationFixer and PhpdocTrimFixer
73-
return 6;
56+
return $this->fixer->getPriority();
57+
}
58+
59+
/**
60+
* @return string[]
61+
*/
62+
public function getSuccessorsNames(): array
63+
{
64+
return [(new \ReflectionObject($this->fixer))->getShortName()];
7465
}
7566
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace PhpCsFixerCustomFixers\Fixer;
6+
7+
use PhpCsFixer\FixerDefinition\CodeSample;
8+
use PhpCsFixer\FixerDefinition\FixerDefinition;
9+
use PhpCsFixer\Preg;
10+
use PhpCsFixer\Tokenizer\Token;
11+
use PhpCsFixer\Tokenizer\Tokens;
12+
13+
final class NoUselessCommentFixer extends AbstractFixer
14+
{
15+
/**
16+
* {@inheritdoc}
17+
*/
18+
public function getDefinition(): FixerDefinition
19+
{
20+
return new FixerDefinition(
21+
'There must be no comment like "Class Foo".',
22+
[
23+
new CodeSample('<?php
24+
/**
25+
* Class Foo
26+
* Class to do something
27+
*/
28+
class Foo {
29+
/**
30+
* Get bar
31+
*/
32+
function getBar() {}
33+
}
34+
'),
35+
]
36+
);
37+
}
38+
39+
public function isCandidate(Tokens $tokens): bool
40+
{
41+
return $tokens->isAnyTokenKindsFound([T_COMMENT, T_DOC_COMMENT]);
42+
}
43+
44+
public function getPriority(): int
45+
{
46+
// must be run before NoEmptyCommentFixer, NoEmptyPhpdocFixer, PhpdocTrimConsecutiveBlankLineSeparationFixer and PhpdocTrimFixer
47+
return 6;
48+
}
49+
50+
public function isRisky(): bool
51+
{
52+
return false;
53+
}
54+
55+
public function fix(\SplFileInfo $file, Tokens $tokens): void
56+
{
57+
foreach ($tokens as $index => $token) {
58+
if (!$token->isGivenKind([T_COMMENT, T_DOC_COMMENT])) {
59+
continue;
60+
}
61+
62+
$nextIndex = $tokens->getTokenNotOfKindSibling(
63+
$index,
64+
1,
65+
[[T_WHITESPACE], [T_COMMENT], [T_DOC_COMMENT], [T_ABSTRACT], [T_FINAL], [T_PUBLIC], [T_PROTECTED], [T_PRIVATE], [T_STATIC], [T_VAR]]
66+
);
67+
if ($nextIndex === null) {
68+
continue;
69+
}
70+
71+
if ($tokens[$nextIndex]->isGivenKind([T_CLASS, T_INTERFACE, T_TRAIT])) {
72+
$newContent = Preg::replace(
73+
'/\R?(?<=\n|\r|\r\n|^#|^\/\/|^\/\*|^\/\*\*)\h+\**\h*(class|interface|trait)\h+[A-Za-z0-9\\\\_]+.?(?=\R|$)/i',
74+
'',
75+
$token->getContent()
76+
);
77+
} elseif ($tokens[$nextIndex]->isGivenKind(T_FUNCTION)) {
78+
$newContent = Preg::replace(
79+
'/\R?(?<=\n|\r|\r\n|^#|^\/\/|^\/\*|^\/\*\*)\h+\**\h*((adds?|gets?|removes?|sets?)\h+[A-Za-z0-9\\\\_]+|([A-Za-z0-9\\\\_]+\h+)?constructor).?(?=\R|$)/i',
80+
'',
81+
$token->getContent()
82+
);
83+
} else {
84+
continue;
85+
}
86+
87+
if ($newContent === $token->getContent()) {
88+
continue;
89+
}
90+
91+
$tokens[$index] = new Token([$token->getId(), $newContent]);
92+
}
93+
}
94+
}

src/Fixer/NoUselessConstructorCommentFixer.php

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,24 @@
44

55
namespace PhpCsFixerCustomFixers\Fixer;
66

7+
use PhpCsFixer\Fixer\DeprecatedFixerInterface;
78
use PhpCsFixer\FixerDefinition\CodeSample;
89
use PhpCsFixer\FixerDefinition\FixerDefinition;
9-
use PhpCsFixer\Preg;
10-
use PhpCsFixer\Tokenizer\Token;
1110
use PhpCsFixer\Tokenizer\Tokens;
1211

13-
final class NoUselessConstructorCommentFixer extends AbstractFixer
12+
/**
13+
* @deprecated use NoUselessCommentFixer instead
14+
*/
15+
final class NoUselessConstructorCommentFixer extends AbstractFixer implements DeprecatedFixerInterface
1416
{
17+
/** @var NoUselessCommentFixer */
18+
private $fixer;
19+
20+
public function __construct()
21+
{
22+
$this->fixer = new NoUselessCommentFixer();
23+
}
24+
1525
public function getDefinition(): FixerDefinition
1626
{
1727
return new FixerDefinition(
@@ -29,42 +39,29 @@ public function __construct() {}
2939

3040
public function isCandidate(Tokens $tokens): bool
3141
{
32-
return $tokens->isAnyTokenKindsFound([T_COMMENT, T_DOC_COMMENT]);
42+
return $this->fixer->isCandidate($tokens);
3343
}
3444

3545
public function isRisky(): bool
3646
{
37-
return false;
47+
return $this->fixer->isRisky();
3848
}
3949

4050
public function fix(\SplFileInfo $file, Tokens $tokens): void
4151
{
42-
foreach ($tokens as $index => $token) {
43-
if (!$token->isGivenKind([T_COMMENT, T_DOC_COMMENT])) {
44-
continue;
45-
}
46-
47-
if (\stripos($token->getContent(), 'constructor') === false) {
48-
continue;
49-
}
50-
51-
$newContent = Preg::replace(
52-
'/(\*|\/\/)\h*(\h+[A-Za-z0-1\\\\_]+\h*)?\hconstructor\.?(\h*\R\h*\*|\h*$)/i',
53-
'$1',
54-
$token->getContent()
55-
);
56-
57-
if ($newContent === $token->getContent()) {
58-
continue;
59-
}
60-
61-
$tokens[$index] = new Token([$token->getId(), $newContent]);
62-
}
52+
$this->fixer->fix($file, $tokens);
6353
}
6454

6555
public function getPriority(): int
6656
{
67-
// must be run before NoEmptyPhpdocFixer, NoEmptyCommentFixer and PhpdocTrimFixer
68-
return 6;
57+
return $this->fixer->getPriority();
58+
}
59+
60+
/**
61+
* @return string[]
62+
*/
63+
public function getSuccessorsNames(): array
64+
{
65+
return [(new \ReflectionObject($this->fixer))->getShortName()];
6966
}
7067
}

tests/Fixer/NoUselessClassCommentFixerTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ public function testIsRisky(): void
3131
static::assertFalse($this->fixer->isRisky());
3232
}
3333

34+
public function testSuccessorName(): void
35+
{
36+
static::assertContains('NoUselessCommentFixer', $this->fixer->getSuccessorsNames());
37+
}
38+
3439
/**
3540
* @param string $expected
3641
* @param null|string $input
@@ -142,7 +147,8 @@ class Foo {}
142147

143148
yield [
144149
'<?php
145-
/** @see example.com
150+
/**
151+
* @see example.com
146152
*/
147153
abstract class Foo {}
148154
',

0 commit comments

Comments
 (0)