Chris@0
|
1 <?php
|
Chris@0
|
2 /**
|
Chris@17
|
3 * \DrupalPractice\Sniffs\Commenting\ExpectedExceptionSniff.
|
Chris@0
|
4 *
|
Chris@0
|
5 * @category PHP
|
Chris@0
|
6 * @package PHP_CodeSniffer
|
Chris@0
|
7 * @link http://pear.php.net/package/PHP_CodeSniffer
|
Chris@0
|
8 */
|
Chris@0
|
9
|
Chris@17
|
10 namespace DrupalPractice\Sniffs\Commenting;
|
Chris@17
|
11
|
Chris@17
|
12 use PHP_CodeSniffer\Files\File;
|
Chris@17
|
13 use PHP_CodeSniffer\Sniffs\Sniff;
|
Chris@17
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * Checks that the PHPunit @expectedExcpetion tags are not used.
|
Chris@0
|
17 *
|
Chris@0
|
18 * See https://thephp.cc/news/2016/02/questioning-phpunit-best-practices .
|
Chris@0
|
19 *
|
Chris@0
|
20 * @category PHP
|
Chris@0
|
21 * @package PHP_CodeSniffer
|
Chris@0
|
22 * @link http://pear.php.net/package/PHP_CodeSniffer
|
Chris@0
|
23 */
|
Chris@17
|
24 class ExpectedExceptionSniff implements Sniff
|
Chris@0
|
25 {
|
Chris@0
|
26
|
Chris@0
|
27
|
Chris@0
|
28 /**
|
Chris@0
|
29 * Returns an array of tokens this test wants to listen for.
|
Chris@0
|
30 *
|
Chris@0
|
31 * @return array
|
Chris@0
|
32 */
|
Chris@0
|
33 public function register()
|
Chris@0
|
34 {
|
Chris@0
|
35 return array(T_DOC_COMMENT_TAG);
|
Chris@0
|
36
|
Chris@0
|
37 }//end register()
|
Chris@0
|
38
|
Chris@0
|
39
|
Chris@0
|
40 /**
|
Chris@0
|
41 * Processes this test, when one of its tokens is encountered.
|
Chris@0
|
42 *
|
Chris@17
|
43 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
|
Chris@17
|
44 * @param int $stackPtr The position of the current token
|
Chris@17
|
45 * in the stack passed in $tokens.
|
Chris@0
|
46 *
|
Chris@0
|
47 * @return void
|
Chris@0
|
48 */
|
Chris@17
|
49 public function process(File $phpcsFile, $stackPtr)
|
Chris@0
|
50 {
|
Chris@0
|
51 $tokens = $phpcsFile->getTokens();
|
Chris@0
|
52
|
Chris@0
|
53 $content = $tokens[$stackPtr]['content'];
|
Chris@0
|
54 if ($content === '@expectedException' || $content === '@expectedExceptionCode'
|
Chris@0
|
55 || $content === '@expectedExceptionMessage'
|
Chris@0
|
56 || $content === '@expectedExceptionMessageRegExp'
|
Chris@0
|
57 ) {
|
Chris@17
|
58 $warning = '%s tags should not be used, use $this->setExpectedException() or $this->expectException() instead';
|
Chris@0
|
59 $phpcsFile->addWarning($warning, $stackPtr, 'TagFound', [$content]);
|
Chris@0
|
60 }
|
Chris@0
|
61
|
Chris@0
|
62 }//end process()
|
Chris@0
|
63
|
Chris@0
|
64
|
Chris@0
|
65 }//end class
|