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 src/Fixer/Commenting/ParamReturnAndVarTagMalformsFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use SplFileInfo;
use Symplify\CodingStandard\Fixer\AbstractSymplifyFixer;
use Symplify\CodingStandard\TokenRunner\Contract\DocBlock\MalformWorkerInterface;
use Symplify\CodingStandard\TokenRunner\DocBlock\MalformWorker\DeadParamMalformWorker;
use Symplify\CodingStandard\TokenRunner\DocBlock\MalformWorker\InlineVariableDocBlockMalformWorker;
use Symplify\CodingStandard\TokenRunner\DocBlock\MalformWorker\InlineVarMalformWorker;
use Symplify\CodingStandard\TokenRunner\DocBlock\MalformWorker\MissingParamNameMalformWorker;
Expand Down Expand Up @@ -51,6 +52,7 @@ public function __construct(
SuperfluousReturnNameMalformWorker $superfluousReturnNameMalformWorker,
SuperfluousVarNameMalformWorker $superfluousVarNameMalformWorker,
SwitchedTypeAndNameMalformWorker $switchedTypeAndNameMalformWorker,
DeadParamMalformWorker $deadParamMalformWorker,
private readonly TokenReverser $tokenReverser
) {
$this->malformWorkers = [
Expand All @@ -63,6 +65,7 @@ public function __construct(
$superfluousReturnNameMalformWorker,
$superfluousVarNameMalformWorker,
$switchedTypeAndNameMalformWorker,
$deadParamMalformWorker,
];
}

Expand Down Expand Up @@ -123,6 +126,17 @@ public function fix(SplFileInfo $fileInfo, Tokens $tokens): void
continue;
}

// doc block became empty after removing dead lines → remove it completely,
// including the whitespace that followed it, to avoid leaving a blank line
if ($this->isEmptyDocBlock($docContent)) {
$tokens->clearAt($index);
if (isset($tokens[$index + 1]) && $tokens[$index + 1]->isWhitespace()) {
$tokens->clearAt($index + 1);
}

continue;
}

$tokens[$index] = new Token([T_DOC_COMMENT, $docContent]);
}
}
Expand All @@ -137,4 +151,9 @@ public function getPriority(): int
{
return -37;
}

private function isEmptyDocBlock(string $docContent): bool
{
return Strings::replace($docContent, '#/\*\*|\*/|\*|\s#', '') === '';
}
}
41 changes: 41 additions & 0 deletions src/TokenRunner/DocBlock/MalformWorker/DeadParamMalformWorker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Symplify\CodingStandard\TokenRunner\DocBlock\MalformWorker;

use Nette\Utils\Strings;
use PhpCsFixer\DocBlock\DocBlock;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use Symplify\CodingStandard\TokenRunner\Contract\DocBlock\MalformWorkerInterface;

/**
* Removes dead param annotation lines that only contain a variable name and no type,
* e.g. "param $value" - such line carries no information and can be removed.
*/
final class DeadParamMalformWorker implements MalformWorkerInterface
{
/**
* @see https://regex101.com/r/Hk4lFc/1
*/
private const string PARAM_WITHOUT_TYPE_REGEX = '#@(?:psalm-|phpstan-)?param\s+\$\w+\s*$#';

/**
* @param Tokens<Token> $tokens
*/
public function work(string $docContent, Tokens $tokens, int $position): string
{
$docBlock = new DocBlock($docContent);

foreach ($docBlock->getLines() as $line) {
if (! Strings::match($line->getContent(), self::PARAM_WITHOUT_TYPE_REGEX)) {
continue;
}

$line->remove();
}

return $docBlock->getContent();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* @param string $name
* @param $age
*/
function withDeadParam(string $name, $age)
{
}

?>
-----
<?php

/**
* @param string $name
*/
function withDeadParam(string $name, $age)
{
}

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

/**
* @param $value
*/
function onlyDeadParam($value)
{
}

?>
-----
<?php

function onlyDeadParam($value)
{
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ function someFunction($instanceof)
}

/**
* @param $value
*
* @return The normalized array value
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ function someFunction($argument, $argument2, $exceptionMessageFactory, $items)
<?php

/**
* @param $argument
* @param string $argument2
* @param ExceptionMessageFactoryInterface|null $exceptionMessageFactory
* @param string[] $items
Expand Down
Loading