You are here

class ExpectedExceptionSniff in Coder 8.2

Same name and namespace in other branches
  1. 8.3 coder_sniffer/DrupalPractice/Sniffs/Commenting/ExpectedExceptionSniff.php \DrupalPractice\Sniffs\Commenting\ExpectedExceptionSniff
  2. 8.3.x coder_sniffer/DrupalPractice/Sniffs/Commenting/ExpectedExceptionSniff.php \DrupalPractice\Sniffs\Commenting\ExpectedExceptionSniff

Checks that the PHPunit @expectedExcpetion tags are not used.

See https://thephp.cc/news/2016/02/questioning-phpunit-best-practices .

@category PHP @package PHP_CodeSniffer @link http://pear.php.net/package/PHP_CodeSniffer

Hierarchy

Expanded class hierarchy of ExpectedExceptionSniff

File

coder_sniffer/DrupalPractice/Sniffs/Commenting/ExpectedExceptionSniff.php, line 24

Namespace

DrupalPractice\Sniffs\Commenting
View source
class ExpectedExceptionSniff implements Sniff {

  /**
   * Returns an array of tokens this test wants to listen for.
   *
   * @return array
   */
  public function register() {
    return array(
      T_DOC_COMMENT_TAG,
    );
  }

  //end register()

  /**
   * Processes this test, when one of its tokens is encountered.
   *
   * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
   * @param int                         $stackPtr  The position of the current token
   *                                               in the stack passed in $tokens.
   *
   * @return void
   */
  public function process(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile
      ->getTokens();
    $content = $tokens[$stackPtr]['content'];
    if ($content === '@expectedException' || $content === '@expectedExceptionCode' || $content === '@expectedExceptionMessage' || $content === '@expectedExceptionMessageRegExp') {
      $warning = '%s tags should not be used, use $this->setExpectedException() or $this->expectException() instead';
      $phpcsFile
        ->addWarning($warning, $stackPtr, 'TagFound', [
        $content,
      ]);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ExpectedExceptionSniff::process public function Processes this test, when one of its tokens is encountered.
ExpectedExceptionSniff::register public function Returns an array of tokens this test wants to listen for.