1<?php
2/**
3 * Squiz_Sniffs_Commenting_ClosingDeclarationCommentSniff.
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
16/**
17 * Squiz_Sniffs_Commenting_ClosingDeclarationCommentSniff.
18 *
19 * Checks the //end ... comments on classes, interfaces and functions.
20 *
21 * @category  PHP
22 * @package   PHP_CodeSniffer
23 * @author    Greg Sherwood <gsherwood@squiz.net>
24 * @author    Marc McIntyre <mmcintyre@squiz.net>
25 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
26 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
27 * @version   Release: @package_version@
28 * @link      http://pear.php.net/package/PHP_CodeSniffer
29 */
30class Squiz_Sniffs_Commenting_ClosingDeclarationCommentSniff implements PHP_CodeSniffer_Sniff
31{
32
33
34    /**
35     * Returns an array of tokens this test wants to listen for.
36     *
37     * @return array
38     */
39    public function register()
40    {
41        return array(
42                T_FUNCTION,
43                T_CLASS,
44                T_INTERFACE,
45               );
46
47    }//end register()
48
49
50    /**
51     * Processes this test, when one of its tokens is encountered.
52     *
53     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
54     * @param int                  $stackPtr  The position of the current token in the
55     *                                        stack passed in $tokens..
56     *
57     * @return void
58     */
59    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
60    {
61        $tokens = $phpcsFile->getTokens();
62
63        if ($tokens[$stackPtr]['code'] === T_FUNCTION) {
64            $methodProps = $phpcsFile->getMethodProperties($stackPtr);
65
66            // Abstract methods do not require a closing comment.
67            if ($methodProps['is_abstract'] === true) {
68                return;
69            }
70
71            // Closures do not require a closing comment.
72            if ($methodProps['is_closure'] === true) {
73                return;
74            }
75
76            // If this function is in an interface then we don't require
77            // a closing comment.
78            if ($phpcsFile->hasCondition($stackPtr, T_INTERFACE) === true) {
79                return;
80            }
81
82            if (isset($tokens[$stackPtr]['scope_closer']) === false) {
83                $error = 'Possible parse error: non-abstract method defined as abstract';
84                $phpcsFile->addWarning($error, $stackPtr, 'Abstract');
85                return;
86            }
87
88            $decName = $phpcsFile->getDeclarationName($stackPtr);
89            $comment = '//end '.$decName.'()';
90        } else if ($tokens[$stackPtr]['code'] === T_CLASS) {
91            $comment = '//end class';
92        } else {
93            $comment = '//end interface';
94        }//end if
95
96        if (isset($tokens[$stackPtr]['scope_closer']) === false) {
97            $error = 'Possible parse error: %s missing opening or closing brace';
98            $data  = array($tokens[$stackPtr]['content']);
99            $phpcsFile->addWarning($error, $stackPtr, 'MissingBrace', $data);
100            return;
101        }
102
103        $closingBracket = $tokens[$stackPtr]['scope_closer'];
104
105        if ($closingBracket === null) {
106            // Possible inline structure. Other tests will handle it.
107            return;
108        }
109
110        $data = array($comment);
111        if (isset($tokens[($closingBracket + 1)]) === false || $tokens[($closingBracket + 1)]['code'] !== T_COMMENT) {
112            $next = $phpcsFile->findNext(T_WHITESPACE, ($closingBracket + 1), null, true);
113            if (rtrim($tokens[$next]['content']) === $comment) {
114                // The comment isn't really missing; it is just in the wrong place.
115                $fix = $phpcsFile->addFixableError('Expected %s directly after closing brace', $closingBracket, 'Misplaced', $data);
116                if ($fix === true) {
117                    $phpcsFile->fixer->beginChangeset();
118                    for ($i = ($closingBracket + 1); $i < $next; $i++) {
119                        $phpcsFile->fixer->replaceToken($i, '');
120                    }
121
122                    // Just in case, because indentation fixes can add indents onto
123                    // these comments and cause us to be unable to fix them.
124                    $phpcsFile->fixer->replaceToken($next, $comment.$phpcsFile->eolChar);
125                    $phpcsFile->fixer->endChangeset();
126                }
127            } else {
128                $fix = $phpcsFile->addFixableError('Expected %s', $closingBracket, 'Missing', $data);
129                if ($fix === true) {
130                    $phpcsFile->fixer->replaceToken($closingBracket, '}'.$comment.$phpcsFile->eolChar);
131                }
132            }
133
134            return;
135        }//end if
136
137        if (rtrim($tokens[($closingBracket + 1)]['content']) !== $comment) {
138            $fix = $phpcsFile->addFixableError('Expected %s', $closingBracket, 'Incorrect', $data);
139            if ($fix === true) {
140                $phpcsFile->fixer->replaceToken(($closingBracket + 1), $comment.$phpcsFile->eolChar);
141            }
142
143            return;
144        }
145
146    }//end process()
147
148
149}//end class
150