1<?php
2/**
3 * Generic_Sniffs_Commenting_TodoSniff.
4 *
5 * PHP version 5
6 *
7 * @category  PHP
8 * @package   PHP_CodeSniffer
9 * @author    Greg Sherwood <gsherwood@squiz.net>
10 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
11 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
12 * @link      http://pear.php.net/package/PHP_CodeSniffer
13 */
14
15/**
16 * Generic_Sniffs_Commenting_TodoSniff.
17 *
18 * Warns about TODO comments.
19 *
20 * @category  PHP
21 * @package   PHP_CodeSniffer
22 * @author    Greg Sherwood <gsherwood@squiz.net>
23 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
24 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
25 * @version   Release: @package_version@
26 * @link      http://pear.php.net/package/PHP_CodeSniffer
27 */
28class Generic_Sniffs_Commenting_TodoSniff implements PHP_CodeSniffer_Sniff
29{
30
31    /**
32     * A list of tokenizers this sniff supports.
33     *
34     * @var array
35     */
36    public $supportedTokenizers = array(
37                                   'PHP',
38                                   'JS',
39                                  );
40
41
42    /**
43     * Returns an array of tokens this test wants to listen for.
44     *
45     * @return array
46     */
47    public function register()
48    {
49        return PHP_CodeSniffer_Tokens::$commentTokens;
50
51    }//end register()
52
53
54    /**
55     * Processes this sniff, when one of its tokens is encountered.
56     *
57     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
58     * @param int                  $stackPtr  The position of the current token
59     *                                        in the stack passed in $tokens.
60     *
61     * @return void
62     */
63    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
64    {
65        $tokens = $phpcsFile->getTokens();
66
67        $content = $tokens[$stackPtr]['content'];
68        $matches = array();
69        preg_match('/(?:\A|[^\p{L}]+)todo([^\p{L}]+(.*)|\Z)/ui', $content, $matches);
70        if (empty($matches) === false) {
71            // Clear whitespace and some common characters not required at
72            // the end of a to-do message to make the warning more informative.
73            $type        = 'CommentFound';
74            $todoMessage = trim($matches[1]);
75            $todoMessage = trim($todoMessage, '-:[](). ');
76            $error       = 'Comment refers to a TODO task';
77            $data        = array($todoMessage);
78            if ($todoMessage !== '') {
79                $type   = 'TaskFound';
80                $error .= ' "%s"';
81            }
82
83            $phpcsFile->addWarning($error, $stackPtr, $type, $data);
84        }
85
86    }//end process()
87
88
89}//end class
90