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

PHPCSStandards / PHP_CodeSniffer / 8939618108

03 May 2024 01:17PM UTC coverage: 75.009% (+0.6%) from 74.408%
8939618108

Pull #455

github

web-flow
Merge eeb3c2fad into bb267bb2c
Pull Request #455: Generic/DisallowLongArraySyntax: improve code coverage

4 of 4 new or added lines in 1 file covered. (100.0%)

710 existing lines in 7 files now uncovered.

23078 of 30767 relevant lines covered (75.01%)

59.01 hits per line

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

96.0
/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<int|string>
25
     */
26
    public function register()
3✔
27
    {
28
        $tokens = Tokens::$comparisonTokens;
3✔
29
        unset($tokens[T_COALESCE]);
3✔
30

31
        return $tokens;
3✔
32

33
    }//end register()
34

35

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

60
        if ($previousIndex === false
2✔
61
            || in_array($tokens[$previousIndex]['code'], $relevantTokens, true) === false
3✔
62
        ) {
1✔
63
            return;
3✔
64
        }
65

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

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

78
        if (in_array($tokens[$prevIndex]['code'], Tokens::$arithmeticTokens, true) === true) {
3✔
UNCOV
79
            return;
×
80
        }
81

82
        if ($tokens[$prevIndex]['code'] === T_STRING_CONCAT) {
3✔
UNCOV
83
            return;
×
84
        }
85

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

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

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

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

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

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

132
    }//end process()
2✔
133

134

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

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

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

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

181
            if (isset($staticTokens[$tokens[$i]['code']]) === false) {
3✔
182
                return false;
3✔
183
            }
184
        }
1✔
185

186
        return true;
3✔
187

188
    }//end isArrayStatic()
189

190

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