• 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

82.61
/src/Standards/MySource/Sniffs/PHP/AjaxNullComparisonSniff.php
1
<?php
2
/**
3
 * Ensures that values submitted via JS are not compared to NULL.
4
 *
5
 * With jQuery 1.8, the behavior of ajax requests changed so that null values are
6
 * submitted as null= instead of null=null.
7
 *
8
 * @author    Greg Sherwood <gsherwood@squiz.net>
9
 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
10
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
11
 *
12
 * @deprecated 3.9.0
13
 */
14

15
namespace PHP_CodeSniffer\Standards\MySource\Sniffs\PHP;
16

17
use PHP_CodeSniffer\Sniffs\DeprecatedSniff;
18
use PHP_CodeSniffer\Sniffs\Sniff;
19
use PHP_CodeSniffer\Files\File;
20

21
class AjaxNullComparisonSniff implements Sniff, DeprecatedSniff
22
{
23

24

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

34
    }//end register()
35

36

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

50
        // Make sure it is an API function. We know this by the doc comment.
51
        $commentEnd   = $phpcsFile->findPrevious(T_DOC_COMMENT_CLOSE_TAG, $stackPtr);
3✔
52
        $commentStart = $phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, ($commentEnd - 1));
3✔
53
        // If function doesn't contain any doc comments - skip it.
54
        if ($commentEnd === false || $commentStart === false) {
3✔
55
            return;
3✔
56
        }
57

58
        $comment = $phpcsFile->getTokensAsString($commentStart, ($commentEnd - $commentStart));
3✔
59
        if (strpos($comment, '* @api') === false) {
3✔
60
            return;
3✔
61
        }
62

63
        // Find all the vars passed in as we are only interested in comparisons
64
        // to NULL for these specific variables.
65
        $foundVars = [];
3✔
66
        $open      = $tokens[$stackPtr]['parenthesis_opener'];
3✔
67
        $close     = $tokens[$stackPtr]['parenthesis_closer'];
3✔
68
        for ($i = ($open + 1); $i < $close; $i++) {
3✔
69
            if ($tokens[$i]['code'] === T_VARIABLE) {
3✔
70
                $foundVars[$tokens[$i]['content']] = true;
3✔
71
            }
1✔
72
        }
1✔
73

74
        if (empty($foundVars) === true) {
3✔
75
            return;
×
76
        }
77

78
        $start = $tokens[$stackPtr]['scope_opener'];
3✔
79
        $end   = $tokens[$stackPtr]['scope_closer'];
3✔
80
        for ($i = ($start + 1); $i < $end; $i++) {
3✔
81
            if ($tokens[$i]['code'] !== T_VARIABLE
3✔
82
                || isset($foundVars[$tokens[$i]['content']]) === false
3✔
83
            ) {
1✔
84
                continue;
3✔
85
            }
86

87
            $operator = $phpcsFile->findNext(T_WHITESPACE, ($i + 1), null, true);
3✔
88
            if ($tokens[$operator]['code'] !== T_IS_IDENTICAL
3✔
89
                && $tokens[$operator]['code'] !== T_IS_NOT_IDENTICAL
3✔
90
            ) {
1✔
91
                continue;
3✔
92
            }
93

94
            $nullValue = $phpcsFile->findNext(T_WHITESPACE, ($operator + 1), null, true);
3✔
95
            if ($tokens[$nullValue]['code'] !== T_NULL) {
3✔
96
                continue;
×
97
            }
98

99
            $error = 'Values submitted via Ajax requests should not be compared directly to NULL; use empty() instead';
3✔
100
            $phpcsFile->addWarning($error, $nullValue, 'Found');
3✔
101
        }//end for
1✔
102

103
    }//end process()
2✔
104

105

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

115
    }//end getDeprecationVersion()
116

117

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

127
    }//end getRemovalVersion()
128

129

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

139
    }//end getDeprecationMessage()
140

141

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