Skip to content

Commit 666ddf0

Browse files
IN-1729 - Fix ExceptionMessageSniff to reduce target only to classes ending with *Exception
* The problem with checking all strings in class body instead only of message still exists * https://github.com/flyeralarm/php-code-validator/pull/29\#issuecomment-1481121548
1 parent 1e781bd commit 666ddf0

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

custom-standards/Flyeralarm/Sniffs/File/ExceptionMessageSniff.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Flyeralarm\CodingGuidelines\Flyeralarm\Sniffs\File;
44

5+
use PHP_CodeSniffer\Exceptions\RuntimeException;
56
use PHP_CodeSniffer\Sniffs\Sniff;
67
use PHP_CodeSniffer\Files\File;
78

@@ -18,14 +19,14 @@ public function register()
1819
/**
1920
* @param File $phpcsFile
2021
* @param int $stackPtr
21-
* @return int|void
22-
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException
22+
* @return void
23+
* @throws RuntimeException
2324
*/
2425
public function process(File $phpcsFile, $stackPtr)
2526
{
2627
$className = $phpcsFile->getDeclarationName($stackPtr);
2728

28-
if (strpos($className, 'Exception') === false) {
29+
if (! $this->doesStringEndsWith($className, 'Exception')) {
2930
return;
3031
}
3132

@@ -49,4 +50,13 @@ public function process(File $phpcsFile, $stackPtr)
4950
}
5051
}
5152
}
53+
54+
/**
55+
* @alias str_ends_with in PHP8+
56+
*/
57+
public function doesStringEndsWith(string $className, string $suffix): bool
58+
{
59+
$suffixLength = strlen($suffix);
60+
return ($suffixLength === 0 || 0 === substr_compare($className, $suffix, - $suffixLength));
61+
}
5262
}

0 commit comments

Comments
 (0)