• 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

75.47
/src/Standards/Generic/Sniffs/Files/LineEndingsSniff.php
1
<?php
2
/**
3
 * Checks that end of line characters are correct.
4
 *
5
 * @author    Greg Sherwood <gsherwood@squiz.net>
6
 * @copyright 2006-2015 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\Files;
11

12
use PHP_CodeSniffer\Files\File;
13
use PHP_CodeSniffer\Sniffs\Sniff;
14

15
class LineEndingsSniff implements Sniff
16
{
17

18
    /**
19
     * The valid EOL character.
20
     *
21
     * @var string
22
     */
23
    public $eolChar = '\n';
24

25

26
    /**
27
     * Returns an array of tokens this test wants to listen for.
28
     *
29
     * @return array<int|string>
30
     */
31
    public function register()
3✔
32
    {
33
        return [
2✔
34
            T_OPEN_TAG,
3✔
35
            T_OPEN_TAG_WITH_ECHO,
3✔
36
        ];
2✔
37

38
    }//end register()
39

40

41
    /**
42
     * Processes this sniff, when one of its tokens is encountered.
43
     *
44
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
45
     * @param int                         $stackPtr  The position of the current token in
46
     *                                               the stack passed in $tokens.
47
     *
48
     * @return int
49
     */
50
    public function process(File $phpcsFile, $stackPtr)
3✔
51
    {
52
        $found = $phpcsFile->eolChar;
3✔
53
        $found = str_replace("\n", '\n', $found);
3✔
54
        $found = str_replace("\r", '\r', $found);
3✔
55

56
        $phpcsFile->recordMetric($stackPtr, 'EOL char', $found);
3✔
57

58
        if ($found === $this->eolChar) {
3✔
59
            // Ignore the rest of the file.
60
            return $phpcsFile->numTokens;
3✔
61
        }
62

63
        // Check for single line files without an EOL. This is a very special
64
        // case and the EOL char is set to \n when this happens.
65
        if ($found === '\n') {
3✔
66
            $tokens    = $phpcsFile->getTokens();
×
67
            $lastToken = ($phpcsFile->numTokens - 1);
×
68
            if ($tokens[$lastToken]['line'] === 1
×
69
                && $tokens[$lastToken]['content'] !== "\n"
×
70
            ) {
71
                return $phpcsFile->numTokens;
×
72
            }
73
        }
74

75
        $error    = 'End of line character is invalid; expected "%s" but found "%s"';
3✔
76
        $expected = $this->eolChar;
3✔
77
        $expected = str_replace("\n", '\n', $expected);
3✔
78
        $expected = str_replace("\r", '\r', $expected);
3✔
79
        $data     = [
2✔
80
            $expected,
3✔
81
            $found,
3✔
82
        ];
2✔
83

84
        // Errors are always reported on line 1, no matter where the first PHP tag is.
85
        $fix = $phpcsFile->addFixableError($error, 0, 'InvalidEOLChar', $data);
3✔
86

87
        if ($fix === true) {
3✔
88
            $tokens = $phpcsFile->getTokens();
3✔
89
            switch ($this->eolChar) {
3✔
90
            case '\n':
3✔
91
                $eolChar = "\n";
3✔
92
                break;
3✔
93
            case '\r':
×
94
                $eolChar = "\r";
×
95
                break;
×
96
            case '\r\n':
×
97
                $eolChar = "\r\n";
×
98
                break;
×
99
            default:
100
                $eolChar = $this->eolChar;
×
101
                break;
×
102
            }
103

104
            for ($i = 0; $i < $phpcsFile->numTokens; $i++) {
3✔
105
                if (isset($tokens[($i + 1)]) === true
3✔
106
                    && $tokens[($i + 1)]['line'] <= $tokens[$i]['line']
3✔
107
                ) {
108
                    continue;
3✔
109
                }
110

111
                // Token is the last on a line.
112
                if (isset($tokens[$i]['orig_content']) === true) {
3✔
113
                    $tokenContent = $tokens[$i]['orig_content'];
3✔
114
                } else {
115
                    $tokenContent = $tokens[$i]['content'];
3✔
116
                }
117

118
                $newContent  = rtrim($tokenContent, "\r\n");
3✔
119
                $newContent .= $eolChar;
3✔
120
                if ($tokenContent !== $newContent) {
3✔
121
                    $phpcsFile->fixer->replaceToken($i, $newContent);
3✔
122
                }
123
            }//end for
124
        }//end if
125

126
        // Ignore the rest of the file.
127
        return $phpcsFile->numTokens;
3✔
128

129
    }//end process()
130

131

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