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

PHPCompatibility / PHPCompatibility / 19804455605

30 Nov 2025 08:31PM UTC coverage: 98.306% (-0.04%) from 98.346%
19804455605

push

github

web-flow
Merge pull request #2014 from PHPCompatibility/php-8.5/new-removedterminatingcasewithsemicolon-sniff

PHP 8.5 | ✨ New `PHPCompatibility.ControlStructures.RemovedTerminatingCaseWithSemicolon` sniff (RFC)

30 of 34 new or added lines in 1 file covered. (88.24%)

21 existing lines in 15 files now uncovered.

8355 of 8499 relevant lines covered (98.31%)

20.51 hits per line

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

96.97
/PHPCompatibility/Sniffs/ParameterValues/NewPackFormatSniff.php
1
<?php
2
/**
3
 * PHPCompatibility, an external standard for PHP_CodeSniffer.
4
 *
5
 * @package   PHPCompatibility
6
 * @copyright 2012-2020 PHPCompatibility Contributors
7
 * @license   https://opensource.org/licenses/LGPL-3.0 LGPL3
8
 * @link      https://github.com/PHPCompatibility/PHPCompatibility
9
 */
10

11
namespace PHPCompatibility\Sniffs\ParameterValues;
12

13
use PHPCompatibility\AbstractFunctionCallParameterSniff;
14
use PHPCompatibility\Helpers\ScannedCode;
15
use PHP_CodeSniffer\Files\File;
16
use PHP_CodeSniffer\Util\Tokens;
17
use PHPCSUtils\Utils\PassedParameters;
18
use PHPCSUtils\Utils\TextStrings;
19

20
/**
21
 * Check for valid values for the `$format` passed to `pack()`.
22
 *
23
 * PHP version 5.4+
24
 *
25
 * @link https://www.php.net/manual/en/function.pack.php#refsect1-function.pack-changelog
26
 *
27
 * @since 9.0.0
28
 * @since 10.0.0 This class is now `final`.
29
 */
30
final class NewPackFormatSniff extends AbstractFunctionCallParameterSniff
31
{
32

33
    /**
34
     * Functions to check for.
35
     *
36
     * @since 9.0.0
37
     *
38
     * @var array<string, true>
39
     */
40
    protected $targetFunctions = [
41
        'pack' => true,
42
    ];
43

44
    /**
45
     * List of new format character codes added to pack().
46
     *
47
     * @since 9.0.0
48
     *
49
     * @var array<string, array<string, bool>> Regex pattern => Version array.
50
     */
51
    protected $newFormats = [
52
        '`([Z])`'    => [
53
            '5.4' => false,
54
            '5.5' => true,
55
        ],
56
        '`([qQJP])`' => [
57
            '5.6.2' => false,
58
            '5.6.3' => true,
59
        ],
60
        '`([eEgG])`' => [
61
            '7.0.14' => false,
62
            '7.0.15' => true, // And 7.1.1.
63
        ],
64
    ];
65

66

67
    /**
68
     * Do a version check to determine if this sniff needs to run at all.
69
     *
70
     * @since 9.0.0
71
     *
72
     * @return bool
73
     */
74
    protected function bowOutEarly()
12✔
75
    {
76
        return (ScannedCode::shouldRunOnOrBelow('7.1') === false);
12✔
77
    }
78

79

80
    /**
81
     * Process the parameters of a matched function.
82
     *
83
     * @since 9.0.0
84
     *
85
     * @param \PHP_CodeSniffer\Files\File $phpcsFile    The file being scanned.
86
     * @param int                         $stackPtr     The position of the current token in the stack.
87
     * @param string                      $functionName The token content (function name) which was matched.
88
     * @param array                       $parameters   Array with information about the parameters.
89
     *
90
     * @return void
91
     */
92
    public function processParameters(File $phpcsFile, $stackPtr, $functionName, $parameters)
12✔
93
    {
94
        $targetParam = PassedParameters::getParameterFromStack($parameters, 1, 'format');
12✔
95
        if ($targetParam === false) {
12✔
UNCOV
96
            return;
×
97
        }
98

99
        $tokens = $phpcsFile->getTokens();
12✔
100

101
        for ($i = $targetParam['start']; $i <= $targetParam['end']; $i++) {
12✔
102
            if (isset(Tokens::NAME_TOKENS[$tokens[$i]['code']]) === true
12✔
103
                || $tokens[$i]['code'] === \T_VARIABLE
12✔
104
            ) {
105
                // Variable, constant, function call. Ignore as undetermined.
106
                return;
12✔
107
            }
108

109
            if (isset(Tokens::STRING_TOKENS[$tokens[$i]['code']]) === false) {
12✔
110
                continue;
12✔
111
            }
112

113
            $content = $tokens[$i]['content'];
12✔
114
            if ($tokens[$i]['code'] === \T_DOUBLE_QUOTED_STRING) {
12✔
115
                $content = TextStrings::stripEmbeds($content);
12✔
116
            }
117

118
            foreach ($this->newFormats as $pattern => $versionArray) {
12✔
119
                if (\preg_match($pattern, $content, $matches) !== 1) {
12✔
120
                    continue;
12✔
121
                }
122

123
                foreach ($versionArray as $version => $present) {
12✔
124
                    if ($present === false && ScannedCode::shouldRunOnOrBelow($version) === true) {
12✔
125
                        $phpcsFile->addError(
8✔
126
                            'Passing the $format(s) "%s" to pack() is not supported in PHP %s or lower. Found: %s',
8✔
127
                            $targetParam['start'],
8✔
128
                            'NewFormatFound',
8✔
129
                            [
4✔
130
                                $matches[1],
8✔
131
                                $version,
8✔
132
                                $targetParam['clean'],
8✔
133
                            ]
4✔
134
                        );
4✔
135
                        continue 2;
8✔
136
                    }
137
                }
138
            }
139
        }
140
    }
6✔
141
}
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