-
Notifications
You must be signed in to change notification settings - Fork 0
/
aif.php
243 lines (207 loc) · 5.66 KB
/
aif.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
/**
* php-array-index-fixer
* =====================
*
* This script parses a PHP source file and replaces unquoted
* array indexes with single-quoted array indexes.
*
* It will fix the following:
* $a[b] = 'c';
*
* While not breaking the following:
* $b = "$a[b]";
* $c = " \"$a[b]\" ";
* $d = $e[$f->g];
*
* It was meant to be invoked as follows:
* php aif.php <source_file>
*
* On a Linux machine, the following commandline script
* will invoke aif.php on all PHP source files in a
* directory, including subfolders:
*
* find . -type f -iname '*.php' -exec php aif.php {} \;
*
* License
* =======
*
* Copyright 2012 Gustav Bertram
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author Gustav Bertram <gustav.bertram@gmail.com>
* @copyright 2012 Gustav Bertram
* @license http://www.gnu.org/licenses/gpl.html GNU Public License Version 3
* @version 1.0
*/
// Debug constants
define("DEBUG", FALSE);
// New token type
define("T_CHAR", 0);
/**
* This class represents tokens.
**/
class Token {
public $type;
public $text;
function __construct($token) {
if(!is_array($token)) {
$this->type = T_CHAR;
$this->text = $token;
} else {
list($this->type, $this->text) = $token;
}
}
public function getName() {
if ($this->type == T_CHAR) {
return 'T_CHAR';
} else {
return token_name($this->type);
}
}
}
/**
* This class represents the tokenizing and parsing state machine.
*
* Usage:
* $sm = new StateMachine();
* $sm->tokenize($text);
* $text = $sm->parse();
*
*
*/
class StateMachine {
private $state;
private $states;
private $last_token;
public $tokens;
function __construct() {
$this->state = 'DEFAULT';
$this->states = array('DEFAULT', 'SINGLE_QUOTED', 'DOUBLE_QUOTED', 'ARRAY_INDEX');
}
protected function setState($state) {
if (in_array($state, $this->states)) {
$this->state = $state;
} else {
throw Exception('Invalid state' . $state);
}
}
public function tokenize($text) {
$php_tokens = token_get_all($text);
$this->tokens = array();
foreach ($php_tokens AS $php_token) {
$this->tokens[] = new Token($php_token);
}
}
public function parse() {
$parsed_text = '';
foreach ($this->tokens as $token) {
$this->parseToken($token);
$parsed_text .= $token->text;
}
return $parsed_text;
}
protected function parseToken(Token $token) {
$state_function = 'parseToken' . $this->state;
$this->$state_function($token);
$this->last_token = $token;
}
protected function parseTokenDEFAULT(Token $token) {
switch ($token->type) {
case T_CHAR:
if ($token->text == '\'') {
// Entering a single quoted string
$this->setState('SINGLE_QUOTED');
}
if ($token->text == '"') {
// Entering a double_quoted_string
$this->setState('DOUBLE_QUOTED');
}
if ($token->text == '[') {
// Entering an array index
$this->setState('ARRAY_INDEX');
}
break;
default:
break;
}
}
protected function parseTokenSINGLE_QUOTED(Token $token) {
switch ($token->type) {
case T_CHAR:
if ($token->text == '\'') {
// Exiting a single quoted string
$this->setState('DEFAULT');
}
break;
default:
break;
}
}
protected function parseTokenDOUBLE_QUOTED(Token $token) {
switch ($token->type) {
case T_CHAR:
if ($token->text == '"') {
// Exiting a double quoted string
$this->setState('DEFAULT');
}
break;
default:
break;
}
}
protected function parseTokenARRAY_INDEX(Token $token) {
switch ($token->type) {
case T_STRING:
if ($this->last_token->text == '[') {
$token->text = "'{$token->text}'";
$token->type = T_CONSTANT_ENCAPSED_STRING;
if (DEBUG) {
echo "REPLACED: " . $token->text;
}
}
else
{
if (DEBUG) {
echo "NOT REPLACED:" . $token->text;
}
}
break;
case T_CHAR:
if ($token->text == ']') {
// Exiting array index
$this->setState('DEFAULT');
}
break;
default:
break;
}
}
}
// Read file
$filename = $argv[1];
$file = file_get_contents($filename);
// Create state machine
$sm = new StateMachine();
// Tokenize
$sm->tokenize($file);
// Parse
$new_file = $sm->parse();
// Write file
if (DEBUG) {
echo $new_file . "\n";
} else {
file_put_contents($filename, $new_file);
}