11
11
12
12
namespace Symfony \Component \Console \Descriptor ;
13
13
14
- use Exception ;
15
14
use Symfony \Component \Console \Application ;
16
15
use Symfony \Component \Console \Command \Command ;
17
16
use Symfony \Component \Console \Helper \Helper ;
@@ -77,11 +76,11 @@ protected function describeInputOption(InputOption $option, array $options = [])
77
76
$ name .= '|- ' .str_replace ('| ' , '|- ' , $ option ->getShortcut ());
78
77
}
79
78
80
- $ option_description = $ option ->getDescription () ? preg_replace ('/\s*[\r\n]\s*/ ' , "\n\n" , $ option ->getDescription ())."\n\n" : '' ;
81
- $ option_description = (new UnicodeString ($ option_description ))->ascii ();
79
+ $ optionDescription = $ option ->getDescription () ? preg_replace ('/\s*[\r\n]\s*/ ' , "\n\n" , $ option ->getDescription ())."\n\n" : '' ;
80
+ $ optionDescription = (new UnicodeString ($ optionDescription ))->ascii ();
82
81
$ this ->write (
83
82
$ name ."\n" .str_repeat ($ this ->paragraphsChar , Helper::width ($ name ))."\n\n"
84
- .$ option_description
83
+ .$ optionDescription
85
84
.'- **Accept value**: ' .($ option ->acceptValue () ? 'yes ' : 'no ' )."\n"
86
85
.'- **Is value required**: ' .($ option ->isValueRequired () ? 'yes ' : 'no ' )."\n"
87
86
.'- **Is multiple**: ' .($ option ->isArray () ? 'yes ' : 'no ' )."\n"
@@ -92,22 +91,21 @@ protected function describeInputOption(InputOption $option, array $options = [])
92
91
93
92
protected function describeInputDefinition (InputDefinition $ definition , array $ options = []): void
94
93
{
95
- if ($ showArguments = (\count ( $ definition ->getArguments ()) > 0 )) {
94
+ if ($ showArguments = (( bool ) $ definition ->getArguments ())) {
96
95
$ this ->write ("Arguments \n" .str_repeat ($ this ->subsubsectionChar , 9 ))."\n\n" ;
97
96
foreach ($ definition ->getArguments () as $ argument ) {
98
97
$ this ->write ("\n\n" );
99
98
$ this ->describeInputArgument ($ argument );
100
99
}
101
100
}
102
101
103
- $ non_default_options = $ this ->getNonDefaultOptions ($ definition );
104
- if (\count ($ non_default_options ) > 0 ) {
102
+ if ($ nonDefaultOptions = $ this ->getNonDefaultOptions ($ definition )) {
105
103
if ($ showArguments ) {
106
104
$ this ->write ("\n\n" );
107
105
}
108
106
109
107
$ this ->write ("Options \n" .str_repeat ($ this ->subsubsectionChar , 7 )."\n\n" );
110
- foreach ($ non_default_options as $ option ) {
108
+ foreach ($ nonDefaultOptions as $ option ) {
111
109
$ this ->describeInputOption ($ option );
112
110
$ this ->write ("\n" );
113
111
}
@@ -122,9 +120,7 @@ protected function describeCommand(Command $command, array $options = []): void
122
120
.str_repeat ($ this ->subsectionChar , Helper::width ($ command ->getName ()))."\n\n"
123
121
.($ command ->getDescription () ? $ command ->getDescription ()."\n\n" : '' )
124
122
."Usage \n" .str_repeat ($ this ->paragraphsChar , 5 )."\n\n"
125
- .array_reduce ($ command ->getAliases (), static function ($ carry , $ usage ) {
126
- return $ carry .'- `` ' .$ usage .'`` ' ."\n" ;
127
- })
123
+ .array_reduce ($ command ->getAliases (), static fn ($ carry , $ usage ) => $ carry .'- `` ' .$ usage .'`` ' ."\n" )
128
124
);
129
125
130
126
return ;
@@ -140,9 +136,7 @@ protected function describeCommand(Command $command, array $options = []): void
140
136
.str_repeat ($ this ->subsectionChar , Helper::width ($ command ->getName ()))."\n\n"
141
137
.($ command ->getDescription () ? $ command ->getDescription ()."\n\n" : '' )
142
138
."Usage \n" .str_repeat ($ this ->subsubsectionChar , 5 )."\n\n"
143
- .array_reduce (array_merge ([$ command ->getSynopsis ()], $ command ->getAliases (), $ command ->getUsages ()), static function ($ carry , $ usage ) {
144
- return $ carry .'- `` ' .$ usage .'`` ' ."\n" ;
145
- })
139
+ .array_reduce (array_merge ([$ command ->getSynopsis ()], $ command ->getAliases (), $ command ->getUsages ()), static fn ($ carry , $ usage ) => $ carry .'- `` ' .$ usage .'`` ' ."\n" )
146
140
);
147
141
148
142
if ($ help = $ command ->getProcessedHelp ()) {
@@ -159,8 +153,7 @@ protected function describeCommand(Command $command, array $options = []): void
159
153
160
154
protected function describeApplication (Application $ application , array $ options = []): void
161
155
{
162
- $ describedNamespace = $ options ['namespace ' ] ?? null ;
163
- $ description = new ApplicationDescription ($ application , $ describedNamespace );
156
+ $ description = new ApplicationDescription ($ application , $ options ['namespace ' ] ?? null );
164
157
$ title = $ this ->getApplicationTitle ($ application );
165
158
166
159
$ this ->write ($ title ."\n" .str_repeat ($ this ->partChar , Helper::width ($ title )));
@@ -170,15 +163,14 @@ protected function describeApplication(Application $application, array $options
170
163
171
164
private function getApplicationTitle (Application $ application ): string
172
165
{
173
- if ('UNKNOWN ' !== $ application ->getName ()) {
174
- if ('UNKNOWN ' !== $ application ->getVersion ()) {
175
- return sprintf ('%s %s ' , $ application ->getName (), $ application ->getVersion ());
176
- }
177
-
178
- return $ application ->getName ();
166
+ if ('UNKNOWN ' === $ application ->getName ()) {
167
+ return 'Console Tool ' ;
168
+ }
169
+ if ('UNKNOWN ' !== $ application ->getVersion ()) {
170
+ return sprintf ('%s %s ' , $ application ->getName (), $ application ->getVersion ());
179
171
}
180
172
181
- return ' Console Tool ' ;
173
+ return $ application -> getName () ;
182
174
}
183
175
184
176
private function describeCommands ($ application , array $ options ): void
@@ -193,23 +185,19 @@ private function describeCommands($application, array $options): void
193
185
$ commands = $ application ->all ($ namespace );
194
186
$ this ->write ($ namespace ."\n" .str_repeat ($ this ->sectionChar , Helper::width ($ namespace ))."\n\n" );
195
187
}
196
- $ commands = $ this ->removeAliasesAndHiddenCommands ($ commands );
197
188
198
- foreach ($ commands as $ command ) {
189
+ foreach ($ this -> removeAliasesAndHiddenCommands ( $ commands) as $ command ) {
199
190
$ this ->describeCommand ($ command , $ options );
200
191
$ this ->write ("\n\n" );
201
192
}
202
193
}
203
194
}
204
195
205
- /**
206
- * @param \Symfony\Component\Console\Descriptor\ApplicationDescription $description
207
- */
208
196
private function createTableOfContents (ApplicationDescription $ description , Application $ application ): void
209
197
{
210
198
$ this ->setVisibleNamespaces ($ description );
211
- $ chapter_title = 'Table of Contents ' ;
212
- $ this ->write ("\n\n$ chapter_title \n" .str_repeat ($ this ->chapterChar , Helper::width ($ chapter_title ))."\n\n" );
199
+ $ chapterTitle = 'Table of Contents ' ;
200
+ $ this ->write ("\n\n$ chapterTitle \n" .str_repeat ($ this ->chapterChar , Helper::width ($ chapterTitle ))."\n\n" );
213
201
foreach ($ this ->visibleNamespaces as $ namespace ) {
214
202
if ('_global ' === $ namespace ) {
215
203
$ commands = $ application ->all ('' );
@@ -221,65 +209,57 @@ private function createTableOfContents(ApplicationDescription $description, Appl
221
209
$ commands = $ this ->removeAliasesAndHiddenCommands ($ commands );
222
210
223
211
$ this ->write ("\n\n" );
224
- $ this ->write (implode ("\n" , array_map (static function ($ commandName ) {
225
- return sprintf ('- `%s`_ ' , $ commandName );
226
- }, array_keys ($ commands ))));
212
+ $ this ->write (implode ("\n" , array_map (static fn ($ commandName ) => sprintf ('- `%s`_ ' , $ commandName ), array_keys ($ commands ))));
227
213
}
228
214
}
229
215
230
216
private function getNonDefaultOptions (InputDefinition $ definition ): array
231
217
{
232
- $ global_options = [
218
+ $ globalOptions = [
233
219
'help ' ,
234
220
'quiet ' ,
235
221
'verbose ' ,
236
222
'version ' ,
237
223
'ansi ' ,
238
224
'no-interaction ' ,
239
225
];
240
- $ non_default_options = [];
226
+ $ nonDefaultOptions = [];
241
227
foreach ($ definition ->getOptions () as $ option ) {
242
228
// Skip global options.
243
- // @todo Show these once at the beginning.
244
- if (!\in_array ($ option ->getName (), $ global_options )) {
245
- $ non_default_options [] = $ option ;
229
+ if (!\in_array ($ option ->getName (), $ globalOptions )) {
230
+ $ nonDefaultOptions [] = $ option ;
246
231
}
247
232
}
248
233
249
- return $ non_default_options ;
234
+ return $ nonDefaultOptions ;
250
235
}
251
236
252
- /**
253
- * @param \Symfony\Component\Console\Descriptor\ApplicationDescription $description
254
- */
255
237
private function setVisibleNamespaces (ApplicationDescription $ description ): void
256
238
{
257
239
$ commands = $ description ->getCommands ();
258
240
foreach ($ description ->getNamespaces () as $ namespace ) {
259
241
try {
260
- // Remove aliases.
261
- $ namespace_commands = $ namespace ['commands ' ];
262
- foreach ($ namespace_commands as $ key => $ command_name ) {
263
- if (!\array_key_exists ($ command_name , $ commands )) {
242
+ $ namespaceCommands = $ namespace ['commands ' ];
243
+ foreach ($ namespaceCommands as $ key => $ commandName ) {
244
+ if (!\array_key_exists ($ commandName , $ commands )) {
264
245
// If the array key does not exist, then this is an alias.
265
- unset($ namespace_commands [$ key ]);
266
- } elseif ($ commands [$ command_name ]->isHidden ()) {
267
- unset($ namespace_commands [$ key ]);
246
+ unset($ namespaceCommands [$ key ]);
247
+ } elseif ($ commands [$ commandName ]->isHidden ()) {
248
+ unset($ namespaceCommands [$ key ]);
268
249
}
269
250
}
270
- if (!\count ( $ namespace_commands ) ) {
251
+ if (!$ namespaceCommands ) {
271
252
// If the namespace contained only aliases or hidden commands, skip the namespace.
272
253
continue ;
273
254
}
274
- } catch (Exception ) {
255
+ } catch (\ Exception ) {
275
256
}
276
257
$ this ->visibleNamespaces [] = $ namespace ['id ' ];
277
258
}
278
259
}
279
260
280
261
private function removeAliasesAndHiddenCommands (array $ commands ): array
281
262
{
282
- // Remove aliases.
283
263
foreach ($ commands as $ key => $ command ) {
284
264
if ($ command ->isHidden () || \in_array ($ key , $ command ->getAliases (), true )) {
285
265
unset($ commands [$ key ]);
0 commit comments