18
18
use Symfony \Component \Console \Input \InputOption ;
19
19
use Symfony \Component \Console \Output \OutputInterface ;
20
20
use Exception ;
21
+ use Symfony \Component \Console \Style \SymfonyStyle ;
21
22
22
23
class StaticChecksCommand extends Command
23
24
{
@@ -35,6 +36,13 @@ class StaticChecksCommand extends Command
35
36
*/
36
37
private $ staticCheckObjects ;
37
38
39
+ /**
40
+ * Console output style
41
+ *
42
+ * @var SymfonyStyle
43
+ */
44
+ protected $ ioStyle ;
45
+
38
46
/**
39
47
* Configures the current command.
40
48
*
@@ -45,8 +53,8 @@ protected function configure()
45
53
$ list = new StaticChecksList ();
46
54
$ this ->allStaticCheckObjects = $ list ->getStaticChecks ();
47
55
$ staticCheckNames = implode (', ' , array_keys ($ this ->allStaticCheckObjects ));
48
- $ description = " This command will run all static checks on xml test materials. "
49
- . " Available static check scripts are: \n{ $ staticCheckNames}" ;
56
+ $ description = ' This command will run all static checks on xml test materials. '
57
+ . ' Available static check scripts are: ' . PHP_EOL . $ staticCheckNames ;
50
58
$ this ->setName ('static-checks ' )
51
59
->setDescription ($ description )
52
60
->addArgument (
@@ -72,36 +80,41 @@ protected function configure()
72
80
*/
73
81
protected function execute (InputInterface $ input , OutputInterface $ output )
74
82
{
83
+ $ this ->ioStyle = new SymfonyStyle ($ input , $ output );
75
84
try {
76
- $ this ->validateInputArguments ($ input );
85
+ $ this ->validateInput ($ input );
77
86
} catch (InvalidArgumentException $ e ) {
78
87
LoggingUtil::getInstance ()->getLogger (StaticChecksCommand::class)->error ($ e ->getMessage ());
79
- $ output -> writeln ($ e ->getMessage () . " Please fix input arguments and rerun. " );
88
+ $ this -> ioStyle -> error ($ e ->getMessage () . ' Please fix input argument(s) or option(s) and rerun. ' );
80
89
return 1 ;
81
90
}
82
91
92
+ $ cmdFailed = false ;
83
93
$ errors = [];
84
94
foreach ($ this ->staticCheckObjects as $ name => $ staticCheck ) {
85
95
LoggingUtil::getInstance ()->getLogger (get_class ($ staticCheck ))->info (
86
- "\nRunning static check script for: " . $ name
87
- );
88
- $ output ->writeln (
89
- "\nRunning static check script for: " . $ name
96
+ 'Running static check script for: ' . $ name . PHP_EOL
90
97
);
98
+
99
+ $ this ->ioStyle ->text (PHP_EOL . 'Running static check script for: ' . $ name . PHP_EOL );
91
100
$ start = microtime (true );
92
- $ staticCheck ->execute ($ input );
101
+ try {
102
+ $ staticCheck ->execute ($ input );
103
+ } catch (Exception $ e ) {
104
+ $ cmdFailed = true ;
105
+ LoggingUtil::getInstance ()->getLogger (get_class ($ staticCheck ))->error ($ e ->getMessage () . PHP_EOL );
106
+ $ this ->ioStyle ->error ($ e ->getMessage ());
107
+ }
93
108
$ end = microtime (true );
109
+ $ errors += $ staticCheck ->getErrors ();
94
110
95
111
$ staticOutput = $ staticCheck ->getOutput ();
96
112
LoggingUtil::getInstance ()->getLogger (get_class ($ staticCheck ))->info ($ staticOutput );
97
- $ output ->writeln ($ staticOutput );
98
- $ errors += $ staticCheck ->getErrors ();
99
- $ output ->writeln (
100
- "\nTotal execution time is " . (string )($ end - $ start ) . " seconds. "
101
- );
102
- }
113
+ $ this ->ioStyle ->text ($ staticOutput );
103
114
104
- if (empty ($ errors )) {
115
+ $ this ->ioStyle ->text ('Total execution time is ' . (string )($ end - $ start ) . ' seconds. ' . PHP_EOL );
116
+ }
117
+ if (!$ cmdFailed && empty ($ errors )) {
105
118
return 0 ;
106
119
} else {
107
120
return 1 ;
@@ -115,7 +128,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
115
128
* @return void
116
129
* @throws InvalidArgumentException
117
130
*/
118
- private function validateInputArguments (InputInterface $ input )
131
+ private function validateInput (InputInterface $ input )
119
132
{
120
133
$ this ->staticCheckObjects = [];
121
134
$ requiredChecksNames = $ input ->getArgument ('names ' );
@@ -137,8 +150,18 @@ private function validateInputArguments(InputInterface $input)
137
150
138
151
if (!empty ($ invalidCheckNames )) {
139
152
throw new InvalidArgumentException (
140
- " Invalid static check script(s): " . implode (', ' , $ invalidCheckNames ) . " . "
153
+ ' Invalid static check script(s): ' . implode (', ' , $ invalidCheckNames ) . ' . '
141
154
);
142
155
}
156
+
157
+ if ($ input ->getOption ('path ' )) {
158
+ if ( (count ($ this ->staticCheckObjects ) !== 1 )
159
+ || array_keys ($ this ->staticCheckObjects )[0 ] !== StaticChecksList::DEPRECATED_ENTITY_USAGE_CHECK_NAME )
160
+ throw new InvalidArgumentException (
161
+ '--path option can only be used for " '
162
+ . StaticChecksList::DEPRECATED_ENTITY_USAGE_CHECK_NAME
163
+ . '". '
164
+ );
165
+ }
143
166
}
144
167
}
0 commit comments