-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
FopenFlagOrderFixer.php
123 lines (100 loc) · 3.47 KB
/
FopenFlagOrderFixer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Fixer\FunctionNotation;
use PhpCsFixer\AbstractFopenFlagFixer;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Preg;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
final class FopenFlagOrderFixer extends AbstractFopenFlagFixer
{
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
'Order the flags in `fopen` calls, `b` and `t` must be last.',
[new CodeSample("<?php\n\$a = fopen(\$foo, 'br+');\n")],
null,
'Risky when the function `fopen` is overridden.'
);
}
protected function fixFopenFlagToken(Tokens $tokens, int $argumentStartIndex, int $argumentEndIndex): void
{
$argumentFlagIndex = null;
for ($i = $argumentStartIndex; $i <= $argumentEndIndex; ++$i) {
if ($tokens[$i]->isGivenKind([T_WHITESPACE, T_COMMENT, T_DOC_COMMENT])) {
continue;
}
if (null !== $argumentFlagIndex) {
return; // multiple meaningful tokens found, no candidate for fixing
}
$argumentFlagIndex = $i;
}
// check if second argument is candidate
if (null === $argumentFlagIndex || !$tokens[$argumentFlagIndex]->isGivenKind(T_CONSTANT_ENCAPSED_STRING)) {
return;
}
$content = $tokens[$argumentFlagIndex]->getContent();
$contentQuote = $content[0]; // `'`, `"`, `b` or `B`
if ('b' === $contentQuote || 'B' === $contentQuote) {
$binPrefix = $contentQuote;
$contentQuote = $content[1]; // `'` or `"`
$mode = substr($content, 2, -1);
} else {
$binPrefix = '';
$mode = substr($content, 1, -1);
}
$modeLength = \strlen($mode);
if ($modeLength < 2) {
return; // nothing to sort
}
if (false === $this->isValidModeString($mode)) {
return;
}
$split = $this->sortFlags(Preg::split('#([^\+]\+?)#', $mode, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE));
$newContent = $binPrefix.$contentQuote.implode('', $split).$contentQuote;
if ($content !== $newContent) {
$tokens[$argumentFlagIndex] = new Token([T_CONSTANT_ENCAPSED_STRING, $newContent]);
}
}
/**
* @param list<string> $flags
*
* @return list<string>
*/
private function sortFlags(array $flags): array
{
usort(
$flags,
static function (string $flag1, string $flag2): int {
if ($flag1 === $flag2) {
return 0;
}
if ('b' === $flag1) {
return 1;
}
if ('b' === $flag2) {
return -1;
}
if ('t' === $flag1) {
return 1;
}
if ('t' === $flag2) {
return -1;
}
return $flag1 < $flag2 ? -1 : 1;
}
);
return $flags;
}
}