1<?php
2/**
3 * Parses and verifies the doc comments for functions.
4 *
5 * PHP version 5
6 *
7 * @category  PHP
8 * @package   PHP_CodeSniffer
9 * @author    Greg Sherwood <gsherwood@squiz.net>
10 * @author    Marc McIntyre <mmcintyre@squiz.net>
11 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
12 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
13 * @link      http://pear.php.net/package/PHP_CodeSniffer
14 */
15
16if (class_exists('Squiz_Sniffs_Commenting_FunctionCommentSniff', true) === false) {
17    $error = 'Class Squiz_Sniffs_Commenting_FunctionCommentSniff not found';
18    throw new PHP_CodeSniffer_Exception($error);
19}
20
21/**
22 * Parses and verifies the doc comments for functions.
23 *
24 * Same as the Squiz standard, but adds support for API tags.
25 *
26 * @category  PHP
27 * @package   PHP_CodeSniffer
28 * @author    Greg Sherwood <gsherwood@squiz.net>
29 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
30 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
31 * @version   Release: @package_version@
32 * @link      http://pear.php.net/package/PHP_CodeSniffer
33 */
34class MySource_Sniffs_Commenting_FunctionCommentSniff extends Squiz_Sniffs_Commenting_FunctionCommentSniff
35{
36
37
38    /**
39     * Processes this test, when one of its tokens is encountered.
40     *
41     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
42     * @param int                  $stackPtr  The position of the current token
43     *                                        in the stack passed in $tokens.
44     *
45     * @return void
46     */
47    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
48    {
49        parent::process($phpcsFile, $stackPtr);
50
51        $tokens = $phpcsFile->getTokens();
52        $find   = PHP_CodeSniffer_Tokens::$methodPrefixes;
53        $find[] = T_WHITESPACE;
54
55        $commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true);
56        if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG) {
57            return;
58        }
59
60        $commentStart = $tokens[$commentEnd]['comment_opener'];
61        $hasApiTag    = false;
62        foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
63            if ($tokens[$tag]['content'] === '@api') {
64                if ($hasApiTag === true) {
65                    // We've come across an API tag already, which means
66                    // we were not the first tag in the API list.
67                    $error = 'The @api tag must come first in the @api tag list in a function comment';
68                    $phpcsFile->addError($error, $tag, 'ApiNotFirst');
69                }
70
71                $hasApiTag = true;
72
73                // There needs to be a blank line before the @api tag.
74                $prev = $phpcsFile->findPrevious(array(T_DOC_COMMENT_STRING, T_DOC_COMMENT_TAG), ($tag - 1));
75                if ($tokens[$prev]['line'] !== ($tokens[$tag]['line'] - 2)) {
76                    $error = 'There must be one blank line before the @api tag in a function comment';
77                    $phpcsFile->addError($error, $tag, 'ApiSpacing');
78                }
79            } else if (substr($tokens[$tag]['content'], 0, 5) === '@api-') {
80                $hasApiTag = true;
81
82                $prev = $phpcsFile->findPrevious(array(T_DOC_COMMENT_STRING, T_DOC_COMMENT_TAG), ($tag - 1));
83                if ($tokens[$prev]['line'] !== ($tokens[$tag]['line'] - 1)) {
84                    $error = 'There must be no blank line before the @%s tag in a function comment';
85                    $data  = array($tokens[$tag]['content']);
86                    $phpcsFile->addError($error, $tag, 'ApiTagSpacing', $data);
87                }
88            }//end if
89        }//end foreach
90
91        if ($hasApiTag === true && substr($tokens[$tag]['content'], 0, 4) !== '@api') {
92            // API tags must be the last tags in a function comment.
93            $error = 'The @api tags must be the last tags in a function comment';
94            $phpcsFile->addError($error, $commentEnd, 'ApiNotLast');
95        }
96
97    }//end process()
98
99
100}//end class
101