File tree Expand file tree Collapse file tree 4 files changed +78
-0
lines changed
custom-standards/Flyeralarm/Sniffs/File
tests/rules/classes/not-allowed Expand file tree Collapse file tree 4 files changed +78
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace flyeralarm \CodingGuidelines \Flyeralarm \Sniffs \File ;
4+
5+ use PHP_CodeSniffer \Sniffs \Sniff ;
6+ use PHP_CodeSniffer \Files \File ;
7+
8+ class ExceptionMessageSniff implements Sniff
9+ {
10+ /**
11+ * @return array
12+ */
13+ public function register ()
14+ {
15+ return array (T_CLASS );
16+ }
17+
18+ /**
19+ * @param File $phpcsFile
20+ * @param int $stackPtr
21+ * @return void
22+ */
23+ public function process (File $ phpcsFile , $ stackPtr )
24+ {
25+ $ className = $ phpcsFile ->getDeclarationName ($ stackPtr );
26+
27+ if (strpos ($ className , 'Exception ' ) === false ) {
28+ return ;
29+ }
30+
31+ $ tokens = $ phpcsFile ->getTokens ();
32+ $ ptr = -1 ;
33+ while ($ ptr = $ phpcsFile ->findNext (T_CONSTANT_ENCAPSED_STRING , $ ptr + 1 )) {
34+ if (strpos ($ tokens [$ ptr ]['content ' ], '! ' ) !== false ) {
35+ $ phpcsFile ->addError (
36+ 'Exclamationmarks are not allowed in Exceptionmessages ' ,
37+ $ stackPtr ,
38+ 'ExceptionMessageWithInvalidCharacter '
39+ );
40+ }
41+
42+ if (strpos ($ tokens [$ ptr ]['content ' ], '. ' ) !== false ) {
43+ $ phpcsFile ->addError (
44+ 'Full stops are not allowed in Exceptionmessages ' ,
45+ $ stackPtr ,
46+ 'ExceptionMessageWithInvalidCharacter '
47+ );
48+ }
49+ }
50+ }
51+ }
Original file line number Diff line number Diff line change 66 <rule ref =" Generic.Arrays.DisallowLongArraySyntax.Found" />
77 <rule ref =" Generic.PHP.DisallowShortOpenTag" />
88 <rule ref =" Generic.NamingConventions.UpperCaseConstantName" />
9+ <rule ref =" ./custom-standards/Flyeralarm/Sniffs/File/ExceptionMessageSniff.php" />
910 <rule ref =" ./custom-standards/Flyeralarm/Sniffs/File/NoClassKindSuffixSniff.php" />
1011 <rule ref =" ./custom-standards/Flyeralarm/Sniffs/Docblock/ReturnTypeSniff.php" />
1112</ruleset >
Original file line number Diff line number Diff line change 1+ <?php
2+ // @expectedError Exclamationmarks are not allowed in Exceptionmessages
3+
4+ class ExclamationException
5+ {
6+ public function foobar ()
7+ {
8+ echo 'Hi ' ;
9+ echo 'Wrong! ' ;
10+ echo 'Bye ' ;
11+ echo "Hola " ;
12+ }
13+ }
Original file line number Diff line number Diff line change 1+ <?php
2+ // @expectedError Full stops are not allowed in Exceptionmessages
3+
4+ class FullStopException
5+ {
6+ public function foobar ()
7+ {
8+ echo 'Hi ' ;
9+ echo 'Bye ' ;
10+ echo "Hola " ;
11+ echo "Ciao. " ;
12+ }
13+ }
You can’t perform that action at this time.
0 commit comments