-
Notifications
You must be signed in to change notification settings - Fork 1
/
fix-fmt.php
145 lines (127 loc) · 4.48 KB
/
fix-fmt.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
// replace this line if necessary
$pharPath = "/home/mkcg/.config/sublime-text-3/Packages/phpfmt/fmt.phar";
$tmpDir = __DIR__;
(new Phar($pharPath))->extractTo($tmpDir);
$lines = explode("\n", file_get_contents('fmt.stub.php'));
// FIX PHP7.1 yield from : https://github.com/nanch/phpfmt_stable/issues/37
$toAdd = [
"",
"\t\t\tcase T_YIELD_FROM:",
"\t\t\t\t\$this->appendCode(\$text . \$this->getSpace(\$this->rightTokenIs(T_STRING)));",
"\t\t\t\tbreak;",
];
if (array_search($toAdd[1], $lines) === false) {
foreach ($toAdd as $key => $value) {
array_splice($lines, 5227 + $key, 0, $value);
}
}
$patchs = [
// FIX PHP7.1 nullable types : https://github.com/nanch/phpfmt_stable/issues/36
[
"\t\t\t\t\t\$this->appendCode(' ' . \$text . \$this->getSpace(!\$this->rightTokenIs(ST_COLON)));",
"\t\t\t\t\t\$this->appendCode(\$this->getSpace(\$id !== ST_QUESTION || !\$this->leftMemoUsefulTokenIs(ST_PARENTHESES_OPEN)) . \$text . \$this->getSpace(!\$this->rightTokenIs(ST_COLON) && (\$id !== ST_QUESTION || !\$this->rightTokenIs(T_STRING))));",
],
// FIX pass ClassToStatic : https://github.com/nanch/phpfmt_stable/issues/31
[
"\t\t\t\t\$this->tkns[\$i] = [T_STRING, self::PLACEHOLDER];",
"\t\t\t\t\$this->tkns[\$i] = [T_STRING, static::PLACEHOLDER];",
],
// Fix : PHP keyword used as function names must not be lowercased since PHP7
[
"\t\t\t\tT_CONSTANT_ENCAPSED_STRING == \$id",
"\t\t\t\tT_CONSTANT_ENCAPSED_STRING == \$id || T_STRING == \$id"
]
];
foreach ($patchs as list($search, $replacement)) {
$lineNumber = array_search($search, $lines);
if ($lineNumber) {
$lines[$lineNumber] = $replacement;
}
}
$pathTokenParse = [
// Fix : PHP keyword used as function names must not be lowercased since PHP7
[
"final class PSR2KeywordsLowerCase extends FormatterPass {",
"\tpublic function format(\$source) {",
"\t\t\$this->tkns = token_get_all(\$source, TOKEN_PARSE);"
],
// Fix : visibility modifier is preserved when method names is a PHP keyword :
// - https://github.com/nanch/phpfmt_stable/issues/18
// - https://github.com/nanch/phpfmt_stable/issues/19
[
"final class PSR2ModifierVisibilityStaticOrder extends FormatterPass {",
"\tpublic function format(\$source) {",
"\t\t\$this->tkns = token_get_all(\$source, TOKEN_PARSE);"
],
];
foreach ($pathTokenParse as list($search, $methodStart, $replacement)) {
$lineNumber = array_search($search, $lines);
for ($i = $lineNumber; isset($lines[$i]); $i++) {
if ($lines[$i] === $methodStart) {
$lines[$i+1] = $replacement;
break;
}
}
}
// Fix replace is_null when preceded by an exclamation to negate it : https://github.com/nanch/phpfmt_stable/issues/11
$inject = [
[
'insert_after',
"\t\t\$this->useCache = true;",
[
"\t\t\$isEqual = true;"
]
],
[
'insert_after',
"\t\t\t\$this->cache = [];",
[
"",
"\t\t\tif (\$id == ST_EXCLAMATION) {",
"\t\t\t\tlist(\$nextToken, \$nextText) = \$this->rightUsefulToken();",
"\t\t\t\tif (\$nextToken === T_STRING && strtolower(\$nextText) === 'is_null') {",
"\t\t\t\t\t\$isEqual = false;",
"\t\t\t\t\tcontinue;",
"\t\t\t\t}",
"\t\t\t}",
"",
]
],
[
'replace',
"\t\t\t\t\$this->appendCode('===');",
"\t\t\t\t\$this->appendCode(\$isEqual ? '===' : '!==');"
],
[
'insert_after',
"\t\t\t\t\$this->printAndStopAt(ST_PARENTHESES_CLOSE);",
[
"\t\t\t\t\$isEqual = true;"
]
]
];
$isNullBegin = array_search("final class ReplaceIsNull extends AdditionalPass {", $lines);
if ($isNullBegin) {
$i = $isNullBegin;
foreach ($inject as list($type, $search, $replacement)) {
for (; isset($lines[$i]); $i++) {
if ($lines[$i] === $search) {
if ($type === 'insert_after') {
array_splice($lines, $i+1, 0, $replacement);
} elseif ($type === 'replace') {
$lines[$i] = $replacement;
}
break;
}
}
}
}
file_put_contents(
'fmt.stub.php',
implode("\n", $lines)
);
$phar = new Phar($pharPath);
$phar->setDefaultStub('fmt.stub.php', 'index.php');
$phar->addFile('fmt.stub.php');
unlink('fmt.stub.php');