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
37 changes: 37 additions & 0 deletions packages/coding-standard/src/Fixer/Commenting/FixTagTypoFixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Symplify\CodingStandard\Fixer\Commenting;

use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use Symplify\CodingStandard\Utils\Regex;

/**
* Fixes a plural-"s" typo on the @return, @param and @var doc block tags (e.g. a stray "@return" + "s" becomes "@return").
*
* @see \Symplify\CodingStandard\Tests\Fixer\Commenting\FixTagTypoFixer\FixTagTypoFixerTest
*/
final class FixTagTypoFixer extends AbstractDocBlockFixer
{
/**
* @see https://regex101.com/r/8tFqJp/1
*/
private const string PLURAL_TAG_REGEX = '#@((?:psalm-|phpstan-)?(?:param|return|var))s\b#';

public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition('Fix a plural typo in a doc block tag, e.g. "@returns" to "@return"', []);
}

/**
* @param Tokens<Token> $tokens
*/
protected function processDocContent(string $docContent, Tokens $tokens, int $position): string
{
return Regex::replace($docContent, self::PLURAL_TAG_REGEX, '@$1');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Symplify\CodingStandard\Tests\Fixer\Commenting\FixTagTypoFixer;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Symplify\EasyCodingStandard\Testing\PHPUnit\AbstractCheckerTestCase;

final class FixTagTypoFixerTest extends AbstractCheckerTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFiles(__DIR__ . '/Fixture');
}

public function provideConfig(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/**
* @params string $one
* @params string $two
* @returns string
*/
function someFunction(string $one, string $two)
{
}

?>
-----
<?php

/**
* @param string $one
* @param string $two
* @return string
*/
function someFunction(string $one, string $two)
{
}

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

/**
* @param string $one
* @param string $two
* @return string
*/
function correctFunction(string $one, string $two)
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

/**
* @throws \RuntimeException
* @property-read int $id
* @method string getName()
*/
class SomeEntity
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

/**
* This method returns a value and accepts params from the caller.
*
* @param string $name
*/
function describedFunction(string $name)
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

class SomeClass
{
/**
* @vars int
*/
private $count;
}

?>
-----
<?php

class SomeClass
{
/**
* @var int
*/
private $count;
}

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

declare(strict_types=1);

use Symplify\CodingStandard\Fixer\Commenting\FixTagTypoFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;

return static function (ECSConfig $ecsConfig): void {
$ecsConfig->rule(FixTagTypoFixer::class);
};
4 changes: 4 additions & 0 deletions src/Config/Level/DocblockLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use Symplify\CodingStandard\Fixer\Commenting\AddMissingVarNameFixer;
use Symplify\CodingStandard\Fixer\Commenting\DoubleAsteriskInlineVarFixer;
use Symplify\CodingStandard\Fixer\Commenting\FixParamNameTypoFixer;
use Symplify\CodingStandard\Fixer\Commenting\FixTagTypoFixer;
use Symplify\CodingStandard\Fixer\Commenting\RemoveDeadParamFixer;
use Symplify\CodingStandard\Fixer\Commenting\RemoveDeadVarThisFixer;
use Symplify\CodingStandard\Fixer\Commenting\RemoveParamNameReferenceFixer;
Expand All @@ -57,6 +58,9 @@ final class DocblockLevel
* @var array<class-string<Sniff|FixerInterface>>
*/
public const array RULES = [
// tag name typos (@returns → @return), run early so later rules see correct tags
FixTagTypoFixer::class,

// inline @var
DoubleAsteriskInlineVarFixer::class,
RemoveDeadVarThisFixer::class,
Expand Down
3 changes: 2 additions & 1 deletion src/Configuration/ConfigInitializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Nette\Utils\FileSystem;
use Symplify\EasyCodingStandard\Application\FileProcessorCollector;
use Symplify\EasyCodingStandard\Console\Style\EasyCodingStandardStyle;
use Symplify\EasyCodingStandard\Contract\Application\FileProcessorInterface;

final readonly class ConfigInitializer
{
Expand All @@ -21,7 +22,7 @@ public function __construct(
public function areSomeCheckersRegistered(): bool
{
$fileProcessors = $this->fileProcessorCollector->getFileProcessors();
return array_any($fileProcessors, fn ($fileProcessor): bool => $fileProcessor->getCheckers() !== []);
return array_any($fileProcessors, fn (FileProcessorInterface $fileProcessor): bool => $fileProcessor->getCheckers() !== []);
}

public function createConfig(string $projectDirectory): void
Expand Down
Loading