|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * WordPoints PHPUnit Missing Covers Sniff class. |
| 5 | + * |
| 6 | + * @package WordPoints_Dev_Lib |
| 7 | + * @since 2.3.0 |
| 8 | + */ |
| 9 | + |
| 10 | +/** |
| 11 | + * Sniff for missing @covers annotations on PHPUnit tests. |
| 12 | + * |
| 13 | + * @since 2.3.0 |
| 14 | + */ |
| 15 | +class WordPoints_Sniffs_PHPUnit_MissingCoversSniff implements PHP_CodeSniffer_Sniff { |
| 16 | + |
| 17 | + /** |
| 18 | + * @since 2.3.0 |
| 19 | + */ |
| 20 | + public function register() { |
| 21 | + return array( T_CLASS, T_FUNCTION ); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * @since 2.3.0 |
| 26 | + */ |
| 27 | + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { |
| 28 | + |
| 29 | + $tokens = $phpcsFile->getTokens(); |
| 30 | + |
| 31 | + $is_class = ! $is_function = T_FUNCTION === $tokens[ $stackPtr ]['code']; |
| 32 | + |
| 33 | + $scope_closer = $tokens[ $stackPtr ]['scope_closer']; |
| 34 | + |
| 35 | + if ( $is_function ) { |
| 36 | + |
| 37 | + $function_name = $phpcsFile->getDeclarationName( $stackPtr ); |
| 38 | + |
| 39 | + // If this isn't a test method, we don't need to check it. |
| 40 | + if ( 'test' !== substr( $function_name, 0, 4 ) ) { |
| 41 | + return $scope_closer; |
| 42 | + } |
| 43 | + |
| 44 | + $class = $phpcsFile->getCondition( $stackPtr, T_CLASS ); |
| 45 | + |
| 46 | + // If it isn't in a class, we don't need to check it. |
| 47 | + if ( ! $class ) { |
| 48 | + return $scope_closer; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + // Find the previous token (excluding scope modifiers and whitespace). |
| 53 | + $exclude = PHP_CodeSniffer_Tokens::$methodPrefixes; |
| 54 | + $exclude[] = T_WHITESPACE; |
| 55 | + |
| 56 | + $comment_closer = $phpcsFile->findPrevious( $exclude, $stackPtr - 1, 0, $exclude ); |
| 57 | + |
| 58 | + // If the token isn't the close of a doc comment, there is no doc comment for |
| 59 | + // this item, and therefore no annotations. |
| 60 | + if ( T_DOC_COMMENT_CLOSE_TAG !== $tokens[ $comment_closer ]['code'] ) { |
| 61 | + |
| 62 | + if ( $is_function ) { |
| 63 | + |
| 64 | + $phpcsFile->addError( |
| 65 | + 'Missing PHPUnit code coverage annotation.' |
| 66 | + , $stackPtr |
| 67 | + , 'NoDocComment' |
| 68 | + ); |
| 69 | + |
| 70 | + return $scope_closer; |
| 71 | + } |
| 72 | + |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + // Now check if the docblock contains the required annotations. |
| 77 | + $comment_opener = $tokens[ $comment_closer ]['comment_opener']; |
| 78 | + |
| 79 | + $has_annotation = false; |
| 80 | + |
| 81 | + foreach ( $tokens[ $comment_opener ]['comment_tags'] as $tag ) { |
| 82 | + |
| 83 | + $tag_name = $tokens[ $tag ]['content']; |
| 84 | + |
| 85 | + if ( '@covers' === $tag_name || '@coversNothing' === $tag_name ) { |
| 86 | + $has_annotation = true; |
| 87 | + break; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + if ( ! $has_annotation ) { |
| 92 | + |
| 93 | + if ( $is_class ) { |
| 94 | + |
| 95 | + // Classes don't have to have the annotation, so this is OK. However, |
| 96 | + // each method will have to have it. |
| 97 | + return null; |
| 98 | + |
| 99 | + } elseif ( $is_function ) { |
| 100 | + |
| 101 | + $phpcsFile->addError( |
| 102 | + 'Missing PHPUnit code coverage annotation.' |
| 103 | + , $stackPtr |
| 104 | + , 'MissingAnnotation' |
| 105 | + ); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + // We skip to the end of the class/function. If we're in a class and we're |
| 110 | + // still here, we found the annotation, so we don't need to check each |
| 111 | + // method. If we're in a function, we don't need to test any sub-functions |
| 112 | + // either, since they won't be test methods. |
| 113 | + return $scope_closer; |
| 114 | + |
| 115 | + } // public function process() |
| 116 | + |
| 117 | +} // class WordPoints_Sniffs_PHPUnit_MissingCoversSniff |
| 118 | + |
| 119 | +// EOF |
0 commit comments