• 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

95.92
/src/Fixer/FunctionNotation/FopenFlagOrderFixer.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\FunctionNotation;
16

17
use PhpCsFixer\AbstractFopenFlagFixer;
18
use PhpCsFixer\FixerDefinition\CodeSample;
19
use PhpCsFixer\FixerDefinition\FixerDefinition;
20
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
21
use PhpCsFixer\Preg;
22
use PhpCsFixer\Tokenizer\Token;
23
use PhpCsFixer\Tokenizer\Tokens;
24

25
/**
26
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise.
27
 */
28
final class FopenFlagOrderFixer extends AbstractFopenFlagFixer
29
{
30
    public function getDefinition(): FixerDefinitionInterface
31
    {
32
        return new FixerDefinition(
3✔
33
            'Order the flags in `fopen` calls, `b` and `t` must be last.',
3✔
34
            [new CodeSample("<?php\n\$a = fopen(\$foo, 'br+');\n")],
3✔
35
            null,
3✔
36
            'Risky when the function `fopen` is overridden.'
3✔
37
        );
3✔
38
    }
39

40
    protected function fixFopenFlagToken(Tokens $tokens, int $argumentStartIndex, int $argumentEndIndex): void
41
    {
42
        $argumentFlagIndex = null;
12✔
43

44
        for ($i = $argumentStartIndex; $i <= $argumentEndIndex; ++$i) {
12✔
45
            if ($tokens[$i]->isGivenKind([\T_WHITESPACE, \T_COMMENT, \T_DOC_COMMENT])) {
12✔
46
                continue;
12✔
47
            }
48

49
            if (null !== $argumentFlagIndex) {
12✔
50
                return; // multiple meaningful tokens found, no candidate for fixing
1✔
51
            }
52

53
            $argumentFlagIndex = $i;
12✔
54
        }
55

56
        // check if second argument is candidate
57
        if (null === $argumentFlagIndex || !$tokens[$argumentFlagIndex]->isGivenKind(\T_CONSTANT_ENCAPSED_STRING)) {
11✔
58
            return;
×
59
        }
60

61
        $content = $tokens[$argumentFlagIndex]->getContent();
11✔
62
        $contentQuote = $content[0]; // `'`, `"`, `b` or `B`
11✔
63

64
        if ('b' === $contentQuote || 'B' === $contentQuote) {
11✔
65
            $binPrefix = $contentQuote;
1✔
66
            $contentQuote = $content[1]; // `'` or `"`
1✔
67
            $mode = substr($content, 2, -1);
1✔
68
        } else {
69
            $binPrefix = '';
10✔
70
            $mode = substr($content, 1, -1);
10✔
71
        }
72

73
        $modeLength = \strlen($mode);
11✔
74
        if ($modeLength < 2) {
11✔
75
            return; // nothing to sort
2✔
76
        }
77

78
        if (false === $this->isValidModeString($mode)) {
11✔
79
            return;
3✔
80
        }
81

82
        $split = $this->sortFlags(Preg::split('#([^\+]\+?)#', $mode, -1, \PREG_SPLIT_NO_EMPTY | \PREG_SPLIT_DELIM_CAPTURE));
8✔
83
        $newContent = $binPrefix.$contentQuote.implode('', $split).$contentQuote;
8✔
84

85
        if ($content !== $newContent) {
8✔
86
            $tokens[$argumentFlagIndex] = new Token([\T_CONSTANT_ENCAPSED_STRING, $newContent]);
7✔
87
        }
88
    }
89

90
    /**
91
     * @param list<string> $flags
92
     *
93
     * @return list<string>
94
     */
95
    private function sortFlags(array $flags): array
96
    {
97
        usort(
8✔
98
            $flags,
8✔
99
            static function (string $flag1, string $flag2): int {
8✔
100
                if ($flag1 === $flag2) {
7✔
101
                    return 0;
×
102
                }
103

104
                if ('b' === $flag1) {
7✔
105
                    return 1;
6✔
106
                }
107

108
                if ('b' === $flag2) {
6✔
109
                    return -1;
5✔
110
                }
111

112
                if ('t' === $flag1) {
5✔
113
                    return 1;
2✔
114
                }
115

116
                if ('t' === $flag2) {
5✔
117
                    return -1;
2✔
118
                }
119

120
                return $flag1 < $flag2 ? -1 : 1;
5✔
121
            }
8✔
122
        );
8✔
123

124
        return $flags;
8✔
125
    }
126
}
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