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

keradus / PHP-CS-Fixer / 17252691116

26 Aug 2025 11:09PM UTC coverage: 94.743% (-0.01%) from 94.755%
17252691116

push

github

keradus
chore: apply phpdoc_tag_no_named_arguments

28313 of 29884 relevant lines covered (94.74%)

45.64 hits per line

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

98.67
/src/Fixer/Alias/NoAliasFunctionsFixer.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of PHP CS Fixer.
7
 *
8
 * (c) Fabien Potencier <fabien@symfony.com>
9
 *     Dariusz Rumiński <dariusz.ruminski@gmail.com>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14

15
namespace PhpCsFixer\Fixer\Alias;
16

17
use PhpCsFixer\AbstractFixer;
18
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
19
use PhpCsFixer\Fixer\ConfigurableFixerTrait;
20
use PhpCsFixer\FixerConfiguration\AllowedValueSubset;
21
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
22
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
23
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
24
use PhpCsFixer\FixerDefinition\CodeSample;
25
use PhpCsFixer\FixerDefinition\FixerDefinition;
26
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
27
use PhpCsFixer\Tokenizer\Analyzer\ArgumentsAnalyzer;
28
use PhpCsFixer\Tokenizer\Analyzer\FunctionsAnalyzer;
29
use PhpCsFixer\Tokenizer\Token;
30
use PhpCsFixer\Tokenizer\Tokens;
31

32
/**
33
 * @phpstan-type _AutogeneratedInputConfiguration array{
34
 *  sets?: list<'@all'|'@exif'|'@ftp'|'@IMAP'|'@internal'|'@ldap'|'@mbreg'|'@mysqli'|'@oci'|'@odbc'|'@openssl'|'@pcntl'|'@pg'|'@posix'|'@snmp'|'@sodium'|'@time'>,
35
 * }
36
 * @phpstan-type _AutogeneratedComputedConfiguration array{
37
 *  sets: list<'@all'|'@exif'|'@ftp'|'@IMAP'|'@internal'|'@ldap'|'@mbreg'|'@mysqli'|'@oci'|'@odbc'|'@openssl'|'@pcntl'|'@pg'|'@posix'|'@snmp'|'@sodium'|'@time'>,
38
 * }
39
 *
40
 * @implements ConfigurableFixerInterface<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration>
41
 *
42
 * @author Vladimir Reznichenko <kalessil@gmail.com>
43
 * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
44
 *
45
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise.
46
 */
47
final class NoAliasFunctionsFixer extends AbstractFixer implements ConfigurableFixerInterface
48
{
49
    /** @use ConfigurableFixerTrait<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration> */
50
    use ConfigurableFixerTrait;
51

52
    private const SETS = [
53
        '@internal' => [
54
            'diskfreespace' => 'disk_free_space',
55

56
            'dns_check_record' => 'checkdnsrr',
57
            'dns_get_mx' => 'getmxrr',
58

59
            'session_commit' => 'session_write_close',
60

61
            'stream_register_wrapper' => 'stream_wrapper_register',
62
            'set_file_buffer' => 'stream_set_write_buffer',
63
            'socket_set_blocking' => 'stream_set_blocking',
64
            'socket_get_status' => 'stream_get_meta_data',
65
            'socket_set_timeout' => 'stream_set_timeout',
66
            'socket_getopt' => 'socket_get_option',
67
            'socket_setopt' => 'socket_set_option',
68

69
            'chop' => 'rtrim',
70
            'close' => 'closedir',
71
            'doubleval' => 'floatval',
72
            'fputs' => 'fwrite',
73
            'get_required_files' => 'get_included_files',
74
            'ini_alter' => 'ini_set',
75
            'is_double' => 'is_float',
76
            'is_integer' => 'is_int',
77
            'is_long' => 'is_int',
78
            'is_real' => 'is_float',
79
            'is_writeable' => 'is_writable',
80
            'join' => 'implode',
81
            'key_exists' => 'array_key_exists',
82
            'magic_quotes_runtime' => 'set_magic_quotes_runtime',
83
            'pos' => 'current',
84
            'show_source' => 'highlight_file',
85
            'sizeof' => 'count',
86
            'strchr' => 'strstr',
87
            'user_error' => 'trigger_error',
88
        ],
89

90
        '@IMAP' => [
91
            'imap_create' => 'imap_createmailbox',
92
            'imap_fetchtext' => 'imap_body',
93
            'imap_header' => 'imap_headerinfo',
94
            'imap_listmailbox' => 'imap_list',
95
            'imap_listsubscribed' => 'imap_lsub',
96
            'imap_rename' => 'imap_renamemailbox',
97
            'imap_scan' => 'imap_listscan',
98
            'imap_scanmailbox' => 'imap_listscan',
99
        ],
100

101
        '@ldap' => [
102
            'ldap_close' => 'ldap_unbind',
103
            'ldap_modify' => 'ldap_mod_replace',
104
        ],
105

106
        '@mysqli' => [
107
            'mysqli_execute' => 'mysqli_stmt_execute',
108
            'mysqli_set_opt' => 'mysqli_options',
109
            'mysqli_escape_string' => 'mysqli_real_escape_string',
110
        ],
111

112
        '@pg' => [
113
            'pg_exec' => 'pg_query',
114
        ],
115

116
        '@oci' => [
117
            'oci_free_cursor' => 'oci_free_statement',
118
        ],
119

120
        '@odbc' => [
121
            'odbc_do' => 'odbc_exec',
122
            'odbc_field_precision' => 'odbc_field_len',
123
        ],
124

125
        '@mbreg' => [
126
            'mbereg' => 'mb_ereg',
127
            'mbereg_match' => 'mb_ereg_match',
128
            'mbereg_replace' => 'mb_ereg_replace',
129
            'mbereg_search' => 'mb_ereg_search',
130
            'mbereg_search_getpos' => 'mb_ereg_search_getpos',
131
            'mbereg_search_getregs' => 'mb_ereg_search_getregs',
132
            'mbereg_search_init' => 'mb_ereg_search_init',
133
            'mbereg_search_pos' => 'mb_ereg_search_pos',
134
            'mbereg_search_regs' => 'mb_ereg_search_regs',
135
            'mbereg_search_setpos' => 'mb_ereg_search_setpos',
136
            'mberegi' => 'mb_eregi',
137
            'mberegi_replace' => 'mb_eregi_replace',
138
            'mbregex_encoding' => 'mb_regex_encoding',
139
            'mbsplit' => 'mb_split',
140
        ],
141

142
        '@openssl' => [
143
            'openssl_get_publickey' => 'openssl_pkey_get_public',
144
            'openssl_get_privatekey' => 'openssl_pkey_get_private',
145
        ],
146

147
        '@sodium' => [
148
            'sodium_crypto_scalarmult_base' => 'sodium_crypto_box_publickey_from_secretkey',
149
        ],
150

151
        '@exif' => [
152
            'read_exif_data' => 'exif_read_data',
153
        ],
154

155
        '@ftp' => [
156
            'ftp_quit' => 'ftp_close',
157
        ],
158

159
        '@posix' => [
160
            'posix_errno' => 'posix_get_last_error',
161
        ],
162

163
        '@pcntl' => [
164
            'pcntl_errno' => 'pcntl_get_last_error',
165
        ],
166

167
        '@time' => [
168
            'mktime' => ['time', 0],
169
            'gmmktime' => ['time', 0],
170
        ],
171
    ];
172

173
    /**
174
     * @var array<string, array{string, int}|string> stores alias (key) - master (value) functions mapping
175
     */
176
    private array $aliases = [];
177

178
    public function getDefinition(): FixerDefinitionInterface
179
    {
180
        return new FixerDefinition(
3✔
181
            'Master functions shall be used instead of aliases.',
3✔
182
            [
3✔
183
                new CodeSample(
3✔
184
                    '<?php
3✔
185
$a = chop($b);
186
close($b);
187
$a = doubleval($b);
188
$a = fputs($b, $c);
189
$a = get_required_files();
190
ini_alter($b, $c);
191
$a = is_double($b);
192
$a = is_integer($b);
193
$a = is_long($b);
194
$a = is_real($b);
195
$a = is_writeable($b);
196
$a = join($glue, $pieces);
197
$a = key_exists($key, $array);
198
magic_quotes_runtime($new_setting);
199
$a = pos($array);
200
$a = show_source($filename, true);
201
$a = sizeof($b);
202
$a = strchr($haystack, $needle);
203
$a = imap_header($imap_stream, 1);
204
user_error($message);
205
mbereg_search_getregs();
206
'
3✔
207
                ),
3✔
208
                new CodeSample(
3✔
209
                    '<?php
3✔
210
$a = is_double($b);
211
mbereg_search_getregs();
212
',
3✔
213
                    ['sets' => ['@mbreg']]
3✔
214
                ),
3✔
215
            ],
3✔
216
            null,
3✔
217
            'Risky when any of the alias functions are overridden.'
3✔
218
        );
3✔
219
    }
220

221
    /**
222
     * {@inheritdoc}
223
     *
224
     * Must run before ImplodeCallFixer, PhpUnitDedicateAssertFixer.
225
     */
226
    public function getPriority(): int
227
    {
228
        return 40;
1✔
229
    }
230

231
    public function isCandidate(Tokens $tokens): bool
232
    {
233
        return $tokens->isTokenKindFound(\T_STRING);
3,006✔
234
    }
235

236
    public function isRisky(): bool
237
    {
238
        return true;
1✔
239
    }
240

241
    protected function configurePostNormalisation(): void
242
    {
243
        $this->aliases = [];
3,015✔
244

245
        foreach ($this->configuration['sets'] as $set) {
3,015✔
246
            if ('@all' === $set) {
3,015✔
247
                $this->aliases = array_merge(...array_values(self::SETS));
1✔
248

249
                break;
1✔
250
            }
251

252
            if (!isset(self::SETS[$set])) {
3,015✔
253
                throw new \LogicException(\sprintf('Set %s passed option validation, but not part of ::SETS.', $set));
×
254
            }
255

256
            $this->aliases = array_merge($this->aliases, self::SETS[$set]);
3,015✔
257
        }
258
    }
259

260
    protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
261
    {
262
        $functionsAnalyzer = new FunctionsAnalyzer();
2,462✔
263
        $argumentsAnalyzer = new ArgumentsAnalyzer();
2,462✔
264

265
        foreach ($tokens->findGivenKind(\T_STRING) as $index => $token) {
2,462✔
266
            // check mapping hit
267
            $tokenContent = strtolower($token->getContent());
2,462✔
268

269
            if (!isset($this->aliases[$tokenContent])) {
2,462✔
270
                continue;
2,140✔
271
            }
272

273
            // skip expressions without parameters list
274
            $openParenthesis = $tokens->getNextMeaningfulToken($index);
1,726✔
275

276
            if (!$tokens[$openParenthesis]->equals('(')) {
1,726✔
277
                continue;
428✔
278
            }
279

280
            if (!$functionsAnalyzer->isGlobalFunctionCall($tokens, $index)) {
1,405✔
281
                continue;
751✔
282
            }
283

284
            if (\is_array($this->aliases[$tokenContent])) {
654✔
285
                [$alias, $numberOfArguments] = $this->aliases[$tokenContent];
2✔
286

287
                $count = $argumentsAnalyzer->countArguments($tokens, $openParenthesis, $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openParenthesis));
2✔
288

289
                if ($numberOfArguments !== $count) {
2✔
290
                    continue;
2✔
291
                }
292
            } else {
293
                $alias = $this->aliases[$tokenContent];
653✔
294
            }
295

296
            $tokens[$index] = new Token([\T_STRING, $alias]);
654✔
297
        }
298
    }
299

300
    protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
301
    {
302
        $sets = [
3,015✔
303
            '@all' => 'all listed sets',
3,015✔
304
            '@internal' => 'native functions',
3,015✔
305
            '@exif' => 'EXIF functions',
3,015✔
306
            '@ftp' => 'FTP functions',
3,015✔
307
            '@IMAP' => 'IMAP functions',
3,015✔
308
            '@ldap' => 'LDAP functions',
3,015✔
309
            '@mbreg' => 'from `ext-mbstring`',
3,015✔
310
            '@mysqli' => 'mysqli functions',
3,015✔
311
            '@oci' => 'oci functions',
3,015✔
312
            '@odbc' => 'odbc functions',
3,015✔
313
            '@openssl' => 'openssl functions',
3,015✔
314
            '@pcntl' => 'PCNTL functions',
3,015✔
315
            '@pg' => 'pg functions',
3,015✔
316
            '@posix' => 'POSIX functions',
3,015✔
317
            '@snmp' => 'SNMP functions', // @TODO Remove on next major 4.0 as this set is now empty
3,015✔
318
            '@sodium' => 'libsodium functions',
3,015✔
319
            '@time' => 'time functions',
3,015✔
320
        ];
3,015✔
321

322
        $list = "List of sets to fix. Defined sets are:\n\n";
3,015✔
323

324
        foreach ($sets as $set => $description) {
3,015✔
325
            $list .= \sprintf("* `%s` (%s);\n", $set, $description);
3,015✔
326
        }
327

328
        $list = rtrim($list, ";\n").'.';
3,015✔
329

330
        return new FixerConfigurationResolver([
3,015✔
331
            (new FixerOptionBuilder('sets', $list))
3,015✔
332
                ->setAllowedTypes(['string[]'])
3,015✔
333
                ->setAllowedValues([new AllowedValueSubset(array_keys($sets))])
3,015✔
334
                ->setDefault(['@internal', '@IMAP', '@pg'])
3,015✔
335
                ->getOption(),
3,015✔
336
        ]);
3,015✔
337
    }
338
}
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