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

PHPCSStandards / PHP_CodeSniffer / 7211360172

14 Dec 2023 02:17PM UTC coverage: 63.963% (-0.05%) from 64.01%
7211360172

push

github

jrfnl
Improve test coverage for the AssignmentInCondition sniff

This commit improves the test coverage for the AssignmentInCondition
sniff by adding a few more test case files to exercise the parts of the
sniff code that checks for invalid syntax.

15110 of 23623 relevant lines covered (63.96%)

5.38 hits per line

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

95.65
/src/Standards/Generic/Sniffs/ControlStructures/DisallowYodaConditionsSniff.php
1
<?php
2
/**
3
 * Ban the use of Yoda conditions.
4
 *
5
 * @author    Mponos George <gmponos@gmail.com>
6
 * @author    Mark Scherer <username@example.com>
7
 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
8
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
9
 */
10

11
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\ControlStructures;
12

13
use PHP_CodeSniffer\Files\File;
14
use PHP_CodeSniffer\Sniffs\Sniff;
15
use PHP_CodeSniffer\Util\Tokens;
16

17
class DisallowYodaConditionsSniff implements Sniff
18
{
19

20

21
    /**
22
     * Returns an array of tokens this test wants to listen for.
23
     *
24
     * @return array
25
     */
26
    public function register()
3✔
27
    {
28
        return Tokens::$comparisonTokens;
3✔
29

30
    }//end register()
31

32

33
    /**
34
     * Processes this test, when one of its tokens is encountered.
35
     *
36
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
37
     * @param int                         $stackPtr  The position of the current token in the
38
     *                                               stack passed in $tokens.
39
     *
40
     * @return void
41
     */
42
    public function process(File $phpcsFile, $stackPtr)
3✔
43
    {
44
        $tokens         = $phpcsFile->getTokens();
3✔
45
        $previousIndex  = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
3✔
46
        $relevantTokens = [
2✔
47
            T_CLOSE_SHORT_ARRAY,
3✔
48
            T_CLOSE_PARENTHESIS,
3✔
49
            T_TRUE,
3✔
50
            T_FALSE,
3✔
51
            T_NULL,
3✔
52
            T_LNUMBER,
3✔
53
            T_DNUMBER,
3✔
54
            T_CONSTANT_ENCAPSED_STRING,
3✔
55
        ];
2✔
56

57
        if ($previousIndex === false
3✔
58
            || in_array($tokens[$previousIndex]['code'], $relevantTokens, true) === false
3✔
59
        ) {
60
            return;
3✔
61
        }
62

63
        if ($tokens[$previousIndex]['code'] === T_CLOSE_SHORT_ARRAY) {
3✔
64
            $previousIndex = $tokens[$previousIndex]['bracket_opener'];
3✔
65
            if ($this->isArrayStatic($phpcsFile, $previousIndex) === false) {
3✔
66
                return;
3✔
67
            }
68
        }
69

70
        $prevIndex = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($previousIndex - 1), null, true);
3✔
71
        if ($prevIndex === false) {
3✔
72
            return;
×
73
        }
74

75
        if (in_array($tokens[$prevIndex]['code'], Tokens::$arithmeticTokens, true) === true) {
3✔
76
            return;
×
77
        }
78

79
        if ($tokens[$prevIndex]['code'] === T_STRING_CONCAT) {
3✔
80
            return;
×
81
        }
82

83
        // Is it a parenthesis.
84
        if ($tokens[$previousIndex]['code'] === T_CLOSE_PARENTHESIS) {
3✔
85
            // Check what exists inside the parenthesis.
86
            $closeParenthesisIndex = $phpcsFile->findPrevious(
3✔
87
                Tokens::$emptyTokens,
3✔
88
                ($tokens[$previousIndex]['parenthesis_opener'] - 1),
3✔
89
                null,
3✔
90
                true
3✔
91
            );
2✔
92

93
            if ($closeParenthesisIndex === false || $tokens[$closeParenthesisIndex]['code'] !== T_ARRAY) {
3✔
94
                if ($tokens[$closeParenthesisIndex]['code'] === T_STRING) {
3✔
95
                    return;
3✔
96
                }
97

98
                // If it is not an array check what is inside.
99
                $found = $phpcsFile->findPrevious(
3✔
100
                    T_VARIABLE,
3✔
101
                    ($previousIndex - 1),
3✔
102
                    $tokens[$previousIndex]['parenthesis_opener']
3✔
103
                );
2✔
104

105
                // If a variable exists, it is not Yoda.
106
                if ($found !== false) {
3✔
107
                    return;
3✔
108
                }
109

110
                // If there is nothing inside the parenthesis, it it not a Yoda.
111
                $opener = $tokens[$previousIndex]['parenthesis_opener'];
3✔
112
                $prev   = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($previousIndex - 1), ($opener + 1), true);
3✔
113
                if ($prev === false) {
3✔
114
                    return;
3✔
115
                }
116
            } else if ($tokens[$closeParenthesisIndex]['code'] === T_ARRAY
3✔
117
                && $this->isArrayStatic($phpcsFile, $closeParenthesisIndex) === false
3✔
118
            ) {
119
                return;
3✔
120
            }//end if
121
        }//end if
122

123
        $phpcsFile->addError(
3✔
124
            'Usage of Yoda conditions is not allowed; switch the expression order',
3✔
125
            $stackPtr,
3✔
126
            'Found'
3✔
127
        );
2✔
128

129
    }//end process()
1✔
130

131

132
    /**
133
     * Determines if an array is a static definition.
134
     *
135
     * @param \PHP_CodeSniffer\Files\File $phpcsFile  The file being scanned.
136
     * @param int                         $arrayToken The position of the array token.
137
     *
138
     * @return bool
139
     */
140
    public function isArrayStatic(File $phpcsFile, $arrayToken)
3✔
141
    {
142
        $tokens = $phpcsFile->getTokens();
3✔
143

144
        $arrayEnd = null;
3✔
145
        if ($tokens[$arrayToken]['code'] === T_OPEN_SHORT_ARRAY) {
3✔
146
            $start = $arrayToken;
3✔
147
            $end   = $tokens[$arrayToken]['bracket_closer'];
3✔
148
        } else if ($tokens[$arrayToken]['code'] === T_ARRAY) {
3✔
149
            $start = $tokens[$arrayToken]['parenthesis_opener'];
3✔
150
            $end   = $tokens[$arrayToken]['parenthesis_closer'];
3✔
151
        } else {
152
            return true;
×
153
        }
154

155
        $staticTokens  = Tokens::$emptyTokens;
3✔
156
        $staticTokens += Tokens::$textStringTokens;
3✔
157
        $staticTokens += Tokens::$assignmentTokens;
3✔
158
        $staticTokens += Tokens::$equalityTokens;
3✔
159
        $staticTokens += Tokens::$comparisonTokens;
3✔
160
        $staticTokens += Tokens::$arithmeticTokens;
3✔
161
        $staticTokens += Tokens::$operators;
3✔
162
        $staticTokens += Tokens::$booleanOperators;
3✔
163
        $staticTokens += Tokens::$castTokens;
3✔
164
        $staticTokens += Tokens::$bracketTokens;
3✔
165
        $staticTokens += [
2✔
166
            T_DOUBLE_ARROW => T_DOUBLE_ARROW,
3✔
167
            T_COMMA        => T_COMMA,
3✔
168
            T_TRUE         => T_TRUE,
3✔
169
            T_FALSE        => T_FALSE,
3✔
170
        ];
2✔
171

172
        for ($i = ($start + 1); $i < $end; $i++) {
3✔
173
            if (isset($tokens[$i]['scope_closer']) === true) {
3✔
174
                $i = $tokens[$i]['scope_closer'];
3✔
175
                continue;
3✔
176
            }
177

178
            if (isset($staticTokens[$tokens[$i]['code']]) === false) {
3✔
179
                return false;
3✔
180
            }
181
        }
182

183
        return true;
3✔
184

185
    }//end isArrayStatic()
186

187

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