• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PHPCSStandards / PHP_CodeSniffer / 15253296250

26 May 2025 11:55AM UTC coverage: 78.632% (+0.3%) from 78.375%
15253296250

Pull #1105

github

web-flow
Merge d9441d98f into caf806050
Pull Request #1105: Skip tests when 'git' command is not available

19665 of 25009 relevant lines covered (78.63%)

88.67 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.75
/src/Standards/PEAR/Sniffs/NamingConventions/ValidVariableNameSniff.php
1
<?php
2
/**
3
 * Checks the naming of member variables.
4
 *
5
 * @author    Greg Sherwood <gsherwood@squiz.net>
6
 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
7
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8
 */
9

10
namespace PHP_CodeSniffer\Standards\PEAR\Sniffs\NamingConventions;
11

12
use PHP_CodeSniffer\Exceptions\RuntimeException;
13
use PHP_CodeSniffer\Files\File;
14
use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
15
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
16
use PHP_CodeSniffer\Util\Tokens;
17

18
class ValidVariableNameSniff extends AbstractVariableSniff
19
{
20

21

22
    /**
23
     * Only listen to variables within OO scopes.
24
     */
25
    public function __construct()
3✔
26
    {
27
        AbstractScopeSniff::__construct(Tokens::OO_SCOPE_TOKENS, [T_VARIABLE], false);
3✔
28

29
    }//end __construct()
1✔
30

31

32
    /**
33
     * Processes class member variables.
34
     *
35
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
36
     * @param int                         $stackPtr  The position of the current token
37
     *                                               in the stack passed in $tokens.
38
     *
39
     * @return void
40
     */
41
    protected function processMemberVar(File $phpcsFile, $stackPtr)
3✔
42
    {
43
        try {
44
            $memberProps = $phpcsFile->getMemberProperties($stackPtr);
3✔
45
        } catch (RuntimeException $e) {
3✔
46
            // Parse error: property in enum. Ignore.
47
            return;
3✔
48
        }
49

50
        $tokens         = $phpcsFile->getTokens();
3✔
51
        $memberName     = ltrim($tokens[$stackPtr]['content'], '$');
3✔
52
        $scope          = $memberProps['scope'];
3✔
53
        $scopeSpecified = $memberProps['scope_specified'];
3✔
54

55
        if ($memberProps['scope'] === 'private') {
3✔
56
            $isPublic = false;
3✔
57
        } else {
58
            $isPublic = true;
3✔
59
        }
60

61
        // If it's a private member, it must have an underscore on the front.
62
        if ($isPublic === false && $memberName[0] !== '_') {
3✔
63
            $error = 'Private member variable "%s" must be prefixed with an underscore';
3✔
64
            $data  = [$memberName];
3✔
65
            $phpcsFile->addError($error, $stackPtr, 'PrivateNoUnderscore', $data);
3✔
66
            return;
3✔
67
        }
68

69
        // If it's not a private member, it must not have an underscore on the front.
70
        if ($isPublic === true && $scopeSpecified === true && $memberName[0] === '_') {
3✔
71
            $error = '%s member variable "%s" must not be prefixed with an underscore';
3✔
72
            $data  = [
2✔
73
                ucfirst($scope),
3✔
74
                $memberName,
3✔
75
            ];
2✔
76
            $phpcsFile->addError($error, $stackPtr, 'PublicUnderscore', $data);
3✔
77
            return;
3✔
78
        }
79

80
    }//end processMemberVar()
1✔
81

82

83
    /**
84
     * Processes normal variables.
85
     *
86
     * @param \PHP_CodeSniffer\Files\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(File $phpcsFile, $stackPtr)
3✔
92
    {
93
        /*
94
            We don't care about normal variables.
95
        */
96

97
    }//end processVariable()
3✔
98

99

100
    /**
101
     * Processes variables in double quoted strings.
102
     *
103
     * @param \PHP_CodeSniffer\Files\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(File $phpcsFile, $stackPtr)
×
109
    {
110
        /*
111
            We don't care about normal variables.
112
        */
113

114
    }//end processVariableInString()
×
115

116

117
}//end class
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc