11
11
12
12
class Console implements Printer
13
13
{
14
- private const DEFAULT_LINE_LENGTH = 80 ;
15
-
16
14
private Highlighter $ highlighter ;
17
15
18
16
public function __construct (Highlighter $ highlighter )
@@ -22,47 +20,61 @@ public function __construct(Highlighter $highlighter)
22
20
23
21
public function printData (OutputInterface $ output , HintList $ hintList , array $ detections ): void
24
22
{
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 = [] ;
28
26
29
27
foreach ($ this ->groupDetectionResultPerFile ($ detections ) as $ detectionResults ) {
30
28
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 [] = '' ;
46
35
47
36
if ($ hintList ->hasHints ()) {
48
37
$ hints = $ hintList ->getHintsByValue ($ detection ->getValue ());
49
38
50
39
if ($ hints !== []) {
51
- $ output -> writeln ( 'Suggestions: ' ) ;
40
+ $ lines [] = 'Suggestions: ' ;
52
41
53
42
foreach ($ hints as $ hint ) {
54
- $ output -> writeln ( "\t\t" . $ hint) ;
43
+ $ lines [] = "\t\t" . $ hint ;
55
44
}
56
45
57
- $ output -> write ( PHP_EOL ) ;
46
+ $ lines [] = PHP_EOL ;
58
47
}
59
48
}
60
49
}
61
-
62
- $ output ->writeln ($ separator . PHP_EOL );
63
50
}
64
51
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
+ );
66
78
}
67
79
68
80
/**
@@ -75,7 +87,7 @@ private function groupDetectionResultPerFile(array $detections): array
75
87
$ groupedResult = [];
76
88
77
89
foreach ($ detections as $ detection ) {
78
- $ groupedResult [$ detection ->getFile ()-> getRelativePathname ()][] = $ detection ;
90
+ $ groupedResult [$ detection ->getFilePath ()][] = $ detection ;
79
91
}
80
92
81
93
return $ groupedResult ;
0 commit comments