16
16
namespace Mmoreram \PHPFormatter \Command ;
17
17
18
18
use Exception ;
19
+ use IteratorAggregate ;
19
20
use Symfony \Component \Console \Command \Command ;
20
21
use Symfony \Component \Console \Input \InputArgument ;
21
22
use Symfony \Component \Console \Input \InputInterface ;
@@ -104,10 +105,66 @@ protected function configure()
104
105
*/
105
106
protected function execute (InputInterface $ input , OutputInterface $ output )
106
107
{
107
- $ verbose = $ output ->getVerbosity ();
108
108
$ path = $ input ->getArgument ('path ' );
109
- $ dryRun = $ input ->getOption ('dry-run ' );
109
+
110
+ /**
111
+ * We load the options to work with
112
+ */
113
+ $ options = $ this ->getUsableConfig ($ input );
114
+
115
+ /**
116
+ * Building the real directory or file to work in
117
+ */
118
+ $ filesystem = new Filesystem ();
119
+ if (!$ filesystem ->isAbsolutePath ($ path )) {
120
+ $ path = getcwd () . DIRECTORY_SEPARATOR . $ path ;
121
+ }
122
+
123
+ if (!is_file ($ path ) && !is_dir ($ path )) {
124
+
125
+ throw new Exception ('Directory or file " ' . $ path . '" does not exist ' );
126
+ }
127
+
128
+ /**
129
+ * Print dry-run message if needed
130
+ */
131
+ $ this ->printDryRunMessage (
132
+ $ input ,
133
+ $ output ,
134
+ $ path
135
+ );
136
+
137
+ /**
138
+ * Print all configuration block if verbose level allows it
139
+ */
140
+ $ this ->printConfigUsed (
141
+ $ output ,
142
+ $ options
143
+ );
144
+
110
145
$ fileFinder = new FileFinder ;
146
+ $ files = $ fileFinder ->findPHPFilesByPath ($ path );
147
+
148
+ /**
149
+ * Parse and fix all found files
150
+ */
151
+ $ this ->parseAndFixFiles (
152
+ $ input ,
153
+ $ output ,
154
+ $ files ,
155
+ $ options
156
+ );
157
+ }
158
+
159
+ /**
160
+ * Load config
161
+ *
162
+ * @param InputInterface $input Input
163
+ *
164
+ * @return array Config array
165
+ */
166
+ public function getUsableConfig (InputInterface $ input )
167
+ {
111
168
$ configLoader = new ConfigLoader ;
112
169
$ configFinder = new ConfigFinder ;
113
170
@@ -119,7 +176,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
119
176
*/
120
177
$ configPath = rtrim ($ input ->getOption ('config ' ), DIRECTORY_SEPARATOR );
121
178
122
- $ options = $ configLoader ->loadConfigValues (
179
+ return $ configLoader ->loadConfigValues (
123
180
self ::COMMAND_NAME ,
124
181
$ configFinder ->findConfigFile ($ configPath ),
125
182
array (
@@ -135,19 +192,25 @@ protected function execute(InputInterface $input, OutputInterface $output)
135
192
'sort-direction ' => UseSorter::SORT_DIRECTION_ASC
136
193
)
137
194
);
195
+ }
138
196
139
- /**
140
- * Building the real directory or file to work in
141
- */
142
- $ filesystem = new Filesystem ();
143
- if (!$ filesystem ->isAbsolutePath ($ path )) {
144
- $ path = getcwd () . DIRECTORY_SEPARATOR . $ path ;
145
- }
146
-
147
- if (!is_file ($ path ) && !is_dir ($ path )) {
148
-
149
- throw new Exception ('Directory or file " ' . $ path . '" does not exist ' );
150
- }
197
+ /**
198
+ * Print the Dry-run message if needed
199
+ *
200
+ * @param InputInterface $input Input
201
+ * @param OutputInterface $output Output
202
+ * @param string $path Path
203
+ *
204
+ * @return UseSortCommand self Object
205
+ */
206
+ public function printDryRunMessage (
207
+ InputInterface $ input ,
208
+ OutputInterface $ output ,
209
+ $ path
210
+ )
211
+ {
212
+ $ dryRun = $ input ->getOption ('dry-run ' );
213
+ $ verbose = $ output ->getVerbosity ();
151
214
152
215
/**
153
216
* Dry-run message
@@ -162,17 +225,23 @@ protected function execute(InputInterface $input, OutputInterface $output)
162
225
$ output ->writeln ('# Executing process in ' . $ path );
163
226
}
164
227
165
- /**
166
- * Creates the new UseSorter file, given config values
167
- */
168
- $ useSorter = new UseSorter ();
169
- $ useSorter
170
- ->setGroups ($ options ['group ' ])
171
- ->setGroupType ($ options ['group-type ' ])
172
- ->setSortType ($ options ['sort-type ' ])
173
- ->setSortDirection ($ options ['sort-direction ' ]);
228
+ return $ this ;
229
+ }
174
230
175
- $ files = $ fileFinder ->findPHPFilesByPath ($ path );
231
+ /**
232
+ * Print the configuration used by the command
233
+ *
234
+ * @param OutputInterface $output Output
235
+ * @param array $options Options used by the command
236
+ *
237
+ * @return UseSortCommand self Object
238
+ */
239
+ public function printConfigUsed (
240
+ OutputInterface $ output ,
241
+ array $ options
242
+ )
243
+ {
244
+ $ verbose = $ output ->getVerbosity ();
176
245
177
246
/**
178
247
* If verbose level is higher or equal than -vv, we print the config
@@ -207,6 +276,31 @@ protected function execute(InputInterface $input, OutputInterface $output)
207
276
208
277
$ output ->writeln ('# ' );
209
278
279
+ return $ this ;
280
+ }
281
+
282
+ /**
283
+ * Parse and fix all files found
284
+ *
285
+ * @param InputInterface $input Input
286
+ * @param OutputInterface $output Output
287
+ * @param IteratorAggregate $files Files
288
+ * @param array $options Options
289
+ *
290
+ * @return UseSortCommand self Object
291
+ */
292
+ public function parseAndFixFiles (
293
+ InputInterface $ input ,
294
+ OutputInterface $ output ,
295
+ IteratorAggregate $ files ,
296
+ array $ options
297
+
298
+ )
299
+ {
300
+ $ dryRun = $ input ->getOption ('dry-run ' );
301
+ $ verbose = $ output ->getVerbosity ();
302
+ $ useSorter = $ this ->createUseSorter ($ options );
303
+
210
304
/**
211
305
* Each found php file is processed
212
306
*/
@@ -230,5 +324,29 @@ protected function execute(InputInterface $input, OutputInterface $output)
230
324
file_put_contents ($ file , $ result );
231
325
}
232
326
}
327
+
328
+ return $ this ;
329
+ }
330
+
331
+ /**
332
+ * Create UseSorter Object given a configuration
333
+ *
334
+ * @param array $options Options
335
+ *
336
+ * @return UseSorter Use sorter instance
337
+ */
338
+ public function createUseSorter (array $ options )
339
+ {
340
+ /**
341
+ * Creates the new UseSorter file, given config values
342
+ */
343
+ $ useSorter = new UseSorter ();
344
+ $ useSorter
345
+ ->setGroups ($ options ['group ' ])
346
+ ->setGroupType ($ options ['group-type ' ])
347
+ ->setSortType ($ options ['sort-type ' ])
348
+ ->setSortDirection ($ options ['sort-direction ' ]);
349
+
350
+ return $ useSorter ;
233
351
}
234
352
}
0 commit comments