15
15
use Symfony \Component \Console \Exception \InvalidArgumentException ;
16
16
use Symfony \Component \Console \Input \InputArgument ;
17
17
use Symfony \Component \Console \Input \InputInterface ;
18
+ use Symfony \Component \Console \Input \InputOption ;
18
19
use Symfony \Component \Console \Output \OutputInterface ;
19
20
use Exception ;
21
+ use Symfony \Component \Console \Style \SymfonyStyle ;
20
22
21
23
class StaticChecksCommand extends Command
22
24
{
@@ -34,6 +36,13 @@ class StaticChecksCommand extends Command
34
36
*/
35
37
private $ staticCheckObjects ;
36
38
39
+ /**
40
+ * Console output style
41
+ *
42
+ * @var SymfonyStyle
43
+ */
44
+ protected $ ioStyle ;
45
+
37
46
/**
38
47
* Configures the current command.
39
48
*
@@ -44,14 +53,20 @@ protected function configure()
44
53
$ list = new StaticChecksList ();
45
54
$ this ->allStaticCheckObjects = $ list ->getStaticChecks ();
46
55
$ staticCheckNames = implode (', ' , array_keys ($ this ->allStaticCheckObjects ));
47
- $ description = " This command will run all static checks on xml test materials. "
48
- . " 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 ;
49
58
$ this ->setName ('static-checks ' )
50
59
->setDescription ($ description )
51
60
->addArgument (
52
61
'names ' ,
53
62
InputArgument::OPTIONAL | InputArgument::IS_ARRAY ,
54
63
'name(s) of specific static check script(s) to run '
64
+ )->addOption (
65
+ 'path ' ,
66
+ 'p ' ,
67
+ InputOption::VALUE_OPTIONAL ,
68
+ 'Path to a MFTF test module to run "deprecatedEntityUsage" static check script. ' . PHP_EOL
69
+ . 'Option is ignored by other static check scripts. ' . PHP_EOL
55
70
);
56
71
}
57
72
@@ -65,32 +80,41 @@ protected function configure()
65
80
*/
66
81
protected function execute (InputInterface $ input , OutputInterface $ output )
67
82
{
83
+ $ this ->ioStyle = new SymfonyStyle ($ input , $ output );
68
84
try {
69
- $ this ->validateInputArguments ($ input, $ output );
85
+ $ this ->validateInput ($ input );
70
86
} catch (InvalidArgumentException $ e ) {
71
87
LoggingUtil::getInstance ()->getLogger (StaticChecksCommand::class)->error ($ e ->getMessage ());
72
- $ 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. ' );
73
89
return 1 ;
74
90
}
75
91
92
+ $ cmdFailed = false ;
76
93
$ errors = [];
77
94
foreach ($ this ->staticCheckObjects as $ name => $ staticCheck ) {
78
95
LoggingUtil::getInstance ()->getLogger (get_class ($ staticCheck ))->info (
79
- "\nRunning static check script for: " . $ name
80
- );
81
- $ output ->writeln (
82
- "\nRunning static check script for: " . $ name
96
+ 'Running static check script for: ' . $ name . PHP_EOL
83
97
);
84
98
85
- $ staticCheck ->execute ($ input );
99
+ $ this ->ioStyle ->text (PHP_EOL . 'Running static check script for: ' . $ name . PHP_EOL );
100
+ $ start = microtime (true );
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
+ }
108
+ $ end = microtime (true );
109
+ $ errors += $ staticCheck ->getErrors ();
86
110
87
111
$ staticOutput = $ staticCheck ->getOutput ();
88
112
LoggingUtil::getInstance ()->getLogger (get_class ($ staticCheck ))->info ($ staticOutput );
89
- $ output ->writeln ($ staticOutput );
90
- $ errors += $ staticCheck ->getErrors ();
91
- }
113
+ $ this ->ioStyle ->text ($ staticOutput );
92
114
93
- if (empty ($ errors )) {
115
+ $ this ->ioStyle ->text ('Total execution time is ' . (string )($ end - $ start ) . ' seconds. ' . PHP_EOL );
116
+ }
117
+ if (!$ cmdFailed && empty ($ errors )) {
94
118
return 0 ;
95
119
} else {
96
120
return 1 ;
@@ -104,7 +128,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
104
128
* @return void
105
129
* @throws InvalidArgumentException
106
130
*/
107
- private function validateInputArguments (InputInterface $ input )
131
+ private function validateInput (InputInterface $ input )
108
132
{
109
133
$ this ->staticCheckObjects = [];
110
134
$ requiredChecksNames = $ input ->getArgument ('names ' );
@@ -126,8 +150,18 @@ private function validateInputArguments(InputInterface $input)
126
150
127
151
if (!empty ($ invalidCheckNames )) {
128
152
throw new InvalidArgumentException (
129
- " Invalid static check script(s): " . implode (', ' , $ invalidCheckNames ) . " . "
153
+ ' Invalid static check script(s): ' . implode (', ' , $ invalidCheckNames ) . ' . '
130
154
);
131
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
+ }
132
166
}
133
167
}
0 commit comments