Skip to content

Commit 6a5d2ae

Browse files
authored
extend dead param worker (#84)
1 parent c829cbd commit 6a5d2ae

6 files changed

Lines changed: 100 additions & 2 deletions

File tree

src/Fixer/Commenting/ParamReturnAndVarTagMalformsFixer.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use SplFileInfo;
1414
use Symplify\CodingStandard\Fixer\AbstractSymplifyFixer;
1515
use Symplify\CodingStandard\TokenRunner\Contract\DocBlock\MalformWorkerInterface;
16+
use Symplify\CodingStandard\TokenRunner\DocBlock\MalformWorker\DeadParamMalformWorker;
1617
use Symplify\CodingStandard\TokenRunner\DocBlock\MalformWorker\InlineVariableDocBlockMalformWorker;
1718
use Symplify\CodingStandard\TokenRunner\DocBlock\MalformWorker\InlineVarMalformWorker;
1819
use Symplify\CodingStandard\TokenRunner\DocBlock\MalformWorker\MissingParamNameMalformWorker;
@@ -51,6 +52,7 @@ public function __construct(
5152
SuperfluousReturnNameMalformWorker $superfluousReturnNameMalformWorker,
5253
SuperfluousVarNameMalformWorker $superfluousVarNameMalformWorker,
5354
SwitchedTypeAndNameMalformWorker $switchedTypeAndNameMalformWorker,
55+
DeadParamMalformWorker $deadParamMalformWorker,
5456
private readonly TokenReverser $tokenReverser
5557
) {
5658
$this->malformWorkers = [
@@ -63,6 +65,7 @@ public function __construct(
6365
$superfluousReturnNameMalformWorker,
6466
$superfluousVarNameMalformWorker,
6567
$switchedTypeAndNameMalformWorker,
68+
$deadParamMalformWorker,
6669
];
6770
}
6871

@@ -123,6 +126,17 @@ public function fix(SplFileInfo $fileInfo, Tokens $tokens): void
123126
continue;
124127
}
125128

129+
// doc block became empty after removing dead lines → remove it completely,
130+
// including the whitespace that followed it, to avoid leaving a blank line
131+
if ($this->isEmptyDocBlock($docContent)) {
132+
$tokens->clearAt($index);
133+
if (isset($tokens[$index + 1]) && $tokens[$index + 1]->isWhitespace()) {
134+
$tokens->clearAt($index + 1);
135+
}
136+
137+
continue;
138+
}
139+
126140
$tokens[$index] = new Token([T_DOC_COMMENT, $docContent]);
127141
}
128142
}
@@ -137,4 +151,9 @@ public function getPriority(): int
137151
{
138152
return -37;
139153
}
154+
155+
private function isEmptyDocBlock(string $docContent): bool
156+
{
157+
return Strings::replace($docContent, '#/\*\*|\*/|\*|\s#', '') === '';
158+
}
140159
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symplify\CodingStandard\TokenRunner\DocBlock\MalformWorker;
6+
7+
use Nette\Utils\Strings;
8+
use PhpCsFixer\DocBlock\DocBlock;
9+
use PhpCsFixer\Tokenizer\Token;
10+
use PhpCsFixer\Tokenizer\Tokens;
11+
use Symplify\CodingStandard\TokenRunner\Contract\DocBlock\MalformWorkerInterface;
12+
13+
/**
14+
* Removes dead param annotation lines that only contain a variable name and no type,
15+
* e.g. "param $value" - such line carries no information and can be removed.
16+
*/
17+
final class DeadParamMalformWorker implements MalformWorkerInterface
18+
{
19+
/**
20+
* @see https://regex101.com/r/Hk4lFc/1
21+
*/
22+
private const string PARAM_WITHOUT_TYPE_REGEX = '#@(?:psalm-|phpstan-)?param\s+\$\w+\s*$#';
23+
24+
/**
25+
* @param Tokens<Token> $tokens
26+
*/
27+
public function work(string $docContent, Tokens $tokens, int $position): string
28+
{
29+
$docBlock = new DocBlock($docContent);
30+
31+
foreach ($docBlock->getLines() as $line) {
32+
if (! Strings::match($line->getContent(), self::PARAM_WITHOUT_TYPE_REGEX)) {
33+
continue;
34+
}
35+
36+
$line->remove();
37+
}
38+
39+
return $docBlock->getContent();
40+
}
41+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/**
4+
* @param string $name
5+
* @param $age
6+
*/
7+
function withDeadParam(string $name, $age)
8+
{
9+
}
10+
11+
?>
12+
-----
13+
<?php
14+
15+
/**
16+
* @param string $name
17+
*/
18+
function withDeadParam(string $name, $age)
19+
{
20+
}
21+
22+
?>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/**
4+
* @param $value
5+
*/
6+
function onlyDeadParam($value)
7+
{
8+
}
9+
10+
?>
11+
-----
12+
<?php
13+
14+
function onlyDeadParam($value)
15+
{
16+
}
17+
18+
?>

tests/Fixer/Commenting/ParamReturnAndVarTagMalformsFixer/Fixture/wrong4.php.inc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ function someFunction($instanceof)
3636
}
3737

3838
/**
39-
* @param $value
4039
*
4140
* @return The normalized array value
4241
*/

tests/Fixer/Commenting/ParamReturnAndVarTagMalformsFixer/Fixture/wrong9.php.inc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ function someFunction($argument, $argument2, $exceptionMessageFactory, $items)
1515
<?php
1616

1717
/**
18-
* @param $argument
1918
* @param string $argument2
2019
* @param ExceptionMessageFactoryInterface|null $exceptionMessageFactory
2120
* @param string[] $items

0 commit comments

Comments
 (0)