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

webimpress / coding-standard / 4086684577

pending completion
4086684577

Pull #178

github

GitHub
Merge 75aa3b533 into 18aa29088
Pull Request #178: Bump phpunit/phpunit from 9.5.20 to 9.6.0

6985 of 6999 relevant lines covered (99.8%)

1.13 hits per line

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

96.88
/src/WebimpressCodingStandard/Sniffs/Operators/TernaryOperatorSniff.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace WebimpressCodingStandard\Sniffs\Operators;
6

7
use PHP_CodeSniffer\Files\File;
8
use PHP_CodeSniffer\Sniffs\Sniff;
9
use PHP_CodeSniffer\Util\Tokens;
10

11
use const T_INLINE_ELSE;
12
use const T_INLINE_THEN;
13
use const T_WHITESPACE;
14

15
class TernaryOperatorSniff implements Sniff
16
{
17
    /**
18
     * @return int[]
19
     */
20
    public function register() : array
21
    {
22
        return [
1✔
23
            T_INLINE_ELSE,
1✔
24
            T_INLINE_THEN,
1✔
25
        ];
1✔
26
    }
27

28
    /**
29
     * @param int $stackPtr
30
     */
31
    public function process(File $phpcsFile, $stackPtr)
32
    {
33
        $tokens = $phpcsFile->getTokens();
1✔
34

35
        $next = $phpcsFile->findNext(Tokens::$emptyTokens, $stackPtr + 1, null, true);
1✔
36
        if ($tokens[$next]['line'] > $tokens[$stackPtr]['line']) {
1✔
37
            $error = 'Invalid position of ternary operator "%s"';
1✔
38
            $data = [$tokens[$stackPtr]['content']];
1✔
39
            $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Invalid', $data);
1✔
40

41
            if ($fix) {
1✔
42
                $isShortTernary = $tokens[$stackPtr]['code'] === T_INLINE_THEN
1✔
43
                    && $tokens[$next]['code'] === T_INLINE_ELSE;
1✔
44

45
                $phpcsFile->fixer->beginChangeset();
1✔
46
                if ($tokens[$stackPtr - 1]['code'] === T_WHITESPACE) {
1✔
47
                    $phpcsFile->fixer->replaceToken($stackPtr - 1, '');
1✔
48
                }
49
                $phpcsFile->fixer->replaceToken($stackPtr, '');
1✔
50
                if ($isShortTernary) {
1✔
51
                    if ($tokens[$stackPtr + 1]['code'] === T_WHITESPACE) {
1✔
52
                        $phpcsFile->fixer->replaceToken($stackPtr + 1, '');
1✔
53
                    }
54
                    $phpcsFile->fixer->addContentBefore($next, $tokens[$stackPtr]['content']);
1✔
55
                } else {
56
                    $phpcsFile->fixer->addContentBefore($next, $tokens[$stackPtr]['content'] . ' ');
1✔
57
                }
58
                $phpcsFile->fixer->endChangeset();
1✔
59
            }
60
        }
61

62
        $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, $stackPtr - 1, null, true);
1✔
63
        if ($tokens[$prev]['line'] < $tokens[$stackPtr]['line']) {
1✔
64
            $isThen = $tokens[$stackPtr]['code'] === T_INLINE_THEN;
1✔
65

66
            $token = $isThen
1✔
67
                ? $this->findElse($phpcsFile, $stackPtr)
1✔
68
                : $this->findThen($phpcsFile, $stackPtr);
1✔
69

70
            if ($token === $prev || $token === $next) {
1✔
71
                return;
1✔
72
            }
73

74
            $tokenNext = $phpcsFile->findNext(Tokens::$emptyTokens, $token + 1, null, true);
1✔
75
            $tokenPrev = $phpcsFile->findPrevious(Tokens::$emptyTokens, $token - 1, null, true);
1✔
76
            if ($tokens[$tokenNext]['line'] === $tokens[$token]['line']
1✔
77
                && $tokens[$tokenPrev]['line'] === $tokens[$token]['line']
1✔
78
            ) {
79
                $error = 'Invalid position of ternary operator "%s"';
1✔
80
                $data = [$tokens[$token]['content']];
1✔
81
                $fix = $phpcsFile->addFixableError($error, $token, 'Invalid', $data);
1✔
82

83
                if ($fix) {
1✔
84
                    $phpcsFile->fixer->beginChangeset();
1✔
85
                    if ($tokens[$token - 1]['code'] === T_WHITESPACE) {
1✔
86
                        $phpcsFile->fixer->replaceToken($token - 1, '');
1✔
87
                    }
88
                    $phpcsFile->fixer->addNewlineBefore($token);
1✔
89
                    $phpcsFile->fixer->endChangeset();
1✔
90
                }
91
            }
92
        }
93
    }
94

95
    protected function findThen(File $phpcsFile, int $stackPtr) : ?int
96
    {
97
        $tokens = $phpcsFile->getTokens();
1✔
98
        $count = 0;
1✔
99

100
        $i = $stackPtr;
1✔
101
        while ($i = $phpcsFile->findPrevious([T_INLINE_ELSE, T_INLINE_THEN], $i - 1)) {
1✔
102
            if ($tokens[$i]['code'] === T_INLINE_ELSE) {
1✔
103
                ++$count;
1✔
104
            } else {
105
                --$count;
1✔
106

107
                if ($count < 0) {
1✔
108
                    return $i;
1✔
109
                }
110
            }
111
        }
112

113
        return null;
×
114
    }
115

116
    protected function findElse(File $phpcsFile, int $stackPtr) : ?int
117
    {
118
        $tokens = $phpcsFile->getTokens();
1✔
119
        $count = 0;
1✔
120

121
        $i = $stackPtr;
1✔
122
        while ($i = $phpcsFile->findNext([T_INLINE_ELSE, T_INLINE_THEN], $i + 1)) {
1✔
123
            if ($tokens[$i]['code'] === T_INLINE_THEN) {
1✔
124
                ++$count;
1✔
125
            } else {
126
                --$count;
1✔
127

128
                if ($count < 0) {
1✔
129
                    return $i;
1✔
130
                }
131
            }
132
        }
133

134
        return null;
×
135
    }
136
}
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