Skip to content

Commit c8b0233

Browse files
committed
Refactored custom CodeSniffer rules
1 parent 7b195e7 commit c8b0233

4 files changed

Lines changed: 32 additions & 16 deletions

File tree

src/Sniffer/Sniffs/Arrays/AmbiguousAssociativitySniff.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
class AmbiguousAssociativitySniff implements Sniff
1919
{
20+
private const WARNING = 'Array should be either associative or list of values';
21+
2022
private array $tokens;
2123

2224
public function register(): array
@@ -28,7 +30,7 @@ public function process(File $phpcsFile, $stackPtr): void
2830
{
2931
$this->tokens = $phpcsFile->getTokens();
3032
if ($this->isValidArray($phpcsFile, $stackPtr)) { return; }
31-
$phpcsFile->addWarning('Array should be either associative or list of values', $stackPtr, 'Found');
33+
$phpcsFile->addWarning(self::WARNING, $stackPtr, 'Found');
3234
}
3335

3436
private function isValidArray(File $file, int $idx): bool
@@ -58,7 +60,7 @@ private function isValidArray(File $file, int $idx): bool
5860
break 2;
5961
}
6062
}
61-
return $expected === T_DOUBLE_ARROW ? $this->isTrailingComma($idx) : true;
63+
return $expected !== T_DOUBLE_ARROW || $this->isTrailingComma($idx);
6264
}
6365

6466
private function isTrailingComma(int $idx): bool

src/Sniffer/Sniffs/PhpDoc/CallableDefinitionSniff.php

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,21 @@
1717

1818
final class CallableDefinitionSniff implements Sniff
1919
{
20-
public string $syntax;
21-
public bool $includeClosure = true;
20+
private const WARNING = 'Callable description should contain definition';
21+
22+
private const FORMAT_EXPLAIN = [
23+
'short' => 'fn(ArgType,...) => ReturnType',
24+
'long' => 'function(ArgType,...): ReturnType'
25+
];
2226

23-
private $regexp = [
27+
private const FORMAT_REGEXP = [
2428
'short' => '#fn\([?a-zA-Z\\\\, |]*\) => \??[a-zA-Z\\\\|]+#',
2529
'long' => '#function\([?a-zA-Z\\\\, |]*\): \??[a-zA-Z\\\\|]+#'
2630
];
2731

32+
public string $syntax = 'both';
33+
public bool $includeClosure = true;
34+
2835
public function register(): array
2936
{
3037
return [T_CLASS, T_TRAIT, T_INTERFACE];
@@ -38,25 +45,23 @@ public function process(File $phpcsFile, $stackPtr): void
3845
if ($tag !== '@param' && $tag !== '@return') { continue; }
3946

4047
if (!$this->validDescription($tokens[$stackPtr + 2]['content'], $tag === '@param')) {
41-
$phpcsFile->addWarning('Callable param description should contain definition', $stackPtr, 'Found');
48+
$phpcsFile->addWarning($this->warningMessage(), $stackPtr, 'Found');
4249
}
4350
}
4451
}
4552

46-
private function validDescription(string $line, bool $variable = true): bool
53+
private function validDescription(string $line, bool $forArgument): bool
4754
{
4855
if (!$this->isLambda($line)) { return true; }
4956

50-
$varStart = $variable ? strpos($line, '$', 8) : 1;
57+
$varStart = $forArgument ? strpos($line, '$', 8) : 1;
5158
$descriptionStart = $varStart ? strpos($line, ' ', $varStart) : 0;
5259
$description = $descriptionStart ? trim(substr($line, $descriptionStart)) : '';
5360
if (!$description) { return false; }
5461

55-
if (isset($this->syntax, $this->regexp[$this->syntax])) {
56-
return (bool) preg_match($this->regexp[$this->syntax], $description);
57-
}
58-
59-
foreach ($this->regexp as $syntax => $pattern) {
62+
$selected = self::FORMAT_REGEXP[$this->syntax] ?? null;
63+
$patterns = $selected ? [$selected] : array_values(self::FORMAT_REGEXP);
64+
foreach ($patterns as $pattern) {
6065
if (preg_match($pattern, $description)) { return true; }
6166
}
6267

@@ -75,6 +80,13 @@ private function isLambda(string $line): bool
7580
$type = substr($type, 0, -2);
7681
}
7782

78-
return $type === 'callable' || ($this->includeClosure && $type === 'Closure');
83+
return $type === 'callable' || $this->includeClosure && $type === 'Closure';
84+
}
85+
86+
private function warningMessage(): string
87+
{
88+
$selected = self::FORMAT_EXPLAIN[$this->syntax] ?? null;
89+
$format = $selected ?: implode('` or `', self::FORMAT_EXPLAIN);
90+
return self::WARNING . ' [format: `' . $format . '`]';
7991
}
8092
}

src/Sniffer/Sniffs/PhpDoc/RequiredForPublicApiSniff.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
final class RequiredForPublicApiSniff implements Sniff
2222
{
23+
private const WARNING = 'Missing phpDoc comment for original public method signature';
24+
2325
private array $tokens;
2426

2527
public function register(): array
@@ -51,7 +53,7 @@ public function process(File $phpcsFile, $stackPtr): void
5153

5254
foreach ($undocumented as [$methodName, $stackPtr]) {
5355
if (isset($ancestorMethods[$methodName])) { continue; }
54-
$phpcsFile->addWarning('Missing phpDoc comment for original public method signature', $stackPtr, 'Missing');
56+
$phpcsFile->addWarning(self::WARNING, $stackPtr, 'Missing');
5557
}
5658
}
5759

tests/Sniffer/Sniffs/Arrays/InvalidMixedArrayTypeSniffTest.php renamed to tests/Sniffer/Sniffs/Arrays/AmbiguousAssociativitySniffTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Polymorphine\Dev\Sniffer\Sniffs\Arrays\AmbiguousAssociativitySniff;
1616

1717

18-
class InvalidMixedArrayTypeSniffTest extends SnifferTest
18+
class AmbiguousAssociativitySniffTest extends SnifferTest
1919
{
2020
public function testAssocArrayWithNonAssocValuesGivesWarning()
2121
{

0 commit comments

Comments
 (0)