55namespace TomasVotruba \ClassLeak \Commands ;
66
77use Symfony \Component \Console \Command \Command ;
8+ use Symfony \Component \Console \Helper \ProgressBar ;
89use Symfony \Component \Console \Input \InputArgument ;
910use Symfony \Component \Console \Input \InputInterface ;
1011use Symfony \Component \Console \Input \InputOption ;
@@ -83,6 +84,8 @@ protected function configure(): void
8384 'File extensions to check ' ,
8485 ['php ' ]
8586 );
87+
88+ $ this ->addOption ('json ' , null , InputOption::VALUE_NONE , 'Output as JSON ' );
8689 }
8790
8891 protected function execute (InputInterface $ input , OutputInterface $ output ): int
@@ -104,19 +107,28 @@ protected function execute(InputInterface $input, OutputInterface $output): int
104107 /** @var string[] $pathsToSkip */
105108 $ pathsToSkip = (array ) $ input ->getOption ('skip-path ' );
106109
110+ $ isJson = (bool ) $ input ->getOption ('json ' );
111+
107112 /** @var string[] $fileExtensions */
108113 $ fileExtensions = (array ) $ input ->getOption ('file-extension ' );
109114
110115 $ phpFilePaths = $ this ->phpFilesFinder ->findPhpFiles ($ paths , $ fileExtensions , $ pathsToSkip );
111116
112- $ this ->symfonyStyle ->title ('1. Finding used classes ' );
113- $ usedNames = $ this ->resolveUsedClassNames ($ phpFilePaths );
117+ $ progressBar = null ;
118+ if (! $ isJson ) {
119+ $ this ->symfonyStyle ->title ('1. Finding used classes ' );
120+ $ progressBar = $ this ->symfonyStyle ->createProgressBar (count ($ phpFilePaths ));
121+ }
122+ $ usedNames = $ this ->resolveUsedClassNames ($ phpFilePaths , $ progressBar );
114123
115124 $ this ->symfonyStyle ->newLine (2 );
116125
117- $ this ->symfonyStyle ->title ('2. Extracting existing files with classes ' );
118-
119- $ existingFilesWithClasses = $ this ->classNamesFinder ->resolveClassNamesToCheck ($ phpFilePaths );
126+ $ progressBar = null ;
127+ if (! $ isJson ) {
128+ $ this ->symfonyStyle ->title ('2. Extracting existing files with classes ' );
129+ $ progressBar = $ this ->symfonyStyle ->createProgressBar (count ($ phpFilePaths ));
130+ }
131+ $ existingFilesWithClasses = $ this ->classNamesFinder ->resolveClassNamesToCheck ($ phpFilePaths , $ progressBar );
120132
121133 $ this ->symfonyStyle ->newLine (2 );
122134
@@ -132,24 +144,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int
132144 $ unusedClassesResult = $ this ->unusedClassesResultFactory ->create ($ possiblyUnusedFilesWithClasses );
133145 $ this ->symfonyStyle ->newLine ();
134146
135- return $ this ->unusedClassReporter ->reportResult ($ unusedClassesResult );
147+ return $ this ->unusedClassReporter ->reportResult ($ unusedClassesResult, $ isJson );
136148 }
137149
138150 /**
139151 * @param string[] $phpFilePaths
140152 * @return string[]
141153 */
142- private function resolveUsedClassNames (array $ phpFilePaths ): array
154+ private function resolveUsedClassNames (array $ phpFilePaths, ? ProgressBar $ progressBar ): array
143155 {
144- $ progressBar = $ this ->symfonyStyle ->createProgressBar (count ($ phpFilePaths ));
145-
146156 $ usedNames = [];
147157
148158 foreach ($ phpFilePaths as $ phpFilePath ) {
149159 $ currentUsedNames = $ this ->useImportsResolver ->resolve ($ phpFilePath );
150160 $ usedNames = [...$ usedNames , ...$ currentUsedNames ];
151161
152- $ progressBar ->advance ();
162+ $ progressBar? ->advance();
153163 }
154164
155165 $ usedNames = array_unique ($ usedNames );
0 commit comments