1111
1212class Console implements Printer
1313{
14- private const DEFAULT_LINE_LENGTH = 80 ;
15-
1614 private Highlighter $ highlighter ;
1715
1816 public function __construct (Highlighter $ highlighter )
@@ -22,47 +20,61 @@ public function __construct(Highlighter $highlighter)
2220
2321 public function printData (OutputInterface $ output , HintList $ hintList , array $ detections ): void
2422 {
25- $ length = ( int ) (` tput cols ` ?: self :: DEFAULT_LINE_LENGTH ) ;
26- $ separator = str_repeat ( ' - ' , $ length );
27- $ output -> writeln ( PHP_EOL . $ separator . PHP_EOL ) ;
23+ $ index = 0 ;
24+
25+ $ lines = [] ;
2826
2927 foreach ($ this ->groupDetectionResultPerFile ($ detections ) as $ detectionResults ) {
3028 foreach ($ detectionResults as $ detection ) {
31- $ output ->writeln (sprintf (
32- '%s:%d. Magic number: %s ' ,
33- $ detection ->getFile ()->getRelativePathname (),
34- $ detection ->getLine (),
35- $ detection ->getValue ()
36- ));
37-
38- $ output ->writeln (
39- $ this ->highlighter ->getCodeSnippet (
40- $ detection ->getFile ()->getContents (),
41- $ detection ->getLine (),
42- 0 ,
43- 0
44- )
45- );
29+ ++$ index ;
30+
31+ $ lines [] = $ this ->getDetectionLine ($ index , $ detection );
32+ $ lines [] = '' ;
33+ $ lines [] = $ this ->getSnippetLine ($ detection );
34+ $ lines [] = '' ;
4635
4736 if ($ hintList ->hasHints ()) {
4837 $ hints = $ hintList ->getHintsByValue ($ detection ->getValue ());
4938
5039 if ($ hints !== []) {
51- $ output -> writeln ( 'Suggestions: ' ) ;
40+ $ lines [] = 'Suggestions: ' ;
5241
5342 foreach ($ hints as $ hint ) {
54- $ output -> writeln ( "\t\t" . $ hint) ;
43+ $ lines [] = "\t\t" . $ hint ;
5544 }
5645
57- $ output -> write ( PHP_EOL ) ;
46+ $ lines [] = PHP_EOL ;
5847 }
5948 }
6049 }
61-
62- $ output ->writeln ($ separator . PHP_EOL );
6350 }
6451
65- $ output ->writeln ('<info>Total of Magic Numbers: ' . count ($ detections ) . '</info> ' );
52+ $ lines [] = '' ;
53+ $ lines [] = '' ;
54+ $ lines [] = '<info>Total of Magic Numbers: ' . count ($ detections ) . '</info> ' ;
55+
56+ $ output ->writeln ($ lines );
57+ }
58+
59+ private function getDetectionLine (int $ index , DetectionResult $ detection ): string
60+ {
61+ return sprintf (
62+ '%d) %s:%d Magic number: %s ' ,
63+ $ index ,
64+ $ detection ->getFilePath (),
65+ $ detection ->getLine (),
66+ $ detection ->getValue ()
67+ );
68+ }
69+
70+ private function getSnippetLine (DetectionResult $ detection ): string
71+ {
72+ return $ this ->highlighter ->getCodeSnippet (
73+ $ detection ->getSource (),
74+ $ detection ->getLine (),
75+ 0 ,
76+ 0
77+ );
6678 }
6779
6880 /**
@@ -75,7 +87,7 @@ private function groupDetectionResultPerFile(array $detections): array
7587 $ groupedResult = [];
7688
7789 foreach ($ detections as $ detection ) {
78- $ groupedResult [$ detection ->getFile ()-> getRelativePathname ()][] = $ detection ;
90+ $ groupedResult [$ detection ->getFilePath ()][] = $ detection ;
7991 }
8092
8193 return $ groupedResult ;
0 commit comments