Subversion Repositories Applications.framework

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5 aurelien 1
<?php
2
/**
3
 * This file is part of the CodeAnalysis addon for PHP_CodeSniffer.
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PHP_CodeSniffer
9
 * @author    Greg Sherwood <gsherwood@squiz.net>
10
 * @author    Manuel Pichler <mapi@manuel-pichler.de>
11
 * @copyright 2007-2008 Manuel Pichler. All rights reserved.
12
 * @license   http://www.opensource.org/licenses/bsd-license.php BSD License
13
 * @version   CVS: $Id: UnnecessaryFinalModifierSniff.php,v 1.1 2008/02/06 02:38:36 squiz Exp $
14
 * @link      http://pear.php.net/package/PHP_CodeSniffer
15
 */
16
 
17
/**
18
 * Detects unnecessary final modifiers inside of final classes.
19
 *
20
 * This rule is based on the PMD rule catalog. The Unnecessary Final Modifier
21
 * sniff detects the use of the final modifier inside of a final class which
22
 * is unnecessary.
23
 *
24
 * <code>
25
 * final class Foo
26
 * {
27
 *     public final function bar()
28
 *     {
29
 *     }
30
 * }
31
 * </code>
32
 *
33
 * @category  PHP
34
 * @package   PHP_CodeSniffer
35
 * @author    Manuel Pichler <mapi@manuel-pichler.de>
36
 * @copyright 2007-2008 Manuel Pichler. All rights reserved.
37
 * @license   http://www.opensource.org/licenses/bsd-license.php BSD License
38
 * @version   Release: 1.2.0RC1
39
 * @link      http://pear.php.net/package/PHP_CodeSniffer
40
 */
41
class Generic_Sniffs_CodeAnalysis_UnnecessaryFinalModifierSniff implements PHP_CodeSniffer_Sniff
42
{
43
 
44
 
45
    /**
46
     * Registers the tokens that this sniff wants to listen for.
47
     *
48
     * @return array(integer)
49
     */
50
    public function register()
51
    {
52
        return array(T_CLASS);
53
 
54
    }//end register()
55
 
56
 
57
    /**
58
     * Processes this test, when one of its tokens is encountered.
59
     *
60
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
61
     * @param int                  $stackPtr  The position of the current token
62
     *                                        in the stack passed in $tokens.
63
     *
64
     * @return void
65
     */
66
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
67
    {
68
        $tokens = $phpcsFile->getTokens();
69
        $token  = $tokens[$stackPtr];
70
 
71
        // Skip for-statements without body.
72
        if (isset($token['scope_opener']) === false) {
73
            return;
74
        }
75
 
76
        // Fetch previous token.
77
        $prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true);
78
 
79
        // Skip for non final class.
80
        if ($prev === false || $tokens[$prev]['code'] !== T_FINAL) {
81
            return;
82
        }
83
 
84
        $next = ++$token['scope_opener'];
85
        $end  = --$token['scope_closer'];
86
 
87
        for (; $next <= $end; ++$next) {
88
            if ($tokens[$next]['code'] === T_FINAL) {
89
                $error = 'Unnecessary FINAL modifier in FINAL class';
90
                $phpcsFile->addWarning($error, $next);
91
            }
92
        }
93
 
94
    }//end process()
95
 
96
 
97
}//end class
98
 
99
?>