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

PHPCSStandards / PHP_CodeSniffer / 15036337869

15 May 2025 04:03AM UTC coverage: 78.375% (-0.2%) from 78.556%
15036337869

Pull #856

github

web-flow
Merge 93f570b46 into f5e7943d0
Pull Request #856: [Doc] Cover all errors of PEAR ClassDeclaration

25112 of 32041 relevant lines covered (78.37%)

69.4 hits per line

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

75.0
/src/Standards/Generic/Sniffs/Debug/ESLintSniff.php
1
<?php
2
/**
3
 * Runs eslint on the file.
4
 *
5
 * @author    Ryan McCue <ryan+gh@hmn.md>
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
 * @deprecated 3.9.0
10
 */
11

12
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\Debug;
13

14
use PHP_CodeSniffer\Config;
15
use PHP_CodeSniffer\Files\File;
16
use PHP_CodeSniffer\Sniffs\DeprecatedSniff;
17
use PHP_CodeSniffer\Sniffs\Sniff;
18
use PHP_CodeSniffer\Util\Common;
19

20
class ESLintSniff implements Sniff, DeprecatedSniff
21
{
22

23
    /**
24
     * A list of tokenizers this sniff supports.
25
     *
26
     * @var array
27
     */
28
    public $supportedTokenizers = ['JS'];
29

30
    /**
31
     * ESLint configuration file path.
32
     *
33
     * @var string|null Path to eslintrc. Null to autodetect.
34
     */
35
    public $configFile = null;
36

37

38
    /**
39
     * Returns the token types that this sniff is interested in.
40
     *
41
     * @return array<int|string>
42
     */
43
    public function register()
2✔
44
    {
45
        return [T_OPEN_TAG];
2✔
46

47
    }//end register()
48

49

50
    /**
51
     * Processes the tokens that this sniff is interested in.
52
     *
53
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found.
54
     * @param int                         $stackPtr  The position in the stack where
55
     *                                               the token was found.
56
     *
57
     * @return int
58
     * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If jshint.js could not be run.
59
     */
60
    public function process(File $phpcsFile, $stackPtr)
2✔
61
    {
62
        $eslintPath = Config::getExecutablePath('eslint');
2✔
63
        if ($eslintPath === null) {
2✔
64
            return $phpcsFile->numTokens;
×
65
        }
66

67
        $filename = $phpcsFile->getFilename();
2✔
68

69
        $configFile = $this->configFile;
2✔
70
        if (empty($configFile) === true) {
2✔
71
            // Attempt to autodetect.
72
            $candidates = glob('.eslintrc{.js,.yaml,.yml,.json}', GLOB_BRACE);
2✔
73
            if (empty($candidates) === false) {
2✔
74
                $configFile = $candidates[0];
2✔
75
            }
1✔
76
        }
1✔
77

78
        $eslintOptions = ['--format json'];
2✔
79
        if (empty($configFile) === false) {
2✔
80
            $eslintOptions[] = '--config '.escapeshellarg($configFile);
2✔
81
        }
1✔
82

83
        $cmd = Common::escapeshellcmd(escapeshellarg($eslintPath).' '.implode(' ', $eslintOptions).' '.escapeshellarg($filename));
2✔
84

85
        // Execute!
86
        exec($cmd, $stdout, $code);
2✔
87

88
        if ($code <= 0) {
2✔
89
            // No errors, continue.
90
            return $phpcsFile->numTokens;
×
91
        }
92

93
        $data = json_decode(implode("\n", $stdout));
2✔
94
        if (json_last_error() !== JSON_ERROR_NONE) {
2✔
95
            // Ignore any errors.
96
            return $phpcsFile->numTokens;
×
97
        }
98

99
        // Data is a list of files, but we only pass a single one.
100
        $messages = $data[0]->messages;
2✔
101
        foreach ($messages as $error) {
2✔
102
            $message = 'eslint says: '.$error->message;
2✔
103
            if (empty($error->fatal) === false || $error->severity === 2) {
2✔
104
                $phpcsFile->addErrorOnLine($message, $error->line, 'ExternalTool');
2✔
105
            } else {
1✔
106
                $phpcsFile->addWarningOnLine($message, $error->line, 'ExternalTool');
×
107
            }
108
        }
1✔
109

110
        // Ignore the rest of the file.
111
        return $phpcsFile->numTokens;
2✔
112

113
    }//end process()
114

115

116
    /**
117
     * Provide the version number in which the sniff was deprecated.
118
     *
119
     * @return string
120
     */
121
    public function getDeprecationVersion()
×
122
    {
123
        return 'v3.9.0';
×
124

125
    }//end getDeprecationVersion()
126

127

128
    /**
129
     * Provide the version number in which the sniff will be removed.
130
     *
131
     * @return string
132
     */
133
    public function getRemovalVersion()
×
134
    {
135
        return 'v4.0.0';
×
136

137
    }//end getRemovalVersion()
138

139

140
    /**
141
     * Provide a custom message to display with the deprecation.
142
     *
143
     * @return string
144
     */
145
    public function getDeprecationMessage()
×
146
    {
147
        return 'Support for scanning JavaScript files will be removed completely in v4.0.0.';
×
148

149
    }//end getDeprecationMessage()
150

151

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