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

PHPCSStandards / PHP_CodeSniffer / 15036337869

15 May 2025 04:03AM UTC coverage: 78.375% (-0.2%) from 78.556%
15036337869

Pull #856

github

web-flow
Merge 93f570b46 into f5e7943d0
Pull Request #856: [Doc] Cover all errors of PEAR ClassDeclaration

25112 of 32041 relevant lines covered (78.37%)

69.4 hits per line

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

91.43
/src/Standards/Generic/Sniffs/Functions/CallTimePassByReferenceSniff.php
1
<?php
2
/**
3
 * Ensures that variables are not passed by reference when calling a function.
4
 *
5
 * @author    Florian Grandel <jerico.dev@gmail.com>
6
 * @copyright 2009-2014 Florian Grandel
7
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8
 *
9
 * @deprecated 3.12.1
10
 */
11

12
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\Functions;
13

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

19
class CallTimePassByReferenceSniff implements Sniff, DeprecatedSniff
20
{
21

22

23
    /**
24
     * Returns an array of tokens this test wants to listen for.
25
     *
26
     * @return array<int|string>
27
     */
28
    public function register()
3✔
29
    {
30
        return [
1✔
31
            T_STRING,
3✔
32
            T_VARIABLE,
3✔
33
            T_ANON_CLASS,
3✔
34
            T_PARENT,
3✔
35
            T_SELF,
3✔
36
            T_STATIC,
3✔
37
        ];
2✔
38

39
    }//end register()
40

41

42
    /**
43
     * Processes this test, when one of its tokens is encountered.
44
     *
45
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
46
     * @param int                         $stackPtr  The position of the current token
47
     *                                               in the stack passed in $tokens.
48
     *
49
     * @return void
50
     */
51
    public function process(File $phpcsFile, $stackPtr)
3✔
52
    {
53
        $tokens = $phpcsFile->getTokens();
3✔
54

55
        $findTokens   = Tokens::$emptyTokens;
3✔
56
        $findTokens[] = T_BITWISE_AND;
3✔
57

58
        $prev = $phpcsFile->findPrevious($findTokens, ($stackPtr - 1), null, true);
3✔
59

60
        // Skip tokens that are the names of functions
61
        // within their definitions. For example: function myFunction...
62
        // "myFunction" is T_STRING but we should skip because it is not a
63
        // function or method *call*.
64
        $prevCode = $tokens[$prev]['code'];
3✔
65
        if ($prevCode === T_FUNCTION) {
3✔
66
            return;
3✔
67
        }
68

69
        // If the next non-whitespace token after the function or method call
70
        // is not an opening parenthesis then it cant really be a *call*.
71
        $functionName = $stackPtr;
3✔
72
        $openBracket  = $phpcsFile->findNext(
3✔
73
            Tokens::$emptyTokens,
3✔
74
            ($functionName + 1),
3✔
75
            null,
3✔
76
            true
2✔
77
        );
2✔
78

79
        if ($openBracket === false || $tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) {
3✔
80
            return;
3✔
81
        }
82

83
        if (isset($tokens[$openBracket]['parenthesis_closer']) === false) {
3✔
84
            return;
3✔
85
        }
86

87
        $closeBracket = $tokens[$openBracket]['parenthesis_closer'];
3✔
88

89
        $nextSeparator = $openBracket;
3✔
90
        $find          = [
1✔
91
            T_VARIABLE,
3✔
92
            T_OPEN_SHORT_ARRAY,
3✔
93
        ];
2✔
94

95
        while (($nextSeparator = $phpcsFile->findNext($find, ($nextSeparator + 1), $closeBracket)) !== false) {
3✔
96
            if ($tokens[$nextSeparator]['code'] === T_OPEN_SHORT_ARRAY) {
3✔
97
                $nextSeparator = $tokens[$nextSeparator]['bracket_closer'];
3✔
98
                continue;
3✔
99
            }
100

101
            // Make sure the variable belongs directly to this function call
102
            // and is not inside a nested function call or array.
103
            $brackets    = $tokens[$nextSeparator]['nested_parenthesis'];
3✔
104
            $lastBracket = array_pop($brackets);
3✔
105
            if ($lastBracket !== $closeBracket) {
3✔
106
                continue;
3✔
107
            }
108

109
            $tokenBefore = $phpcsFile->findPrevious(
3✔
110
                Tokens::$emptyTokens,
3✔
111
                ($nextSeparator - 1),
3✔
112
                null,
3✔
113
                true
2✔
114
            );
2✔
115

116
            if ($tokens[$tokenBefore]['code'] === T_BITWISE_AND) {
3✔
117
                if ($phpcsFile->isReference($tokenBefore) === false) {
3✔
118
                    continue;
3✔
119
                }
120

121
                // We also want to ignore references used in assignment
122
                // operations passed as function arguments, but isReference()
123
                // sees them as valid references (which they are).
124
                $tokenBefore = $phpcsFile->findPrevious(
3✔
125
                    Tokens::$emptyTokens,
3✔
126
                    ($tokenBefore - 1),
3✔
127
                    null,
3✔
128
                    true
2✔
129
                );
2✔
130

131
                if (isset(Tokens::$assignmentTokens[$tokens[$tokenBefore]['code']]) === true) {
3✔
132
                    continue;
3✔
133
                }
134

135
                // T_BITWISE_AND represents a pass-by-reference.
136
                $error = 'Call-time pass-by-reference calls are prohibited';
3✔
137
                $phpcsFile->addError($error, $tokenBefore, 'NotAllowed');
3✔
138
            }//end if
1✔
139
        }//end while
1✔
140

141
    }//end process()
2✔
142

143

144
    /**
145
     * Provide the version number in which the sniff was deprecated.
146
     *
147
     * @return string
148
     */
149
    public function getDeprecationVersion()
×
150
    {
151
        return 'v3.12.1';
×
152

153
    }//end getDeprecationVersion()
154

155

156
    /**
157
     * Provide the version number in which the sniff will be removed.
158
     *
159
     * @return string
160
     */
161
    public function getRemovalVersion()
×
162
    {
163
        return 'v4.0.0';
×
164

165
    }//end getRemovalVersion()
166

167

168
    /**
169
     * Provide a custom message to display with the deprecation.
170
     *
171
     * @return string
172
     */
173
    public function getDeprecationMessage()
×
174
    {
175
        return '';
×
176

177
    }//end getDeprecationMessage()
178

179

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