1<?php
2/**
3 * Verifies that class members have scope modifiers.
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('PHP_CodeSniffer_Standards_AbstractVariableSniff', true) === false) {
17    throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Standards_AbstractVariableSniff not found');
18}
19
20/**
21 * Verifies that class members have scope modifiers.
22 *
23 * @category  PHP
24 * @package   PHP_CodeSniffer
25 * @author    Greg Sherwood <gsherwood@squiz.net>
26 * @author    Marc McIntyre <mmcintyre@squiz.net>
27 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
28 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
29 * @version   Release: @package_version@
30 * @link      http://pear.php.net/package/PHP_CodeSniffer
31 */
32class Squiz_Sniffs_Scope_MemberVarScopeSniff extends PHP_CodeSniffer_Standards_AbstractVariableSniff
33{
34
35
36    /**
37     * Processes the function tokens within the class.
38     *
39     * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
40     * @param int                  $stackPtr  The position where the token was found.
41     *
42     * @return void
43     */
44    protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
45    {
46        $tokens = $phpcsFile->getTokens();
47
48        $modifier = null;
49        for ($i = ($stackPtr - 1); $i > 0; $i--) {
50            if ($tokens[$i]['line'] < $tokens[$stackPtr]['line']) {
51                break;
52            } else if (isset(PHP_CodeSniffer_Tokens::$scopeModifiers[$tokens[$i]['code']]) === true) {
53                $modifier = $i;
54                break;
55            }
56        }
57
58        if ($modifier === null) {
59            $error = 'Scope modifier not specified for member variable "%s"';
60            $data  = array($tokens[$stackPtr]['content']);
61            $phpcsFile->addError($error, $stackPtr, 'Missing', $data);
62        }
63
64    }//end processMemberVar()
65
66
67    /**
68     * Processes normal variables.
69     *
70     * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
71     * @param int                  $stackPtr  The position where the token was found.
72     *
73     * @return void
74     */
75    protected function processVariable(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
76    {
77        /*
78            We don't care about normal variables.
79        */
80
81    }//end processVariable()
82
83
84    /**
85     * Processes variables in double quoted strings.
86     *
87     * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
88     * @param int                  $stackPtr  The position where the token was found.
89     *
90     * @return void
91     */
92    protected function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
93    {
94        /*
95            We don't care about normal variables.
96        */
97
98    }//end processVariableInString()
99
100
101}//end class
102