28
28
class LintCommand extends Command
29
29
{
30
30
private $ parser ;
31
+ private $ format ;
32
+ private $ displayCorrectFiles ;
31
33
32
34
/**
33
35
* {@inheritdoc}
@@ -65,13 +67,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
65
67
{
66
68
$ io = new SymfonyStyle ($ input , $ output );
67
69
$ filename = $ input ->getArgument ('filename ' );
70
+ $ this ->format = $ input ->getOption ('format ' );
71
+ $ this ->displayCorrectFiles = $ output ->isVerbose ();
68
72
69
73
if (!$ filename ) {
70
74
if (!$ stdin = $ this ->getStdin ()) {
71
75
throw new \RuntimeException ('Please provide a filename or pipe file content to STDIN. ' );
72
76
}
73
77
74
- return $ this ->display ($ input , $ output , $ io , array ($ this ->validate ($ stdin )));
78
+ return $ this ->display ($ io , array ($ this ->validate ($ stdin )));
75
79
}
76
80
77
81
if (!$ this ->isReadable ($ filename )) {
@@ -83,7 +87,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
83
87
$ filesInfo [] = $ this ->validate (file_get_contents ($ file ), $ file );
84
88
}
85
89
86
- return $ this ->display ($ input , $ output , $ io , $ filesInfo );
90
+ return $ this ->display ($ io , $ filesInfo );
87
91
}
88
92
89
93
private function validate ($ content , $ file = null )
@@ -97,29 +101,29 @@ private function validate($content, $file = null)
97
101
return array ('file ' => $ file , 'valid ' => true );
98
102
}
99
103
100
- private function display (InputInterface $ input , OutputInterface $ output , SymfonyStyle $ io , $ files )
104
+ private function display (SymfonyStyle $ io , array $ files )
101
105
{
102
- switch ($ input -> getOption ( ' format ' ) ) {
106
+ switch ($ this -> format ) {
103
107
case 'txt ' :
104
- return $ this ->displayTxt ($ output , $ io , $ files );
108
+ return $ this ->displayTxt ($ io , $ files );
105
109
case 'json ' :
106
110
return $ this ->displayJson ($ io , $ files );
107
111
default :
108
- throw new \InvalidArgumentException (sprintf ('The format "%s" is not supported. ' , $ input -> getOption ( ' format ' ) ));
112
+ throw new \InvalidArgumentException (sprintf ('The format "%s" is not supported. ' , $ this -> format ));
109
113
}
110
114
}
111
115
112
- private function displayTxt (OutputInterface $ output , SymfonyStyle $ io , $ filesInfo )
116
+ private function displayTxt (SymfonyStyle $ io , array $ filesInfo )
113
117
{
114
118
$ countFiles = count ($ filesInfo );
115
119
$ erroredFiles = 0 ;
116
120
117
121
foreach ($ filesInfo as $ info ) {
118
- if ($ info ['valid ' ] && $ output -> isVerbose () ) {
122
+ if ($ info ['valid ' ] && $ this -> displayCorrectFiles ) {
119
123
$ io ->comment ('<info>OK</info> ' .($ info ['file ' ] ? sprintf (' in %s ' , $ info ['file ' ]) : '' ));
120
124
} elseif (!$ info ['valid ' ]) {
121
125
++$ erroredFiles ;
122
- $ io ->text (sprintf ( '<error> ERROR </error> in %s ' , $ info ['file ' ]));
126
+ $ io ->text ('<error> ERROR </error> ' .( $ info [ ' file ' ] ? sprintf ( ' in %s ' , $ info ['file ' ]) : '' ));
123
127
$ io ->text (sprintf ('<error> >> %s</error> ' , $ info ['message ' ]));
124
128
}
125
129
}
@@ -133,7 +137,7 @@ private function displayTxt(OutputInterface $output, SymfonyStyle $io, $filesInf
133
137
return min ($ erroredFiles , 1 );
134
138
}
135
139
136
- private function displayJson (OutputInterface $ output , $ filesInfo )
140
+ private function displayJson (SymfonyStyle $ io , array $ filesInfo )
137
141
{
138
142
$ errors = 0 ;
139
143
@@ -144,7 +148,7 @@ private function displayJson(OutputInterface $output, $filesInfo)
144
148
}
145
149
});
146
150
147
- $ output ->writeln (json_encode ($ filesInfo , JSON_PRETTY_PRINT ));
151
+ $ io ->writeln (json_encode ($ filesInfo , JSON_PRETTY_PRINT ));
148
152
149
153
return min ($ errors , 1 );
150
154
}
0 commit comments