44
55namespace Rector \SwissKnife \Command ;
66
7+ use Entropy \Console \Contract \CommandInterface ;
8+ use Entropy \Console \Enum \ExitCode ;
79use Rector \SwissKnife \Comments \CommentedCodeAnalyzer ;
810use Rector \SwissKnife \Finder \PhpFilesFinder ;
9- use Symfony \Component \Console \Command \Command ;
10- use Symfony \Component \Console \Input \InputArgument ;
11- use Symfony \Component \Console \Input \InputInterface ;
12- use Symfony \Component \Console \Input \InputOption ;
13- use Symfony \Component \Console \Output \OutputInterface ;
1411use Symfony \Component \Console \Style \SymfonyStyle ;
1512
16- final class CheckCommentedCodeCommand extends Command
13+ final readonly class CheckCommentedCodeCommand implements CommandInterface
1714{
1815 private const int DEFAULT_LINE_LIMIT = 5 ;
1916
2017 public function __construct (
21- private readonly CommentedCodeAnalyzer $ commentedCodeAnalyzer ,
22- private readonly SymfonyStyle $ symfonyStyle ,
18+ private CommentedCodeAnalyzer $ commentedCodeAnalyzer ,
19+ private SymfonyStyle $ symfonyStyle ,
2320 ) {
24- parent ::__construct ();
2521 }
2622
27- protected function configure (): void
23+ /**
24+ * @param string[] $sources One or more paths to check
25+ * @param string[] $skipFiles File paths to skip
26+ * @param int $lineLimit Maximum number of comment lines in a row allowed
27+ *
28+ * @return ExitCode::*
29+ */
30+ public function run (array $ sources , array $ skipFiles = [], int $ lineLimit = self ::DEFAULT_LINE_LIMIT ): int
2831 {
29- $ this ->setName ('check-commented-code ' );
30-
31- $ this ->addArgument (
32- 'sources ' ,
33- InputArgument::REQUIRED | InputArgument::IS_ARRAY ,
34- 'One or more paths to check '
35- );
36- $ this ->addOption (
37- 'skip-file ' ,
38- null ,
39- InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY ,
40- 'Skip file path '
41- );
42- $ this ->setDescription ('Checks code for commented snippets ' );
43-
44- $ this ->addOption (
45- 'line-limit ' ,
46- null ,
47- InputOption::VALUE_REQUIRED | InputOption::VALUE_OPTIONAL ,
48- 'Amount of allowed comment lines in a row ' ,
49- self ::DEFAULT_LINE_LIMIT
50- );
51- }
52-
53- protected function execute (InputInterface $ input , OutputInterface $ output ): int
54- {
55- $ sources = (array ) $ input ->getArgument ('sources ' );
56- $ skipFiles = (array ) $ input ->getOption ('skip-file ' );
57-
5832 $ phpFileInfos = PhpFilesFinder::find ($ sources , $ skipFiles );
5933
6034 $ message = sprintf ('Analysing %d *.php files ' , count ($ phpFileInfos ));
6135 $ this ->symfonyStyle ->note ($ message );
6236
63- $ lineLimit = (int ) $ input ->getOption ('line-limit ' );
64-
6537 $ commentedLinesByFilePaths = [];
6638 foreach ($ phpFileInfos as $ phpFileInfo ) {
6739 $ commentedLines = $ this ->commentedCodeAnalyzer ->process ($ phpFileInfo ->getRealPath (), $ lineLimit );
@@ -75,7 +47,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7547
7648 if ($ commentedLinesByFilePaths === []) {
7749 $ this ->symfonyStyle ->success ('No commented code found ' );
78- return self ::SUCCESS ;
50+ return ExitCode ::SUCCESS ;
7951 }
8052
8153 foreach ($ commentedLinesByFilePaths as $ filePath => $ commentedLines ) {
@@ -86,6 +58,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8658 }
8759
8860 $ this ->symfonyStyle ->error ('Errors found ' );
89- return self ::FAILURE ;
61+
62+ return ExitCode::ERROR ;
63+ }
64+
65+ public function getName (): string
66+ {
67+ return 'check-commented-code ' ;
68+ }
69+
70+ public function getDescription (): string
71+ {
72+ return 'Checks code for commented snippets ' ;
9073 }
9174}
0 commit comments