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
95 changes: 94 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,12 @@ Each chain method call must be on own line

## ParamReturnAndVarTagMalformsFixer

Fixes @param, @return, `@var` and inline `@var` annotations broken formats
Fixes `@param`, `@return`, `@var` and inline `@var` annotations broken formats. This single rule covers a wide range of docblock malforms:

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

**Add a missing `@param` variable name**

```diff
/**
- * @param string
Expand All @@ -141,6 +143,97 @@ Fixes @param, @return, `@var` and inline `@var` annotations broken formats
}
```

**Reorder switched type and variable name**

```diff
/**
- * @param $a string
- * @param $b string|null
+ * @param string $a
+ * @param string|null $b
*/
function test($a, string $b = null): string
{
}
```

**Remove a dead `@param` line that has only a name and no type**

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

**Fix a typo in the `@param` variable name to match the real argument**

```diff
/**
* @param string $one
- * @param string $twoTypo
+ * @param string $two
*/
function anotherFunction(string $one, string $two)
{
}
```

**Remove the reference `&` from a `@param` name**

```diff
/**
- * @param string &$name
+ * @param string $name
*/
function paramReference($name)
{
}
```

**Remove a superfluous variable name from `@return`**

```diff
/**
- * @return int $value
+ * @return int
*/
function function1(): int
{
}
```

**Remove a superfluous variable name from a property `@var`**

```diff
/**
- * @var string $property
+ * @var string
*/
private $property;
```

**Add a missing variable name to an inline `@var`**

```diff
-/** @var int */
+/** @var int $value */
$value = 1000;
```

**Normalize a malformed inline `@var` (single asterisk, switched name/type)**

```diff
-/* @var $variable int */
+/** @var int $variable */
$variable = 5;
```

It also handles the `@phpstan-` and `@psalm-` prefixed variants of these tags.

<br>

## RemovePropertyVariableNameDescriptionFixer
Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"license": "MIT",
"require": {
"php": ">=8.4",
"nette/utils": "^4.1",
"friendsofphp/php-cs-fixer": "^3.95.4"
},
"require-dev": {
Expand Down
6 changes: 3 additions & 3 deletions src/DocBlock/UselessDocBlockCleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Symplify\CodingStandard\DocBlock;

use Nette\Utils\Strings;
use PhpCsFixer\Tokenizer\Token;
use Symplify\CodingStandard\Utils\Regex;

final class UselessDocBlockCleaner
{
Expand Down Expand Up @@ -72,7 +72,7 @@ public function clearDocTokenContent(Token $currentToken, ?string $classLikeName
}

foreach (self::CLEANING_REGEXES as $cleaningRegex) {
$commentLine = Strings::replace($commentLine, $cleaningRegex);
$commentLine = Regex::replace($commentLine, $cleaningRegex);
}

$cleanedCommentLines[$key] = $commentLine;
Expand All @@ -90,7 +90,7 @@ public function clearDocTokenContent(Token $currentToken, ?string $classLikeName
$commentText = implode("\n", $cleanedCommentLines);

// run multilines regex on final result
return Strings::replace($commentText, self::DOCTRINE_GENERATED_COMMENT_REGEX);
return Regex::replace($commentText, self::DOCTRINE_GENERATED_COMMENT_REGEX);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Symplify\CodingStandard\Fixer\Annotation;

use Nette\Utils\Strings;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Token;
Expand All @@ -13,6 +12,7 @@
use Symplify\CodingStandard\Fixer\AbstractSymplifyFixer;
use Symplify\CodingStandard\Fixer\Naming\MethodNameResolver;
use Symplify\CodingStandard\TokenRunner\Traverser\TokenReverser;
use Symplify\CodingStandard\Utils\Regex;

/**
* @see \Symplify\CodingStandard\Tests\Fixer\Annotation\RemoveMethodNameDuplicateDescriptionFixer\RemoveMethodNameDuplicateDescriptionFixerTest
Expand Down Expand Up @@ -70,7 +70,7 @@ public function fix(SplFileInfo $fileInfo, Tokens $tokens): void

$docblockLines = explode("\n", $originalDocContent);
foreach ($docblockLines as $key => $docblockLine) {
$spacelessDocblockLine = Strings::replace($docblockLine, '#[\s\n]+#', '');
$spacelessDocblockLine = Regex::replace($docblockLine, '#[\s\n]+#', '');
if (strtolower($spacelessDocblockLine) !== strtolower('*' . $methodName)) {
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Fixer/Annotation/RemovePHPStormAnnotationFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

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\TokenRunner\Traverser\TokenReverser;
use Symplify\CodingStandard\Utils\Regex;

/**
* @see \Symplify\CodingStandard\Tests\Fixer\Annotation\RemovePHPStormAnnotationFixer\RemovePHPStormAnnotationFixerTest
Expand Down Expand Up @@ -56,7 +56,7 @@ public function fix(SplFileInfo $fileInfo, Tokens $tokens): void
}

$originalDocContent = $token->getContent();
$cleanedDocContent = Strings::replace($originalDocContent, self::CREATED_BY_PHPSTORM_DOC_REGEX, '');
$cleanedDocContent = Regex::replace($originalDocContent, self::CREATED_BY_PHPSTORM_DOC_REGEX, '');
if ($cleanedDocContent !== '') {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Symplify\CodingStandard\Fixer\Annotation;

use Nette\Utils\Strings;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Token;
Expand Down Expand Up @@ -90,7 +89,7 @@ public function fix(SplFileInfo $fileInfo, Tokens $tokens): void
}

// remove last x characters
$docblockLine = Strings::substring($docblockLine, 0, -strlen(' ' . $propertyName));
$docblockLine = mb_substr($docblockLine, 0, -strlen(' ' . $propertyName));

$hasChanged = true;
$docblockLines[$key] = rtrim($docblockLine);
Expand Down
6 changes: 3 additions & 3 deletions src/Fixer/Commenting/ParamReturnAndVarTagMalformsFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Symplify\CodingStandard\Fixer\Commenting;

use Nette\Utils\Strings;
use Override;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
Expand All @@ -24,6 +23,7 @@
use Symplify\CodingStandard\TokenRunner\DocBlock\MalformWorker\SuperfluousVarNameMalformWorker;
use Symplify\CodingStandard\TokenRunner\DocBlock\MalformWorker\SwitchedTypeAndNameMalformWorker;
use Symplify\CodingStandard\TokenRunner\Traverser\TokenReverser;
use Symplify\CodingStandard\Utils\Regex;

/**
* @see \Symplify\CodingStandard\Tests\Fixer\Commenting\ParamReturnAndVarTagMalformsFixer\ParamReturnAndVarTagMalformsFixerTest
Expand Down Expand Up @@ -113,7 +113,7 @@ public function fix(SplFileInfo $fileInfo, Tokens $tokens): void
}

$docContent = $token->getContent();
if (! Strings::match($docContent, self::TYPE_ANNOTATION_REGEX)) {
if (! Regex::match($docContent, self::TYPE_ANNOTATION_REGEX)) {
continue;
}

Expand Down Expand Up @@ -154,6 +154,6 @@ public function getPriority(): int

private function isEmptyDocBlock(string $docContent): bool
{
return Strings::replace($docContent, '#/\*\*|\*/|\*|\s#', '') === '';
return Regex::replace($docContent, '#/\*\*|\*/|\*|\s#', '') === '';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

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;
use Symplify\CodingStandard\Utils\Regex;

/**
* Removes dead param annotation lines that only contain a variable name and no type,
Expand All @@ -29,7 +29,7 @@ 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)) {
if (! Regex::match($line->getContent(), self::PARAM_WITHOUT_TYPE_REGEX)) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Symplify\CodingStandard\TokenRunner\DocBlock\MalformWorker;

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

final class InlineVarMalformWorker implements MalformWorkerInterface
{
Expand All @@ -28,6 +28,6 @@ public function work(string $docContent, Tokens $tokens, int $position): string
return $docContent;
}

return Strings::replace($docContent, self::SINGLE_ASTERISK_START_REGEX, '/**$1');
return Regex::replace($docContent, self::SINGLE_ASTERISK_START_REGEX, '/**$1');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Symplify\CodingStandard\TokenRunner\DocBlock\MalformWorker;

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

final class InlineVariableDocBlockMalformWorker implements MalformWorkerInterface
{
Expand Down Expand Up @@ -41,13 +41,13 @@ public function work(string $docContent, Tokens $tokens, int $position): string
}

// asterisk start
$docContent = Strings::replace($docContent, self::SINGLE_ASTERISK_START_REGEX, '/**$1');
$docContent = Regex::replace($docContent, self::SINGLE_ASTERISK_START_REGEX, '/**$1');

// inline
$docContent = Strings::replace($docContent, self::SPACE_REGEX, ' ');
$docContent = Regex::replace($docContent, self::SPACE_REGEX, ' ');

// remove asterisk leftover
return Strings::replace($docContent, self::ASTERISK_LEFTOVERS_REGEX, '$1');
return Regex::replace($docContent, self::ASTERISK_LEFTOVERS_REGEX, '$1');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace Symplify\CodingStandard\TokenRunner\DocBlock\MalformWorker;

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

final readonly class MissingParamNameMalformWorker implements MalformWorkerInterface
{
Expand Down Expand Up @@ -64,7 +64,7 @@ private function filterOutExistingParamNames(string $docContent, array $function
{
foreach ($functionArgumentNames as $key => $functionArgumentName) {
$pattern = '# ' . preg_quote($functionArgumentName, '#') . '\b#';
if (Strings::match($docContent, $pattern)) {
if (Regex::match($docContent, $pattern)) {
unset($functionArgumentNames[$key]);
}
}
Expand Down Expand Up @@ -116,11 +116,11 @@ private function shouldSkipLine(Line $line): bool
}

// already has a param name
if (Strings::match($line->getContent(), self::PARAM_WITH_NAME_REGEX)) {
if (Regex::match($line->getContent(), self::PARAM_WITH_NAME_REGEX)) {
return true;
}

$match = Strings::match($line->getContent(), self::PARAM_WITHOUT_NAME_REGEX);
$match = Regex::match($line->getContent(), self::PARAM_WITHOUT_NAME_REGEX);
return $match === null;
}

Expand All @@ -130,12 +130,12 @@ private function createNewLineContent(string $newArgumentName, Line $line): stri
$missingDollarSignPattern = '#(@param\s+([\w\|\[\]\\\\]+\s)?)(' . ltrim($newArgumentName, '$') . ')#';

// missing \$ case - possibly own worker
if (Strings::match($line->getContent(), $missingDollarSignPattern)) {
return Strings::replace($line->getContent(), $missingDollarSignPattern, '$1$$3');
if (Regex::match($line->getContent(), $missingDollarSignPattern)) {
return Regex::replace($line->getContent(), $missingDollarSignPattern, '$1$$3');
}

$replacement = '@param $1 ' . $newArgumentName . '$2' . "\n";

return Strings::replace($line->getContent(), self::PARAM_WITHOUT_NAME_REGEX, $replacement);
return Regex::replace($line->getContent(), self::PARAM_WITHOUT_NAME_REGEX, $replacement);
}
}
Loading
Loading