~ubuntu-branches/ubuntu/vivid/php-codesniffer/vivid

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): David Prévot, Greg Sherwood, Alexey, Emily, David Prévot
  • Date: 2014-09-26 13:44:35 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20140926134435-wvjq16miqq4d60y0
Tags: 1.5.5-1
[ Greg Sherwood ]
* Improved closure support in Generic ScopeIndentSniff
* Improved indented PHP tag support in Generic ScopeIndentSniff
* Standards can now be located within hidden directories
 (further fix for bug #20323)
* Fixed bug #20373 : Inline comment sniff tab handling way
* Fixed bug #20378 : Report appended to existing file if no errors
  found in run
* Fixed bug #20381 : Invalid "Comment closer must be on a new line"
* PHP tokenizer no longer converts class/function names to special
  tokens types
* Fixed bug #20386 : Squiz.Commenting.ClassComment.SpacingBefore
  thrown if first block comment
* Squiz and PEAR FunctionCommentSnif now support _()
* PEAR ValidFunctionNameSniff no longer throws an error for _()
* Fixed bug #248 : FunctionCommentSniff expects ampersand on param name
* Fixed bug #248 in Squiz sniff as well
* Fixed bug #265 : False positives with type hints in ForbiddenFunctionsSniff
* Prepare for 1.5.5 release

[ Alexey ]
* Allowed single undersored methods and functions

[ Emily ]
* Added var_dump to discouraged functions sniff

[ David Prévot ]
* Revert "Add XS-Testsuite still needed for ci.d.n"
* Add self to uploaders
* Bump standards version to 3.9.6

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-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
 
 
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-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: 1.5.5
 
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
 
 
94
    }//end processVariable()
 
95
 
 
96
 
 
97
    /**
 
98
     * Processes variables in double quoted strings.
 
99
     *
 
100
     * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
 
101
     * @param int                  $stackPtr  The position where the token was found.
 
102
     *
 
103
     * @return void
 
104
     */
 
105
    protected function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 
106
    {
 
107
        // We don't care about normal variables.
 
108
 
 
109
    }//end processVariableInString()
 
110
 
 
111
 
 
112
}//end class
 
113
 
 
114
?>