22
33namespace Flyeralarm \CodingGuidelines \Flyeralarm \Sniffs \File ;
44
5+ use PHP_CodeSniffer \Exceptions \RuntimeException ;
56use PHP_CodeSniffer \Sniffs \Sniff ;
67use PHP_CodeSniffer \Files \File ;
78
@@ -12,26 +13,26 @@ class ExceptionMessageSniff implements Sniff
1213 */
1314 public function register ()
1415 {
15- return array ( T_CLASS ) ;
16+ return [ T_CLASS ] ;
1617 }
1718
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
3233 $ tokens = $ phpcsFile ->getTokens ();
3334 $ ptr = -1 ;
34- while ($ ptr = $ phpcsFile ->findNext (T_CONSTANT_ENCAPSED_STRING , $ ptr + 1 )) {
35+ while ($ ptr = $ phpcsFile ->findNext (T_CONSTANT_ENCAPSED_STRING , $ ptr + 1 )) {
3536 if (strpos ($ tokens [$ ptr ]['content ' ], '! ' ) !== false ) {
3637 $ phpcsFile ->addError (
3738 'Exclamationmarks are not allowed in Exceptionmessages ' ,
@@ -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