Subversion Repositories Applications.framework

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5 aurelien 1
<?php
2
/**
3
 * Class Declaration Test.
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 Squiz Pty Ltd (ABN 77 084 670 600)
12
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
13
 * @version   CVS: $Id: ClassDeclarationSniff.php,v 1.5 2008/05/19 05:59:25 squiz Exp $
14
 * @link      http://pear.php.net/package/PHP_CodeSniffer
15
 */
16
 
17
/**
18
 * Class Declaration Test.
19
 *
20
 * Checks the declaration of the class is correct.
21
 *
22
 * @category  PHP
23
 * @package   PHP_CodeSniffer
24
 * @author    Greg Sherwood <gsherwood@squiz.net>
25
 * @author    Marc McIntyre <mmcintyre@squiz.net>
26
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
27
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
28
 * @version   Release: 1.2.0RC1
29
 * @link      http://pear.php.net/package/PHP_CodeSniffer
30
 */
31
class PEAR_Sniffs_Classes_ClassDeclarationSniff implements PHP_CodeSniffer_Sniff
32
{
33
 
34
 
35
    /**
36
     * Returns an array of tokens this test wants to listen for.
37
     *
38
     * @return array
39
     */
40
    public function register()
41
    {
42
        return array(
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 (isset($tokens[$stackPtr]['scope_opener']) === false) {
64
            $error  = 'Possible parse error: ';
65
            $error .= $tokens[$stackPtr]['content'];
66
            $error .= ' missing opening or closing brace';
67
            $phpcsFile->addWarning($error, $stackPtr);
68
            return;
69
        }
70
 
71
        $curlyBrace  = $tokens[$stackPtr]['scope_opener'];
72
        $lastContent = $phpcsFile->findPrevious(T_WHITESPACE, ($curlyBrace - 1), $stackPtr, true);
73
        $classLine   = $tokens[$lastContent]['line'];
74
        $braceLine   = $tokens[$curlyBrace]['line'];
75
        if ($braceLine === $classLine) {
76
            $error  = 'Opening brace of a ';
77
            $error .= $tokens[$stackPtr]['content'];
78
            $error .= ' must be on the line after the definition';
79
            $phpcsFile->addError($error, $curlyBrace);
80
            return;
81
        } else if ($braceLine > ($classLine + 1)) {
82
            $difference  = ($braceLine - $classLine - 1);
83
            $difference .= ($difference === 1) ? ' line' : ' lines';
84
            $error       = 'Opening brace of a ';
85
            $error      .= $tokens[$stackPtr]['content'];
86
            $error      .= ' must be on the line following the ';
87
            $error      .= $tokens[$stackPtr]['content'];
88
            $error      .= ' declaration; found '.$difference;
89
            $phpcsFile->addError($error, $curlyBrace);
90
            return;
91
        }
92
 
93
        if ($tokens[($curlyBrace + 1)]['content'] !== $phpcsFile->eolChar) {
94
            $type  = strtolower($tokens[$stackPtr]['content']);
95
            $error = "Opening $type brace must be on a line by itself";
96
            $phpcsFile->addError($error, $curlyBrace);
97
        }
98
 
99
        if ($tokens[($curlyBrace - 1)]['code'] === T_WHITESPACE) {
100
            $prevContent = $tokens[($curlyBrace - 1)]['content'];
101
            if ($prevContent !== $phpcsFile->eolChar) {
102
                $blankSpace = substr($prevContent, strpos($prevContent, $phpcsFile->eolChar));
103
                $spaces     = strlen($blankSpace);
104
                if ($spaces !== 0) {
105
                    $error = "Expected 0 spaces before opening brace; $spaces found";
106
                    $phpcsFile->addError($error, $curlyBrace);
107
                }
108
            }
109
        }
110
 
111
    }//end process()
112
 
113
 
114
}//end class
115
 
116
?>