Skip to content

Commit 5023ff8

Browse files
authored
NoUselessCommentFixer - support PHPDoc like /** ClassName */ (#636)
1 parent 64e5438 commit 5023ff8

File tree

4 files changed

+118
-27
lines changed

4 files changed

+118
-27
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## v3.2.0
44
- Add PhpUnitAssertArgumentsOrderFixer
55
- PromotedConstructorPropertyFixer - add option "promote_only_existing_properties"
6+
- NoUselessCommentFixer - support PHPDoc like `/** ClassName */`
67

78
## v3.1.0
89
- Add MultilinePromotedPropertiesFixer

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Latest stable version](https://img.shields.io/packagist/v/kubawerlos/php-cs-fixer-custom-fixers.svg?label=current%20version)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
44
[![PHP version](https://img.shields.io/packagist/php-v/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://php.net)
55
[![License](https://img.shields.io/github/license/kubawerlos/php-cs-fixer-custom-fixers.svg)](LICENSE)
6-
![Tests](https://img.shields.io/badge/tests-2927-brightgreen.svg)
6+
![Tests](https://img.shields.io/badge/tests-2932-brightgreen.svg)
77
[![Downloads](https://img.shields.io/packagist/dt/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
88

99
[![CI Status](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/workflows/CI/badge.svg?branch=main&event=push)](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions)

src/Fixer/NoUselessCommentFixer.php

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -68,30 +68,7 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
6868
continue;
6969
}
7070

71-
$nextIndex = $tokens->getTokenNotOfKindSibling(
72-
$index,
73-
1,
74-
[[\T_WHITESPACE], [\T_COMMENT], [\T_ABSTRACT], [\T_FINAL], [\T_PUBLIC], [\T_PROTECTED], [\T_PRIVATE], [\T_STATIC]]
75-
);
76-
if ($nextIndex === null) {
77-
continue;
78-
}
79-
80-
if ($tokens[$nextIndex]->isGivenKind([\T_CLASS, \T_INTERFACE, \T_TRAIT])) {
81-
$newContent = Preg::replace(
82-
'/\R?(?<=\n|\r|\r\n|^#|^\/\/|^\/\*|^\/\*\*)\h+\**\h*(class|interface|trait)\h+[A-Za-z0-9\\\\_]+.?(?=\R|$)/i',
83-
'',
84-
$tokens[$index]->getContent()
85-
);
86-
} elseif ($tokens[$nextIndex]->isGivenKind(\T_FUNCTION)) {
87-
$newContent = Preg::replace(
88-
'/\R?(?<=\n|\r|\r\n|^#|^\/\/|^\/\*|^\/\*\*)\h+\**\h*((adds?|gets?|removes?|sets?)\h+[A-Za-z0-9\\\\_]+|([A-Za-z0-9\\\\_]+\h+)?constructor).?(?=\R|$)/i',
89-
'',
90-
$tokens[$index]->getContent()
91-
);
92-
} else {
93-
continue;
94-
}
71+
$newContent = $this->getNewContent($tokens, $index);
9572

9673
if ($newContent === $tokens[$index]->getContent()) {
9774
continue;
@@ -100,4 +77,58 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
10077
$tokens[$index] = new Token([$tokens[$index]->getId(), $newContent]);
10178
}
10279
}
80+
81+
private function getNewContent(Tokens $tokens, int $index): string
82+
{
83+
$content = $tokens[$index]->getContent();
84+
85+
$nextIndex = $tokens->getTokenNotOfKindSibling(
86+
$index,
87+
1,
88+
[[\T_WHITESPACE], [\T_COMMENT], [\T_ABSTRACT], [\T_FINAL], [\T_PUBLIC], [\T_PROTECTED], [\T_PRIVATE], [\T_STATIC]]
89+
);
90+
91+
if ($nextIndex === null) {
92+
return $content;
93+
}
94+
95+
if ($tokens[$nextIndex]->isGivenKind([\T_CLASS, \T_INTERFACE, \T_TRAIT])) {
96+
/** @var int $classyNameIndex */
97+
$classyNameIndex = $tokens->getNextMeaningfulToken($nextIndex);
98+
99+
/** @var string $content */
100+
$content = Preg::replace(
101+
\sprintf('~
102+
\R?
103+
(?<=\n|\r|\r\n|^\#|^/{2}|^/\*[^\*\s]|^/\*{2})
104+
\h*\**\h*
105+
(
106+
(class|interface|trait)\h+([a-zA-Z\d\\\\]+)
107+
|
108+
%s
109+
)
110+
\.?
111+
\h*
112+
(?=\R|\*/$|$)
113+
~ix', $tokens[$classyNameIndex]->getContent()),
114+
'',
115+
$content
116+
);
117+
} elseif ($tokens[$nextIndex]->isGivenKind(\T_FUNCTION)) {
118+
/** @var string $content */
119+
$content = Preg::replace(
120+
'/\R?(?<=\n|\r|\r\n|^#|^\/\/|^\/\*|^\/\*\*)\h+\**\h*((adds?|gets?|removes?|sets?)\h+[A-Za-z0-9\\\\_]+|([A-Za-z0-9\\\\_]+\h+)?constructor).?(?=\R|$)/i',
121+
'',
122+
$content
123+
);
124+
} else {
125+
return $content;
126+
}
127+
128+
if ($content === '/***/') {
129+
$content = '/** */';
130+
}
131+
132+
return $content;
133+
}
103134
}

tests/Fixer/NoUselessCommentFixerTest.php

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,32 @@ public function testFix(string $expected, ?string $input = null): void
3838
*/
3939
public static function provideFixCases(): iterable
4040
{
41+
yield ['<?php
42+
/**
43+
* Class Foo.
44+
*/
45+
foo();
46+
'];
47+
48+
yield ['<?php
49+
/**
50+
* DoNotRemoveMe
51+
*/
52+
class Foo {}
53+
'];
54+
4155
yield [
4256
'<?php
4357
/**
44-
* Class Foo.
4558
*/
46-
',
59+
class Foo {}
60+
',
61+
'<?php
62+
/**
63+
* Class Bar.
64+
*/
65+
class Foo {}
66+
',
4767
];
4868

4969
yield [
@@ -60,6 +80,17 @@ class Foo {}
6080
',
6181
];
6282

83+
yield [
84+
'<?php
85+
/** */
86+
class Foo {}
87+
',
88+
'<?php
89+
/** Class Foo */
90+
class Foo {}
91+
',
92+
];
93+
6394
yield [
6495
'<?php
6596
/**
@@ -88,6 +119,34 @@ class Foo {}
88119
',
89120
];
90121

122+
yield [
123+
'<?php
124+
/**
125+
*/
126+
class Foo {}
127+
',
128+
'<?php
129+
/**
130+
* Foo
131+
*/
132+
class Foo {}
133+
',
134+
];
135+
136+
yield [
137+
'<?php
138+
/**
139+
*/
140+
class Foo {}
141+
',
142+
'<?php
143+
/**
144+
* Foo.
145+
*/
146+
class Foo {}
147+
',
148+
];
149+
91150
yield [
92151
'<?php
93152
/**

0 commit comments

Comments
 (0)