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

PHPCSStandards / PHP_CodeSniffer / 9066641192

13 May 2024 04:41PM UTC coverage: 75.021% (+0.006%) from 75.015%
9066641192

push

github

web-flow
Merge pull request #465 from rodrigoprimo/test-coverage-disallow-yoda-condition

Generic/DisallowYodaConditions: improve code coverage

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

1 existing line in 1 file now uncovered.

23081 of 30766 relevant lines covered (75.02%)

59.01 hits per line

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

98.95
/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 (in_array($tokens[$previousIndex]['code'], $relevantTokens, true) === false) {
3✔
61
            return;
3✔
62
        }
63

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

71
        $prevIndex = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($previousIndex - 1), null, true);
3✔
72

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

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

81
        // Is it a parenthesis.
82
        if ($tokens[$previousIndex]['code'] === T_CLOSE_PARENTHESIS) {
3✔
83
            $beforeOpeningParenthesisIndex = $phpcsFile->findPrevious(
3✔
84
                Tokens::$emptyTokens,
3✔
85
                ($tokens[$previousIndex]['parenthesis_opener'] - 1),
3✔
86
                null,
3✔
87
                true
2✔
88
            );
2✔
89

90
            if ($beforeOpeningParenthesisIndex === false || $tokens[$beforeOpeningParenthesisIndex]['code'] !== T_ARRAY) {
3✔
91
                if ($tokens[$beforeOpeningParenthesisIndex]['code'] === T_STRING) {
3✔
92
                    return;
3✔
93
                }
94

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

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

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

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

126
    }//end process()
2✔
127

128

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

141
        if ($tokens[$arrayToken]['code'] === T_OPEN_SHORT_ARRAY) {
3✔
142
            $start = $arrayToken;
3✔
143
            $end   = $tokens[$arrayToken]['bracket_closer'];
3✔
144
        } else if ($tokens[$arrayToken]['code'] === T_ARRAY) {
3✔
145
            $start = $tokens[$arrayToken]['parenthesis_opener'];
3✔
146
            $end   = $tokens[$arrayToken]['parenthesis_closer'];
3✔
147
        } else {
1✔
148
            // Shouldn't be possible but may happen if external sniffs are using this method.
UNCOV
149
            return true;
×
150
        }
151

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

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

175
            if (isset($staticTokens[$tokens[$i]['code']]) === false) {
3✔
176
                return false;
3✔
177
            }
178
        }
1✔
179

180
        return true;
3✔
181

182
    }//end isArrayStatic()
183

184

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