• 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

97.14
/src/Standards/Generic/Sniffs/PHP/DisallowAlternativePHPTagsSniff.php
1
<?php
2
/**
3
 * Verifies that no alternative PHP tags are used.
4
 *
5
 * If alternative PHP open tags are found, this sniff can fix both the open and close tags.
6
 *
7
 * @author    Greg Sherwood <gsherwood@squiz.net>
8
 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
9
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
10
 */
11

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

14
use PHP_CodeSniffer\Files\File;
15
use PHP_CodeSniffer\Sniffs\Sniff;
16

17
class DisallowAlternativePHPTagsSniff 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 [T_INLINE_HTML];
3✔
29

30
    }//end register()
31

32

33
    /**
34
     * Processes this test, when one of its tokens is encountered.
35
     *
36
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
37
     * @param int                         $stackPtr  The position of the current token
38
     *                                               in the stack passed in $tokens.
39
     *
40
     * @return void
41
     */
42
    public function process(File $phpcsFile, $stackPtr)
3✔
43
    {
44
        $tokens  = $phpcsFile->getTokens();
3✔
45
        $openTag = $tokens[$stackPtr];
3✔
46
        $content = $openTag['content'];
3✔
47

48
        if (trim($content) === '') {
3✔
49
            return;
×
50
        }
51

52
        // Account for script open tags.
53
        if (preg_match('`(<script (?:[^>]+)?language=[\'"]?php[\'"]?(?:[^>]+)?>)`i', $content, $match) === 1) {
3✔
54
            $error   = 'Script style opening tag used; expected "<?php" but found "%s"';
3✔
55
            $snippet = $this->getSnippet($content, $match[1]);
3✔
56
            $data    = [$match[1].$snippet];
3✔
57

58
            $phpcsFile->addError($error, $stackPtr, 'ScriptOpenTagFound', $data);
3✔
59
            return;
3✔
60
        }
61

62
        // Account for ASP style tags.
63
        if (strpos($content, '<%=') !== false) {
3✔
64
            $error   = 'Possible use of ASP style short opening tags detected; found: %s';
3✔
65
            $snippet = $this->getSnippet($content, '<%=');
3✔
66
            $data    = ['<%='.$snippet];
3✔
67

68
            $phpcsFile->addWarning($error, $stackPtr, 'MaybeASPShortOpenTagFound', $data);
3✔
69
        } else if (strpos($content, '<%') !== false) {
3✔
70
            $error   = 'Possible use of ASP style opening tags detected; found: %s';
3✔
71
            $snippet = $this->getSnippet($content, '<%');
3✔
72
            $data    = ['<%'.$snippet];
3✔
73

74
            $phpcsFile->addWarning($error, $stackPtr, 'MaybeASPOpenTagFound', $data);
3✔
75
        }
76

77
    }//end process()
1✔
78

79

80
    /**
81
     * Get a snippet from a HTML token.
82
     *
83
     * @param string $content The content of the HTML token.
84
     * @param string $start   Partial string to use as a starting point for the snippet.
85
     * @param int    $length  The target length of the snippet to get. Defaults to 40.
86
     *
87
     * @return string
88
     */
89
    protected function getSnippet($content, $start='', $length=40)
3✔
90
    {
91
        $startPos = 0;
3✔
92

93
        if ($start !== '') {
3✔
94
            $startPos = strpos($content, $start);
3✔
95
            if ($startPos !== false) {
3✔
96
                $startPos += strlen($start);
3✔
97
            }
98
        }
99

100
        $snippet = substr($content, $startPos, $length);
3✔
101
        if ((strlen($content) - $startPos) > $length) {
3✔
102
            $snippet .= '...';
3✔
103
        }
104

105
        return $snippet;
3✔
106

107
    }//end getSnippet()
108

109

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