Skip to content

Commit 9b70e78

Browse files
Exception message testing
1 parent dc449d2 commit 9b70e78

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
}

ruleset.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
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
</ruleset>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
}

0 commit comments

Comments
 (0)