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

PHPCSStandards / PHP_CodeSniffer / 18114150721

30 Sep 2025 12:00AM UTC coverage: 78.601% (-0.6%) from 79.178%
18114150721

push

github

web-flow
Merge pull request #1284 from PHPCSStandards/dependabot/github_actions/3.x/action-runners-f175f89d6c

GH Actions: Bump actions/cache from 4.2.4 to 4.3.0 in the action-runners group

25341 of 32240 relevant lines covered (78.6%)

74.7 hits per line

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

86.54
/src/Standards/MySource/Sniffs/PHP/GetRequestDataSniff.php
1
<?php
2
/**
3
 * Ensures that getRequestData() is used to access super globals.
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/HEAD/licence.txt BSD Licence
8
 *
9
 * @deprecated 3.9.0
10
 */
11

12
namespace PHP_CodeSniffer\Standards\MySource\Sniffs\PHP;
13

14
use PHP_CodeSniffer\Sniffs\DeprecatedSniff;
15
use PHP_CodeSniffer\Sniffs\Sniff;
16
use PHP_CodeSniffer\Files\File;
17

18
class GetRequestDataSniff implements Sniff, DeprecatedSniff
19
{
20

21

22
    /**
23
     * Returns an array of tokens this test wants to listen for.
24
     *
25
     * @return array<int|string>
26
     */
27
    public function register()
3✔
28
    {
29
        return [T_VARIABLE];
3✔
30

31
    }//end register()
32

33

34
    /**
35
     * Processes this sniff, when one of its tokens is encountered.
36
     *
37
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
38
     * @param int                         $stackPtr  The position of the current token in
39
     *                                               the stack passed in $tokens.
40
     *
41
     * @return void
42
     */
43
    public function process(File $phpcsFile, $stackPtr)
3✔
44
    {
45
        $tokens = $phpcsFile->getTokens();
3✔
46

47
        $varName = $tokens[$stackPtr]['content'];
3✔
48
        if ($varName !== '$_REQUEST'
2✔
49
            && $varName !== '$_GET'
3✔
50
            && $varName !== '$_POST'
3✔
51
            && $varName !== '$_FILES'
3✔
52
        ) {
1✔
53
            return;
3✔
54
        }
55

56
        // The only place these super globals can be accessed directly is
57
        // in the getRequestData() method of the Security class.
58
        $inClass = false;
3✔
59
        foreach ($tokens[$stackPtr]['conditions'] as $i => $type) {
3✔
60
            if ($tokens[$i]['code'] === T_CLASS) {
3✔
61
                $className = $phpcsFile->findNext(T_STRING, $i);
3✔
62
                $className = $tokens[$className]['content'];
3✔
63
                if (strtolower($className) === 'security') {
3✔
64
                    $inClass = true;
3✔
65
                } else {
1✔
66
                    // We don't have nested classes.
67
                    break;
3✔
68
                }
69
            } else if ($inClass === true && $tokens[$i]['code'] === T_FUNCTION) {
3✔
70
                $funcName = $phpcsFile->findNext(T_STRING, $i);
3✔
71
                $funcName = $tokens[$funcName]['content'];
3✔
72
                if (strtolower($funcName) === 'getrequestdata') {
3✔
73
                    // This is valid.
74
                    return;
3✔
75
                } else {
76
                    // We don't have nested functions.
77
                    break;
×
78
                }
79
            }//end if
80
        }//end foreach
1✔
81

82
        // If we get to here, the super global was used incorrectly.
83
        // First find out how it is being used.
84
        $globalName = strtolower(substr($varName, 2));
3✔
85
        $usedVar    = '';
3✔
86

87
        $openBracket = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
3✔
88
        if ($tokens[$openBracket]['code'] === T_OPEN_SQUARE_BRACKET) {
3✔
89
            $closeBracket = $tokens[$openBracket]['bracket_closer'];
3✔
90
            $usedVar      = $phpcsFile->getTokensAsString(($openBracket + 1), ($closeBracket - $openBracket - 1));
3✔
91
        }
1✔
92

93
        $type  = 'SuperglobalAccessed';
3✔
94
        $error = 'The %s super global must not be accessed directly; use Security::getRequestData(';
3✔
95
        $data  = [$varName];
3✔
96
        if ($usedVar !== '') {
3✔
97
            $type  .= 'WithVar';
3✔
98
            $error .= '%s, \'%s\'';
3✔
99
            $data[] = $usedVar;
3✔
100
            $data[] = $globalName;
3✔
101
        }
1✔
102

103
        $error .= ') instead';
3✔
104
        $phpcsFile->addError($error, $stackPtr, $type, $data);
3✔
105

106
    }//end process()
2✔
107

108

109
    /**
110
     * Provide the version number in which the sniff was deprecated.
111
     *
112
     * @return string
113
     */
114
    public function getDeprecationVersion()
×
115
    {
116
        return 'v3.9.0';
×
117

118
    }//end getDeprecationVersion()
119

120

121
    /**
122
     * Provide the version number in which the sniff will be removed.
123
     *
124
     * @return string
125
     */
126
    public function getRemovalVersion()
×
127
    {
128
        return 'v4.0.0';
×
129

130
    }//end getRemovalVersion()
131

132

133
    /**
134
     * Provide a custom message to display with the deprecation.
135
     *
136
     * @return string
137
     */
138
    public function getDeprecationMessage()
×
139
    {
140
        return 'The MySource standard will be removed completely in v4.0.0.';
×
141

142
    }//end getDeprecationMessage()
143

144

145
}//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

© 2026 Coveralls, Inc