1616namespace Mmoreram \PHPFormatter \Command ;
1717
1818use Exception ;
19+ use IteratorAggregate ;
1920use Symfony \Component \Console \Command \Command ;
2021use Symfony \Component \Console \Input \InputArgument ;
2122use Symfony \Component \Console \Input \InputInterface ;
@@ -104,10 +105,66 @@ protected function configure()
104105 */
105106 protected function execute (InputInterface $ input , OutputInterface $ output )
106107 {
107- $ verbose = $ output ->getVerbosity ();
108108 $ 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+
110145 $ 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+ {
111168 $ configLoader = new ConfigLoader ;
112169 $ configFinder = new ConfigFinder ;
113170
@@ -119,7 +176,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
119176 */
120177 $ configPath = rtrim ($ input ->getOption ('config ' ), DIRECTORY_SEPARATOR );
121178
122- $ options = $ configLoader ->loadConfigValues (
179+ return $ configLoader ->loadConfigValues (
123180 self ::COMMAND_NAME ,
124181 $ configFinder ->findConfigFile ($ configPath ),
125182 array (
@@ -135,19 +192,25 @@ protected function execute(InputInterface $input, OutputInterface $output)
135192 'sort-direction ' => UseSorter::SORT_DIRECTION_ASC
136193 )
137194 );
195+ }
138196
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 ();
151214
152215 /**
153216 * Dry-run message
@@ -162,17 +225,23 @@ protected function execute(InputInterface $input, OutputInterface $output)
162225 $ output ->writeln ('# Executing process in ' . $ path );
163226 }
164227
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+ }
174230
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 ();
176245
177246 /**
178247 * If verbose level is higher or equal than -vv, we print the config
@@ -207,6 +276,31 @@ protected function execute(InputInterface $input, OutputInterface $output)
207276
208277 $ output ->writeln ('# ' );
209278
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+
210304 /**
211305 * Each found php file is processed
212306 */
@@ -230,5 +324,29 @@ protected function execute(InputInterface $input, OutputInterface $output)
230324 file_put_contents ($ file , $ result );
231325 }
232326 }
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 ;
233351 }
234352}
0 commit comments