Skip to content

Commit c484cfe

Browse files
Daniel MüllerDaniel Müller
authored andcommitted
Merge pull request #4 in CFA/coding-guidelines from expected-exception to master
* commit '989e38a08f33c16d81f4cd744463e09d603fef2b': - Added newline - Added expected exception name sniff - Fixed test runner problem with empty brackets [ ]
2 parents ef46e2b + 989e38a commit c484cfe

File tree

5 files changed

+110
-1
lines changed

5 files changed

+110
-1
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace flyeralarm\CodingGuidelines\Flyeralarm\Sniffs\Docblock;
4+
5+
use PHP_CodeSniffer\Sniffs\Sniff;
6+
use PHP_CodeSniffer\Files\File;
7+
8+
class ExpectedExceptionMessageSniff implements Sniff
9+
{
10+
/**
11+
* @return array
12+
*/
13+
public function register()
14+
{
15+
return array(T_DOC_COMMENT_OPEN_TAG);
16+
}
17+
18+
/**
19+
* @param File $phpcsFile
20+
* @param int $stackPtr
21+
* @return void
22+
*/
23+
public function process(File $phpcsFile, $stackPtr)
24+
{
25+
if (!$this->hasAnnotationInDoc($phpcsFile, $stackPtr, '@expectedException')) {
26+
return;
27+
}
28+
if ($this->hasAnnotationInDoc($phpcsFile, $stackPtr, '@expectedExceptionMessage')) {
29+
return;
30+
}
31+
32+
$expectedExceptionPtr = $this->getAnnotationPtr($phpcsFile, $stackPtr, '@expectedException');
33+
34+
$phpcsFile->addError(
35+
'Annotation @expectedExceptionMessage missing',
36+
$expectedExceptionPtr,
37+
'AnnotationMissing'
38+
);
39+
}
40+
41+
/**
42+
* @param File $phpcsFile
43+
* @param $docStartPtr
44+
* @return int
45+
*/
46+
private function getAnnotationPtr(File $phpcsFile, $docStartPtr, $annotation)
47+
{
48+
$tokens = $phpcsFile->getTokens();
49+
$commentEndPtr = $tokens[$docStartPtr]['comment_closer'];
50+
for ($currentTokenIndex = $docStartPtr; $currentTokenIndex < $commentEndPtr; $currentTokenIndex++) {
51+
$currentToken = $phpcsFile->findNext(T_DOC_COMMENT_TAG, $currentTokenIndex, $commentEndPtr);
52+
if ($tokens[$currentToken]['content'] == $annotation) {
53+
return $currentToken;
54+
}
55+
}
56+
57+
throw new \OutOfBoundsException('Found no @return tag');
58+
}
59+
60+
/**
61+
* @param File $phpcsFile
62+
* @param int $docStartPtr
63+
* @param string $annotation
64+
* @return bool
65+
*/
66+
public function hasAnnotationInDoc(File $phpcsFile, $docStartPtr, $annotation)
67+
{
68+
$tokens = $phpcsFile->getTokens();
69+
$commentEnd = $tokens[$docStartPtr]['comment_closer'];
70+
for ($currentTokenIndex = $docStartPtr; $currentTokenIndex < $commentEnd; $currentTokenIndex++) {
71+
$currentToken = $phpcsFile->findNext(T_DOC_COMMENT_TAG, $currentTokenIndex, $commentEnd);
72+
if ($tokens[$currentToken]['content'] == $annotation) {
73+
return true;
74+
}
75+
}
76+
77+
return false;
78+
}
79+
}

ruleset.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
<rule ref="./custom-standards/Flyeralarm/Sniffs/File/ForbiddenKeywordsSniff.php"/>
1111
<rule ref="./custom-standards/Flyeralarm/Sniffs/File/NoClassKindSuffixSniff.php"/>
1212
<rule ref="./custom-standards/Flyeralarm/Sniffs/Docblock/ReturnTypeSniff.php"/>
13+
<rule ref="./custom-standards/Flyeralarm/Sniffs/Docblock/ExpectedExceptionMessageSniff.php"/>
1314
</ruleset>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
// @expectedPass
4+
5+
namespace flyeralarm\Test;
6+
7+
class FooTest
8+
{
9+
/**
10+
* @expectedException \RuntimeException
11+
* @expectedExceptionMessage The exception message
12+
*/
13+
public function testBar()
14+
{
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
// @expectedError Annotation @expectedExceptionMessage missing
4+
5+
class FooTest
6+
{
7+
/**
8+
* @expectedException \RuntimeException
9+
*/
10+
public function testBar()
11+
{
12+
}
13+
}

tests/runner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function processDir($dirPath)
4242
continue;
4343
}
4444
$expected = $expectedMatch[1];
45-
if (preg_match('/ERROR\s\|\s?[\[\]x]*\s' . preg_quote($expected, '/') . '/', $snifferOutput) === 0) {
45+
if (preg_match('/ERROR\s\|\s?[\[\]x\s]*\s' . preg_quote($expected, '/') . '/', $snifferOutput) === 0) {
4646
$hasError = true;
4747
echo 'ERROR - [' . $dirPath . $file . ']: Expectation <<' . $expected . '>> not found in result: ' . PHP_EOL . $snifferOutput . PHP_EOL;
4848
} else {

0 commit comments

Comments
 (0)