4
4
// modified coding_standards script, only checks line endings and encoding
5
5
6
6
if (PHP_SAPI !== 'cli ' ) {
7
- echo 'error: this script may only be run from CLI ' , PHP_EOL ;
8
- exit (1 );
7
+ echo 'error: this script may only be run from CLI ' , PHP_EOL ;
8
+ exit (1 );
9
9
}
10
10
11
11
stream_set_blocking (STDIN , 0 );
20
20
echo PHP_EOL , PHP_EOL ;
21
21
22
22
if (!isset ($ argv [1 ]) || !in_array ($ argv [1 ], array ('fix ' , 'check ' ))) {
23
- echo 'Usage:
24
- php ' , $ argv [0 ], ' <mode> [options]
25
- php ' , $ argv [0 ], ' <mode> <path> [options]
26
- php ' , $ argv [0 ], ' <mode> package <package> [options]
23
+ echo 'Usage:
24
+ php ' , $ argv [0 ], ' <mode> [options]
25
+ php ' , $ argv [0 ], ' <mode> <path> [options]
26
+ php ' , $ argv [0 ], ' <mode> package <package> [options]
27
27
28
- <mode> "check" or "fix"
29
- <path> path to a directory or a file
30
- <package> package id ("addonname" or "addonname/pluginname")
28
+ <mode> "check" or "fix"
29
+ <path> path to a directory or a file
30
+ <package> package id ("addonname" or "addonname/pluginname")
31
31
32
- options:
33
- --hide-process: Don \'t show current checking file path ' , PHP_EOL , PHP_EOL ;
32
+ options:
33
+ --hide-process: Don \'t show current checking file path ' , PHP_EOL , PHP_EOL ;
34
34
35
- exit (1 );
35
+ exit (1 );
36
36
}
37
37
38
38
class rex_coding_standards_fixer
39
39
{
40
- protected
41
- $ content ,
42
- $ fixable = array (),
43
- $ nonFixable = array ();
44
-
45
- public function __construct ($ content )
46
- {
47
- $ this ->content = $ content ;
48
-
49
- $ this ->fix ();
50
- }
51
-
52
- public function hasChanged ()
53
- {
54
- return !empty ($ this ->fixable ) || !empty ($ this ->nonFixable );
55
- }
56
-
57
- public function getFixable ()
58
- {
59
- return array_keys ($ this ->fixable );
60
- }
61
-
62
- public function getNonFixable ()
63
- {
64
- return array_keys ($ this ->nonFixable );
65
- }
66
-
67
- public function getResult ()
68
- {
69
- return $ this ->content ;
70
- }
71
-
72
- protected function addFixable ($ fixable )
73
- {
74
- $ this ->fixable [$ fixable ] = true ;
75
- }
76
-
77
- protected function addNonFixable ($ nonFixable )
78
- {
79
- $ this ->nonFixable [$ nonFixable ] = true ;
80
- }
81
-
82
- protected function fix ()
83
- {
84
- if (($ encoding = mb_detect_encoding ($ this ->content , 'UTF-8,ISO-8859-1,WINDOWS-1252 ' )) != 'UTF-8 ' ) {
85
- if ($ encoding === false ) {
86
- $ encoding = mb_detect_encoding ($ this ->content );
87
- }
88
- if ($ encoding !== false ) {
89
- $ this ->content = iconv ($ encoding , 'UTF-8 ' , $ this ->content );
90
- $ this ->addFixable ('fix encoding from ' . $ encoding . ' to UTF-8 ' );
91
- } else {
92
- $ this ->addNonFixable ('couldn \'t detect encoding, change it to UTF-8 ' );
93
- }
94
- } elseif (strpos ($ this ->content , "\xEF\xBB\xBF" ) === 0 ) {
95
- $ this ->content = substr ($ this ->content , 3 );
96
- $ this ->addFixable ('remove BOM (Byte Order Mark) ' );
40
+ protected $ content ;
41
+ protected $ fixable = array ();
42
+ protected $ nonFixable = array ();
43
+
44
+ public function __construct ($ content )
45
+ {
46
+ $ this ->content = $ content ;
47
+
48
+ $ this ->fix ();
49
+ }
50
+
51
+ public function hasChanged ()
52
+ {
53
+ return !empty ($ this ->fixable ) || !empty ($ this ->nonFixable );
97
54
}
98
55
99
- if ( strpos ( $ this -> content , "\r" ) !== false ) {
100
- $ this -> content = str_replace ( array ( "\r\n" , "\r" ), "\n" , $ this -> content );
101
- $ this ->addFixable ( ' fix line endings to LF ' );
56
+ public function getFixable ()
57
+ {
58
+ return array_keys ( $ this ->fixable );
102
59
}
103
60
104
- /*if (strpos($this->content, "\t") !== false) {
105
- $this->content = str_replace("\t", ' ', $this->content);
106
- $this->addFixable('convert tabs to spaces' );
61
+ public function getNonFixable ()
62
+ {
63
+ return array_keys ( $ this ->nonFixable );
107
64
}
108
65
109
- if (preg_match('/ $/m', $this->content)) {
110
- $this->content = preg_replace('/ +$/m', '', $this->content);
111
- $this->addFixable('remove trailing whitespace') ;
66
+ public function getResult ()
67
+ {
68
+ return $ this ->content ;
112
69
}
113
70
114
- if (strlen($this->content) && substr($this->content, -1) != "\n") {
115
- $this->content .= "\n";
116
- $this->addFixable('add newline at end of file') ;
71
+ protected function addFixable ( $ fixable )
72
+ {
73
+ $ this ->fixable [ $ fixable ] = true ;
117
74
}
118
75
119
- if (preg_match("/\n{2,}$/", $this->content)) {
120
- $this->content = rtrim($this->content, "\n") . "\n";
121
- $this->addFixable('remove multiple newlines at end of file');
122
- }*/
123
- }
76
+ protected function addNonFixable ($ nonFixable )
77
+ {
78
+ $ this ->nonFixable [$ nonFixable ] = true ;
79
+ }
80
+
81
+ protected function fix ()
82
+ {
83
+ if (($ encoding = mb_detect_encoding ($ this ->content , 'UTF-8,ISO-8859-1,WINDOWS-1252 ' )) != 'UTF-8 ' ) {
84
+ if ($ encoding === false ) {
85
+ $ encoding = mb_detect_encoding ($ this ->content );
86
+ }
87
+ if ($ encoding !== false ) {
88
+ $ this ->content = iconv ($ encoding , 'UTF-8 ' , $ this ->content );
89
+ $ this ->addFixable ('fix encoding from ' . $ encoding . ' to UTF-8 ' );
90
+ } else {
91
+ $ this ->addNonFixable ('couldn \'t detect encoding, change it to UTF-8 ' );
92
+ }
93
+ } elseif (strpos ($ this ->content , "\xEF\xBB\xBF" ) === 0 ) {
94
+ $ this ->content = substr ($ this ->content , 3 );
95
+ $ this ->addFixable ('remove BOM (Byte Order Mark) ' );
96
+ }
97
+
98
+ if (strpos ($ this ->content , "\r" ) !== false ) {
99
+ $ this ->content = str_replace (array ("\r\n" , "\r" ), "\n" , $ this ->content );
100
+ $ this ->addFixable ('fix line endings to LF ' );
101
+ }
102
+
103
+ /*if (strpos($this->content, "\t") !== false) {
104
+ $this->content = str_replace("\t", ' ', $this->content);
105
+ $this->addFixable('convert tabs to spaces');
106
+ }
107
+
108
+ if (preg_match('/ $/m', $this->content)) {
109
+ $this->content = preg_replace('/ +$/m', '', $this->content);
110
+ $this->addFixable('remove trailing whitespace');
111
+ }
112
+
113
+ if (strlen($this->content) && substr($this->content, -1) != "\n") {
114
+ $this->content .= "\n";
115
+ $this->addFixable('add newline at end of file');
116
+ }
117
+
118
+ if (preg_match("/\n{2,}$/", $this->content)) {
119
+ $this->content = rtrim($this->content, "\n") . "\n";
120
+ $this->addFixable('remove multiple newlines at end of file');
121
+ }*/
122
+ }
124
123
}
125
124
126
125
$ fix = $ argv [1 ] == 'fix ' ;
127
126
128
127
$ dir = dirname (Phar::running (false )) ?: __DIR__ ;
129
128
$ files = null ;
130
129
if (isset ($ argv [2 ]) && $ argv [2 ][0 ] !== '- ' ) {
131
- if ($ argv [2 ] == 'package ' ) {
132
- if (!isset ($ argv [3 ]) || $ argv [3 ][0 ] === '- ' ) {
133
- echo 'ERROR: Missing package id! ' , PHP_EOL , PHP_EOL ;
134
- exit (1 );
135
- }
136
- $ package = $ argv [3 ];
137
- if (strpos ($ package , '/ ' ) === false ) {
138
- $ dir .= DIRECTORY_SEPARATOR . 'redaxo ' . DIRECTORY_SEPARATOR . 'include ' . DIRECTORY_SEPARATOR . 'addons ' . DIRECTORY_SEPARATOR . $ package ;
139
- } else {
140
- list ($ addon , $ plugin ) = explode ('/ ' , $ package , 2 );
141
- $ dir .= DIRECTORY_SEPARATOR . 'redaxo ' . DIRECTORY_SEPARATOR . 'include ' . DIRECTORY_SEPARATOR . 'addons ' . DIRECTORY_SEPARATOR . $ addon . DIRECTORY_SEPARATOR . 'plugins ' . DIRECTORY_SEPARATOR . $ plugin ;
142
- }
143
- if (!is_dir ($ dir )) {
144
- echo 'ERROR: Package " ' , $ package , '" does not exist! ' , PHP_EOL , PHP_EOL ;
145
- exit (1 );
146
- }
147
- } else {
148
- if (is_dir ($ argv [2 ])) {
149
- $ dir = realpath ($ argv [2 ]);
150
- } elseif (is_file ($ argv [2 ])) {
151
- $ file = realpath ($ argv [2 ]);
152
- $ files = array ($ file => $ file );
153
- $ dir = dirname ($ file );
130
+ if ($ argv [2 ] == 'package ' ) {
131
+ if (!isset ($ argv [3 ]) || $ argv [3 ][0 ] === '- ' ) {
132
+ echo 'ERROR: Missing package id! ' , PHP_EOL , PHP_EOL ;
133
+ exit (1 );
134
+ }
135
+ $ package = $ argv [3 ];
136
+ if (strpos ($ package , '/ ' ) === false ) {
137
+ $ dir .= DIRECTORY_SEPARATOR . 'redaxo ' . DIRECTORY_SEPARATOR . 'include ' . DIRECTORY_SEPARATOR . 'addons ' . DIRECTORY_SEPARATOR . $ package ;
138
+ } else {
139
+ list ($ addon , $ plugin ) = explode ('/ ' , $ package , 2 );
140
+ $ dir .= DIRECTORY_SEPARATOR . 'redaxo ' . DIRECTORY_SEPARATOR . 'include ' . DIRECTORY_SEPARATOR . 'addons ' . DIRECTORY_SEPARATOR . $ addon . DIRECTORY_SEPARATOR . 'plugins ' . DIRECTORY_SEPARATOR . $ plugin ;
141
+ }
142
+ if (!is_dir ($ dir )) {
143
+ echo 'ERROR: Package " ' , $ package , '" does not exist! ' , PHP_EOL , PHP_EOL ;
144
+ exit (1 );
145
+ }
154
146
} else {
155
- echo 'ERROR: Directory or file " ' , $ argv [2 ], '" does not exist! ' , PHP_EOL , PHP_EOL ;
156
- exit (1 );
147
+ if (is_dir ($ argv [2 ])) {
148
+ $ dir = realpath ($ argv [2 ]);
149
+ } elseif (is_file ($ argv [2 ])) {
150
+ $ file = realpath ($ argv [2 ]);
151
+ $ files = array ($ file => $ file );
152
+ $ dir = dirname ($ file );
153
+ } else {
154
+ echo 'ERROR: Directory or file " ' , $ argv [2 ], '" does not exist! ' , PHP_EOL , PHP_EOL ;
155
+ exit (1 );
156
+ }
157
157
}
158
- }
159
158
} elseif ($ input = file ('php://stdin ' , FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES )) {
160
- $ files = array_flip (array_map ('realpath ' , array_filter ($ input , 'file_exists ' )));
159
+ $ files = array_flip (array_map ('realpath ' , array_filter ($ input , 'file_exists ' )));
161
160
}
162
161
163
162
if (!is_array ($ files )) {
164
- $ files = new RecursiveIteratorIterator (new RecursiveDirectoryIterator ($ dir , RecursiveDirectoryIterator::CURRENT_AS_SELF | RecursiveDirectoryIterator::SKIP_DOTS ));
163
+ $ files = new RecursiveIteratorIterator (new RecursiveDirectoryIterator ($ dir , RecursiveDirectoryIterator::CURRENT_AS_SELF | RecursiveDirectoryIterator::SKIP_DOTS ));
165
164
}
166
165
167
166
$ hideProcess = in_array ('--hide-process ' , $ argv );
@@ -171,57 +170,57 @@ protected function fix()
171
170
$ countNonFixable = 0 ;
172
171
173
172
foreach ($ files as $ path => $ _n ) {
174
- $ subPath = str_replace ($ dir . DIRECTORY_SEPARATOR , '' , $ path );
175
- $ fileExt = pathinfo ($ path , PATHINFO_EXTENSION );
176
- if (!in_array ($ fileExt , $ textExtensions ) || strpos (DIRECTORY_SEPARATOR . $ subPath , DIRECTORY_SEPARATOR . 'vendor ' . DIRECTORY_SEPARATOR ) !== false ) {
177
- continue ;
178
- }
179
-
180
- if (!$ hideProcess ) {
181
- $ checkString = $ subPath ;
182
- if (mb_strlen ($ checkString ) > 60 ) {
183
- $ checkString = mb_substr ($ checkString , 0 , 20 ) . '... ' . mb_substr ($ checkString , -37 );
173
+ $ subPath = str_replace ($ dir . DIRECTORY_SEPARATOR , '' , $ path );
174
+ $ fileExt = pathinfo ($ path , PATHINFO_EXTENSION );
175
+ if (!in_array ($ fileExt , $ textExtensions ) || strpos (DIRECTORY_SEPARATOR . $ subPath , DIRECTORY_SEPARATOR . 'vendor ' . DIRECTORY_SEPARATOR ) !== false ) {
176
+ continue ;
184
177
}
185
- echo $ checkString = 'check ' . $ checkString . ' ... ' ;
186
- }
187
178
188
- $ countFiles ++;
189
- $ fixer = new rex_coding_standards_fixer (file_get_contents ($ path ));
179
+ if (!$ hideProcess ) {
180
+ $ checkString = $ subPath ;
181
+ if (mb_strlen ($ checkString ) > 60 ) {
182
+ $ checkString = mb_substr ($ checkString , 0 , 20 ) . '... ' . mb_substr ($ checkString , -37 );
183
+ }
184
+ echo $ checkString = 'check ' . $ checkString . ' ... ' ;
185
+ }
190
186
191
- if (!$ hideProcess ) {
192
- echo str_repeat ("\010 \010" , mb_strlen ($ checkString ));
193
- }
187
+ $ countFiles ++;
188
+ $ fixer = new rex_coding_standards_fixer (file_get_contents ($ path ));
194
189
195
- if ($ fixer ->hasChanged ()) {
196
- echo $ subPath , ': ' , PHP_EOL ;
197
- if ($ fixable = $ fixer ->getFixable ()) {
198
- echo ' > ' , implode (PHP_EOL . ' > ' , $ fixable ), PHP_EOL ;
199
- $ countFixable ++;
200
- }
201
- if ($ nonFixable = $ fixer ->getNonFixable ()) {
202
- echo ' ! ' , implode (PHP_EOL . ' ! ' , $ nonFixable ), PHP_EOL ;
203
- $ countNonFixable ++;
190
+ if (!$ hideProcess ) {
191
+ echo str_repeat ("\010 \010" , mb_strlen ($ checkString ));
204
192
}
205
- echo PHP_EOL ;
206
193
207
- if ($ fix ) {
208
- file_put_contents ($ path , $ fixer ->getResult ());
194
+ if ($ fixer ->hasChanged ()) {
195
+ echo $ subPath , ': ' , PHP_EOL ;
196
+ if ($ fixable = $ fixer ->getFixable ()) {
197
+ echo ' > ' , implode (PHP_EOL . ' > ' , $ fixable ), PHP_EOL ;
198
+ $ countFixable ++;
199
+ }
200
+ if ($ nonFixable = $ fixer ->getNonFixable ()) {
201
+ echo ' ! ' , implode (PHP_EOL . ' ! ' , $ nonFixable ), PHP_EOL ;
202
+ $ countNonFixable ++;
203
+ }
204
+ echo PHP_EOL ;
205
+
206
+ if ($ fix ) {
207
+ file_put_contents ($ path , $ fixer ->getResult ());
208
+ }
209
209
}
210
- }
211
210
}
212
211
213
212
echo '----------------------------------- ' , PHP_EOL ;
214
213
echo 'checked ' , $ countFiles , ' files ' , PHP_EOL ;
215
214
if ($ countFixable ) {
216
- echo '' , ($ fix ? 'fixed ' : 'found fixable ' ), ' problems in ' , $ countFixable , ' files ' , PHP_EOL ;
215
+ echo '' , ($ fix ? 'fixed ' : 'found fixable ' ), ' problems in ' , $ countFixable , ' files ' , PHP_EOL ;
217
216
}
218
217
if ($ countNonFixable ) {
219
- echo 'found non-fixable problems in ' , $ countNonFixable , ' files ' , PHP_EOL ;
218
+ echo 'found non-fixable problems in ' , $ countNonFixable , ' files ' , PHP_EOL ;
220
219
}
221
220
222
221
echo PHP_EOL ;
223
222
if ($ hasColorSupport ) {
224
- echo ($ countNonFixable + ($ fix ? 0 : $ countFixable )) ? "\033[1;37;41m " : "\033[1;30;42m " ;
223
+ echo ($ countNonFixable + ($ fix ? 0 : $ countFixable )) ? "\033[1;37;41m " : "\033[1;30;42m " ;
225
224
}
226
225
echo 'FINISHED, ' , !$ countFixable && !$ countNonFixable ? 'no problems ' : 'found problems ' ;
227
226
echo $ hasColorSupport ? "\033[0m " : '' ;
0 commit comments