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

PHPCSStandards / PHP_CodeSniffer / 15253296250

26 May 2025 11:55AM UTC coverage: 78.632% (+0.3%) from 78.375%
15253296250

Pull #1105

github

web-flow
Merge d9441d98f into caf806050
Pull Request #1105: Skip tests when 'git' command is not available

19665 of 25009 relevant lines covered (78.63%)

88.67 hits per line

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

98.72
/src/Standards/Generic/Sniffs/WhiteSpace/LanguageConstructSpacingSniff.php
1
<?php
2
/**
3
 * Ensures all language constructs contain a single space between themselves and their content.
4
 *
5
 * @author    Greg Sherwood <gsherwood@squiz.net>
6
 * @copyright 2006-2017 Squiz Pty Ltd (ABN 77 084 670 600)
7
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8
 */
9

10
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\WhiteSpace;
11

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

17
class LanguageConstructSpacingSniff 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
        return [
2✔
29
            T_ECHO,
3✔
30
            T_PRINT,
3✔
31
            T_RETURN,
3✔
32
            T_INCLUDE,
3✔
33
            T_INCLUDE_ONCE,
3✔
34
            T_REQUIRE,
3✔
35
            T_REQUIRE_ONCE,
3✔
36
            T_NEW,
3✔
37
            T_YIELD,
3✔
38
            T_YIELD_FROM,
3✔
39
            T_THROW,
3✔
40
            T_NAMESPACE,
3✔
41
            T_USE,
3✔
42
            T_GOTO,
3✔
43
        ];
2✔
44

45
    }//end register()
46

47

48
    /**
49
     * Processes this test, when one of its tokens is encountered.
50
     *
51
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
52
     * @param int                         $stackPtr  The position of the current token in
53
     *                                               the stack passed in $tokens.
54
     *
55
     * @return int|void Integer stack pointer to skip forward or void to continue
56
     *                  normal file processing.
57
     */
58
    public function process(File $phpcsFile, $stackPtr)
3✔
59
    {
60
        $tokens = $phpcsFile->getTokens();
3✔
61

62
        $nextToken = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
3✔
63
        if ($nextToken === false) {
3✔
64
            // Skip when at end of file.
65
            return;
3✔
66
        }
67

68
        if ($tokens[($stackPtr + 1)]['code'] === T_SEMICOLON) {
3✔
69
            // No content for this language construct.
70
            return;
3✔
71
        }
72

73
        $content = $tokens[$stackPtr]['content'];
3✔
74
        if ($tokens[$stackPtr]['code'] === T_NAMESPACE) {
3✔
75
            $nextNonEmpty = $phpcsFile->findNext(Tokens::EMPTY_TOKENS, ($stackPtr + 1), null, true);
3✔
76
            if ($nextNonEmpty !== false && $tokens[$nextNonEmpty]['code'] === T_NAME_FULLY_QUALIFIED) {
3✔
77
                // Namespace keyword used as operator, not as the language construct.
78
                // Note: in PHP >= 8 namespaced names no longer allow for whitespace/comments between the parts (parse error).
79
                return;
3✔
80
            }
81
        }
82

83
        if ($tokens[$stackPtr]['code'] === T_YIELD_FROM
3✔
84
            && strtolower($content) !== 'yield from'
3✔
85
        ) {
86
            $found        = $content;
3✔
87
            $hasComment   = false;
3✔
88
            $yieldFromEnd = $stackPtr;
3✔
89

90
            // Handle potentially multi-line/multi-token "yield from" expressions.
91
            if (preg_match('`yield\s+from`i', $content) !== 1) {
3✔
92
                for ($i = ($stackPtr + 1); $i < $phpcsFile->numTokens; $i++) {
3✔
93
                    if (isset(Tokens::EMPTY_TOKENS[$tokens[$i]['code']]) === false
3✔
94
                        && $tokens[$i]['code'] !== T_YIELD_FROM
3✔
95
                    ) {
96
                        break;
×
97
                    }
98

99
                    if (isset(Tokens::COMMENT_TOKENS[$tokens[$i]['code']]) === true) {
3✔
100
                        $hasComment = true;
3✔
101
                    }
102

103
                    $found .= $tokens[$i]['content'];
3✔
104

105
                    if ($tokens[$i]['code'] === T_YIELD_FROM
3✔
106
                        && strtolower(trim($tokens[$i]['content'])) === 'from'
3✔
107
                    ) {
108
                        break;
3✔
109
                    }
110
                }
111

112
                $yieldFromEnd = $i;
3✔
113
            }//end if
114

115
            $error = 'Language constructs must be followed by a single space; expected 1 space between YIELD FROM found "%s"';
3✔
116
            $data  = [Common::prepareForOutput($found)];
3✔
117

118
            if ($hasComment === true) {
3✔
119
                $phpcsFile->addError($error, $stackPtr, 'IncorrectYieldFromWithComment', $data);
3✔
120
            } else {
121
                $fix = $phpcsFile->addFixableError($error, $stackPtr, 'IncorrectYieldFrom', $data);
3✔
122
                if ($fix === true) {
3✔
123
                    preg_match('/yield/i', $found, $yield);
3✔
124
                    preg_match('/from/i', $found, $from);
3✔
125
                    $phpcsFile->fixer->beginChangeset();
3✔
126
                    $phpcsFile->fixer->replaceToken($stackPtr, $yield[0].' '.$from[0]);
3✔
127

128
                    for ($i = ($stackPtr + 1); $i <= $yieldFromEnd; $i++) {
3✔
129
                        $phpcsFile->fixer->replaceToken($i, '');
3✔
130
                    }
131

132
                    $phpcsFile->fixer->endChangeset();
3✔
133
                }
134
            }//end if
135

136
            return ($yieldFromEnd + 1);
3✔
137
        }//end if
138

139
        if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE) {
3✔
140
            $content = $tokens[($stackPtr + 1)]['content'];
3✔
141
            if ($content !== ' ') {
3✔
142
                $error = 'Language constructs must be followed by a single space; expected 1 space but found "%s"';
3✔
143
                $data  = [Common::prepareForOutput($content)];
3✔
144
                $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'IncorrectSingle', $data);
3✔
145
                if ($fix === true) {
3✔
146
                    $phpcsFile->fixer->replaceToken(($stackPtr + 1), ' ');
3✔
147
                }
148
            }
149
        } else if ($tokens[($stackPtr + 1)]['code'] !== T_OPEN_PARENTHESIS) {
3✔
150
            $error = 'Language constructs must be followed by a single space; expected "%s" but found "%s"';
3✔
151
            $data  = [
2✔
152
                $tokens[$stackPtr]['content'].' '.$tokens[($stackPtr + 1)]['content'],
3✔
153
                $tokens[$stackPtr]['content'].$tokens[($stackPtr + 1)]['content'],
3✔
154
            ];
2✔
155
            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'Incorrect', $data);
3✔
156
            if ($fix === true) {
3✔
157
                $phpcsFile->fixer->addContent($stackPtr, ' ');
3✔
158
            }
159
        }//end if
160

161
    }//end process()
1✔
162

163

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