~ubuntu-branches/ubuntu/trusty/php-codesniffer/trusty

« back to all changes in this revision

Viewing changes to PHP_CodeSniffer-1.3.4/CodeSniffer/Standards/PEAR/Sniffs/NamingConventions/ValidVariableNameSniff.php

  • Committer: Package Import Robot
  • Author(s): Thomas Goirand
  • Date: 2013-07-12 15:16:25 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20130712151625-4autdc0twzbueha9
Tags: 1.5.0~rc2-1
* New upstream release.
* Refreshed patch.
* Standards-Version is now 3.9.4.
* Canonical VCS URLs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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-2011 Squiz Pty Ltd (ABN 77 084 670 600)
11
 
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
12
 
 * @link      http://pear.php.net/package/PHP_CodeSniffer
13
 
 */
14
 
 
15
 
if (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-2011 Squiz Pty Ltd (ABN 77 084 670 600)
29
 
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
30
 
 * @version   Release: 1.3.4
31
 
 * @link      http://pear.php.net/package/PHP_CodeSniffer
32
 
 */
33
 
class 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
 
        $isPublic       = ($memberProps['scope'] === 'private') ? false : true;
57
 
        $scope          = $memberProps['scope'];
58
 
        $scopeSpecified = $memberProps['scope_specified'];
59
 
 
60
 
        // If it's a private member, it must have an underscore on the front.
61
 
        if ($isPublic === false && $memberName{0} !== '_') {
62
 
            $error = 'Private member variable "%s" must be prefixed with an underscore';
63
 
            $data  = array($memberName);
64
 
            $phpcsFile->addError($error, $stackPtr, 'PrivateNoUnderscore', $data);
65
 
            return;
66
 
        }
67
 
 
68
 
        // If it's not a private member, it must not have an underscore on the front.
69
 
        if ($isPublic === true && $scopeSpecified === true && $memberName{0} === '_') {
70
 
            $error = '%s member variable "%s" must not be prefixed with an underscore';
71
 
            $data  = array(
72
 
                      ucfirst($scope),
73
 
                      $memberName,
74
 
                     );
75
 
            $phpcsFile->addError($error, $stackPtr, 'PublicUnderscore', $data);
76
 
            return;
77
 
        }
78
 
 
79
 
    }//end processMemberVar()
80
 
 
81
 
 
82
 
    /**
83
 
     * Processes normal variables.
84
 
     *
85
 
     * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
86
 
     * @param int                  $stackPtr  The position where the token was found.
87
 
     *
88
 
     * @return void
89
 
     */
90
 
    protected function processVariable(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
91
 
    {
92
 
        // We don't care about normal variables.
93
 
        return;
94
 
 
95
 
    }//end processVariable()
96
 
 
97
 
 
98
 
    /**
99
 
     * Processes variables in double quoted strings.
100
 
     *
101
 
     * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
102
 
     * @param int                  $stackPtr  The position where the token was found.
103
 
     *
104
 
     * @return void
105
 
     */
106
 
    protected function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
107
 
    {
108
 
        // We don't care about normal variables.
109
 
        return;
110
 
 
111
 
    }//end processVariableInString()
112
 
 
113
 
 
114
 
}//end class
115
 
 
116
 
?>