1<?php
2/**
3 * PEAR_Sniffs_NamingConventions_ValidVariableNameSniff.
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    $error = 'Class PHP_CodeSniffer_Standards_AbstractVariableSniff not found';
17    throw new PHP_CodeSniffer_Exception($error);
18}
19
20/**
21 * PEAR_Sniffs_NamingConventions_ValidVariableNameSniff.
22 *
23 * Checks the naming of member variables.
24 *
25 * @category  PHP
26 * @package   PHP_CodeSniffer
27 * @author    Greg Sherwood <gsherwood@squiz.net>
28 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
29 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
30 * @version   Release: @package_version@
31 * @link      http://pear.php.net/package/PHP_CodeSniffer
32 */
33class PEAR_Sniffs_NamingConventions_ValidVariableNameSniff extends PHP_CodeSniffer_Standards_AbstractVariableSniff
34{
35
36
37    /**
38     * Processes class member variables.
39     *
40     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
41     * @param int                  $stackPtr  The position of the current token
42     *                                        in the stack passed in $tokens.
43     *
44     * @return void
45     */
46    protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
47    {
48        $tokens = $phpcsFile->getTokens();
49
50        $memberProps = $phpcsFile->getMemberProperties($stackPtr);
51        if (empty($memberProps) === true) {
52            return;
53        }
54
55        $memberName     = ltrim($tokens[$stackPtr]['content'], '$');
56        $scope          = $memberProps['scope'];
57        $scopeSpecified = $memberProps['scope_specified'];
58
59        if ($memberProps['scope'] === 'private') {
60            $isPublic = false;
61        } else {
62            $isPublic = true;
63        }
64
65        // If it's a private member, it must have an underscore on the front.
66        if ($isPublic === false && $memberName{0} !== '_') {
67            $error = 'Private member variable "%s" must be prefixed with an underscore';
68            $data  = array($memberName);
69            $phpcsFile->addError($error, $stackPtr, 'PrivateNoUnderscore', $data);
70            return;
71        }
72
73        // If it's not a private member, it must not have an underscore on the front.
74        if ($isPublic === true && $scopeSpecified === true && $memberName{0} === '_') {
75            $error = '%s member variable "%s" must not be prefixed with an underscore';
76            $data  = array(
77                      ucfirst($scope),
78                      $memberName,
79                     );
80            $phpcsFile->addError($error, $stackPtr, 'PublicUnderscore', $data);
81            return;
82        }
83
84    }//end processMemberVar()
85
86
87    /**
88     * Processes normal variables.
89     *
90     * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
91     * @param int                  $stackPtr  The position where the token was found.
92     *
93     * @return void
94     */
95    protected function processVariable(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
96    {
97        /*
98            We don't care about normal variables.
99        */
100
101    }//end processVariable()
102
103
104    /**
105     * Processes variables in double quoted strings.
106     *
107     * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
108     * @param int                  $stackPtr  The position where the token was found.
109     *
110     * @return void
111     */
112    protected function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
113    {
114        /*
115            We don't care about normal variables.
116        */
117
118    }//end processVariableInString()
119
120
121}//end class
122