1<?php
2/**
3 * Verifies that properties are declared correctly.
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
15if (class_exists('PHP_CodeSniffer_Standards_AbstractVariableSniff', true) === false) {
16    throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Standards_AbstractVariableSniff not found');
17}
18
19/**
20 * Verifies that properties are declared correctly.
21 *
22 * @category  PHP
23 * @package   PHP_CodeSniffer
24 * @author    Greg Sherwood <gsherwood@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 PSR2_Sniffs_Classes_PropertyDeclarationSniff extends PHP_CodeSniffer_Standards_AbstractVariableSniff
31{
32
33
34    /**
35     * Processes the function tokens within the class.
36     *
37     * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
38     * @param int                  $stackPtr  The position where the token was found.
39     *
40     * @return void
41     */
42    protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
43    {
44        $tokens = $phpcsFile->getTokens();
45
46        if ($tokens[$stackPtr]['content'][1] === '_') {
47            $error = 'Property name "%s" should not be prefixed with an underscore to indicate visibility';
48            $data  = array($tokens[$stackPtr]['content']);
49            $phpcsFile->addWarning($error, $stackPtr, 'Underscore', $data);
50        }
51
52        // Detect multiple properties defined at the same time. Throw an error
53        // for this, but also only process the first property in the list so we don't
54        // repeat errors.
55        $find = PHP_CodeSniffer_Tokens::$scopeModifiers;
56        $find = array_merge($find, array(T_VARIABLE, T_VAR, T_SEMICOLON));
57        $prev = $phpcsFile->findPrevious($find, ($stackPtr - 1));
58        if ($tokens[$prev]['code'] === T_VARIABLE) {
59            return;
60        }
61
62        if ($tokens[$prev]['code'] === T_VAR) {
63            $error = 'The var keyword must not be used to declare a property';
64            $phpcsFile->addError($error, $stackPtr, 'VarUsed');
65        }
66
67        $next = $phpcsFile->findNext(array(T_VARIABLE, T_SEMICOLON), ($stackPtr + 1));
68        if ($tokens[$next]['code'] === T_VARIABLE) {
69            $error = 'There must not be more than one property declared per statement';
70            $phpcsFile->addError($error, $stackPtr, 'Multiple');
71        }
72
73        $modifier = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$scopeModifiers, $stackPtr);
74        if (($modifier === false) || ($tokens[$modifier]['line'] !== $tokens[$stackPtr]['line'])) {
75            $error = 'Visibility must be declared on property "%s"';
76            $data  = array($tokens[$stackPtr]['content']);
77            $phpcsFile->addError($error, $stackPtr, 'ScopeMissing', $data);
78        }
79
80    }//end processMemberVar()
81
82
83    /**
84     * Processes normal variables.
85     *
86     * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
87     * @param int                  $stackPtr  The position where the token was found.
88     *
89     * @return void
90     */
91    protected function processVariable(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
92    {
93        /*
94            We don't care about normal variables.
95        */
96
97    }//end processVariable()
98
99
100    /**
101     * Processes variables in double quoted strings.
102     *
103     * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
104     * @param int                  $stackPtr  The position where the token was found.
105     *
106     * @return void
107     */
108    protected function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
109    {
110        /*
111            We don't care about normal variables.
112        */
113
114    }//end processVariableInString()
115
116
117}//end class
118