3030use Symfony \Component \Console \Output \OutputInterface ;
3131use Symfony \Component \Console \Style \SymfonyStyle ;
3232use Symfony \Component \HttpKernel \Kernel ;
33- use function is_string ;
34- use function sprintf ;
3533
3634/**
3735 * @author Javier Eguiluz <javier.eguiluz@gmail.com>
@@ -45,7 +43,7 @@ public function __construct(private ?PhpCompatUtil $phpCompatUtil = null)
4543 @trigger_deprecation (
4644 'symfony/maker-bundle ' ,
4745 '1.55.0 ' ,
48- sprintf ('Initializing MakeCommand while providing an instance of "%s" is deprecated. The $phpCompatUtil param will be removed in a future version. ' , PhpCompatUtil::class),
46+ \ sprintf ('Initializing MakeCommand while providing an instance of "%s" is deprecated. The $phpCompatUtil param will be removed in a future version. ' , PhpCompatUtil::class),
4947 );
5048 }
5149 }
@@ -63,7 +61,7 @@ public static function getCommandDescription(): string
6361 public function configureCommand (Command $ command , InputConfiguration $ inputConfig ): void
6462 {
6563 $ command
66- ->addArgument ('name ' , InputArgument::OPTIONAL , sprintf ('Choose a command name (e.g. <fg=yellow>app:%s</>) ' , Str::asCommand (Str::getRandomTerm ())))
64+ ->addArgument ('name ' , InputArgument::OPTIONAL , \ sprintf ('Choose a command name (e.g. <fg=yellow>app:%s</>) ' , Str::asCommand (Str::getRandomTerm ())))
6765 ->setHelp ($ this ->getHelpFileContents ('MakeCommand.txt ' ));
6866
6967 if ($ this ->supportsInvokableCommand ()) {
@@ -73,19 +71,14 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
7371
7472 public function generate (InputInterface $ input , ConsoleStyle $ io , Generator $ generator ): void
7573 {
76- if (true !== $ input ->getOption ('invokable ' ) && $ this ->supportsInvokableCommand ()) {
77- $ wantsInvokable = $ io ->confirm ('Would you like this command to be inokvable? ' , false );
78- $ input ->setOption ('invokable ' , $ wantsInvokable );
79- }
80-
8174 $ commandName = trim ($ input ->getArgument ('name ' ));
8275 $ commandNameHasAppPrefix = str_starts_with ($ commandName , 'app: ' );
8376
8477 $ commandClassNameDetails = $ generator ->createClassNameDetails (
8578 $ commandNameHasAppPrefix ? substr ($ commandName , 4 ) : $ commandName ,
8679 'Command \\' ,
8780 'Command ' ,
88- sprintf ('The "%s" command name is not valid because it would be implemented by "%s" class, which is not valid as a PHP class name (it must start with a letter or underscore, followed by any number of letters, numbers, or underscores). ' , $ commandName , Str::asClassName ($ commandName , 'Command ' ))
81+ \ sprintf ('The "%s" command name is not valid because it would be implemented by "%s" class, which is not valid as a PHP class name (it must start with a letter or underscore, followed by any number of letters, numbers, or underscores). ' , $ commandName , Str::asClassName ($ commandName , 'Command ' ))
8982 );
9083
9184 $ input ->getOption ('invokable ' ) ?
@@ -133,8 +126,8 @@ private function generateInvokableCommand(string $commandName, ClassNameDetails
133126 }
134127
135128 $ description = $ io ->ask ('What is the command description? ' );
136- if (false === is_string ($ description )) {
137- $ description = (string )$ description ;
129+ if (false === \ is_string ($ description )) {
130+ $ description = (string ) $ description ;
138131 }
139132
140133 $ arguments = $ this ->askForArguments ($ io );
@@ -169,8 +162,9 @@ private function generateInvokableCommand(string $commandName, ClassNameDetails
169162 }
170163
171164 /**
172- * @param array<int, array{name: string, type: string, description: string|null, default: mixed, nullable: bool}> $arguments
165+ * @param array<int, array{name: string, type: string, description: string|null, default: mixed, nullable: bool}> $arguments
173166 * @param array<int, array{name: string, shortcut: string|null, type: string, description: string|null, default: mixed}> $options
167+ *
174168 * @return array<int, array{name: string, type: string, description: string|null, default: mixed, nullable?: bool, shortcut?: string|null, param_type: string}>
175169 */
176170 private function mergeAndSortParameters (array $ arguments , array $ options ): array
@@ -200,6 +194,7 @@ private function mergeAndSortParameters(array $arguments, array $options): array
200194 return $ parameters ;
201195 }
202196
197+ /** @param array{default: mixed, param_type: string, nullable?: bool} $param */
203198 private function parameterHasDefault (array $ param ): bool
204199 {
205200 if ('argument ' === $ param ['param_type ' ]) {
@@ -216,6 +211,8 @@ private function parameterHasDefault(array $param): bool
216211 */
217212 private function askForArguments (ConsoleStyle $ io ): array
218213 {
214+ $ io ->writeln ('Now, let \'s add some arguments. ' );
215+
219216 $ arguments = [];
220217 $ isFirst = true ;
221218
@@ -236,7 +233,7 @@ private function askForArguments(ConsoleStyle $io): array
236233
237234 foreach ($ arguments as $ arg ) {
238235 if ($ arg ['name ' ] === $ name ) {
239- throw new \InvalidArgumentException (sprintf ('The "%s" argument already exists. ' , $ name ));
236+ throw new \InvalidArgumentException (\ sprintf ('The "%s" argument already exists. ' , $ name ));
240237 }
241238 }
242239
@@ -255,41 +252,12 @@ private function askForArguments(ConsoleStyle $io): array
255252 'string '
256253 );
257254
258- $ nullable = $ io ->confirm ('Is this argument nullable? ' , false );
259-
260- $ description = $ io ->ask ('What is the argument description? ' , null );
261- if (!is_string ($ description ) && null !== $ description ) {
262- $ description = (string )$ description ;
263- }
264-
265- $ hasDefault = $ io ->confirm ('Does this argument have a default value? ' , false );
266- $ default = null ;
267- if ($ hasDefault ) {
268- if ('bool ' === $ type ) {
269- $ default = $ io ->confirm ('What is the default value? ' , false );
270- } elseif ('int ' === $ type ) {
271- $ default = (int )$ io ->ask ('What is the default value? ' , '0 ' );
272- } elseif ('float ' === $ type ) {
273- $ default = (float )$ io ->ask ('What is the default value? ' , '0.0 ' );
274- } elseif ('array ' === $ type ) {
275- $ defaultValue = $ io ->ask ('What is the default value? ' , '[] ' );
276- $ default = '[] ' === $ defaultValue ? [] : $ defaultValue ;
277- } else {
278- $ default = $ io ->ask ('What is the default value? ' , '' );
279- if (!is_string ($ default )) {
280- $ default = (string )$ default ;
281- }
282- }
283- } elseif ($ nullable ) {
284- $ default = null ;
285- }
286-
287255 $ arguments [] = [
288256 'name ' => $ name ,
289257 'type ' => $ type ,
290- 'description ' => $ description ,
291- 'default ' => $ default ,
292- 'nullable ' => $ nullable ,
258+ 'description ' => $ this -> askForParameterDescription ( $ io ) ,
259+ 'default ' => $ this -> askForDefaults ( $ io , $ type ) ,
260+ 'nullable ' => $ io -> confirm ( ' Is this argument nullable? ' , false ) ,
293261 ];
294262 }
295263
@@ -301,6 +269,8 @@ private function askForArguments(ConsoleStyle $io): array
301269 */
302270 private function askForOptions (ConsoleStyle $ io ): array
303271 {
272+ $ io ->writeln ('Now, let \'s add some options. ' );
273+
304274 $ options = [];
305275 $ isFirst = true ;
306276
@@ -321,7 +291,7 @@ private function askForOptions(ConsoleStyle $io): array
321291
322292 foreach ($ options as $ opt ) {
323293 if ($ opt ['name ' ] === $ name ) {
324- throw new \InvalidArgumentException (sprintf ('The "%s" option already exists. ' , $ name ));
294+ throw new \InvalidArgumentException (\ sprintf ('The "%s" option already exists. ' , $ name ));
325295 }
326296 }
327297
@@ -335,8 +305,8 @@ private function askForOptions(ConsoleStyle $io): array
335305 $ isFirst = false ;
336306
337307 $ shortcut = $ io ->ask ('What is the option shortcut? ' , null );
338- if (!is_string ($ shortcut ) && null !== $ shortcut ) {
339- $ shortcut = (string )$ shortcut ;
308+ if (!\ is_string ($ shortcut ) && null !== $ shortcut ) {
309+ $ shortcut = (string ) $ shortcut ;
340310 }
341311
342312 $ type = $ io ->choice (
@@ -345,40 +315,60 @@ private function askForOptions(ConsoleStyle $io): array
345315 'bool '
346316 );
347317
348- $ description = $ io ->ask ('What is the option description? ' , null );
349- if (!is_string ($ description ) && null !== $ description ) {
350- $ description = (string )$ description ;
351- }
352-
353- $ default = null ;
354- if ('bool ' === $ type ) {
355- $ default = $ io ->confirm ('What is the default value? ' , false );
356- } elseif ('int ' === $ type ) {
357- $ default = (int )$ io ->ask ('What is the default value? ' , '0 ' );
358- } elseif ('float ' === $ type ) {
359- $ default = (float )$ io ->ask ('What is the default value? ' , '0.0 ' );
360- } elseif ('array ' === $ type ) {
361- $ defaultValue = $ io ->ask ('What is the default value? ' , '[] ' );
362- $ default = '[] ' === $ defaultValue ? [] : $ defaultValue ;
363- } else {
364- $ default = $ io ->ask ('What is the default value? ' , '' );
365- if (!is_string ($ default )) {
366- $ default = (string )$ default ;
367- }
368- }
369-
370318 $ options [] = [
371319 'name ' => $ name ,
372320 'shortcut ' => $ shortcut ,
373321 'type ' => $ type ,
374- 'description ' => $ description ,
375- 'default ' => $ default ,
322+ 'description ' => $ this -> askForParameterDescription ( $ io ) ,
323+ 'default ' => $ this -> askForDefaults ( $ io , $ type ) ,
376324 ];
377325 }
378326
379327 return $ options ;
380328 }
381329
330+ private function askForParameterDescription (ConsoleStyle $ io ): ?string
331+ {
332+ $ description = $ io ->ask ('What is the description? ' , null );
333+ if (null !== $ description && !\is_string ($ description )) {
334+ $ description = (string ) $ description ;
335+ }
336+
337+ if (false === \is_scalar ($ description )) {
338+ $ description = null ;
339+ }
340+
341+ return $ description ;
342+ }
343+
344+ private function askForDefaults (ConsoleStyle $ io , string $ type ): mixed
345+ {
346+ $ hasDefault = $ io ->confirm ('Does it have a default value? ' , false );
347+
348+ if (false === $ hasDefault ) {
349+ return null ;
350+ }
351+
352+ if ('bool ' === $ type ) {
353+ return $ io ->confirm ('What is the default value? ' , false );
354+ } elseif ('int ' === $ type ) {
355+ return (int ) $ io ->ask ('What is the default value? ' , '0 ' );
356+ } elseif ('float ' === $ type ) {
357+ return (float ) $ io ->ask ('What is the default value? ' , '0.0 ' );
358+ } elseif ('array ' === $ type ) {
359+ $ defaultValue = $ io ->ask ('What is the default value? ' , '[] ' );
360+
361+ return '[] ' === $ defaultValue ? [] : $ defaultValue ;
362+ } else {
363+ $ default = $ io ->ask ('What is the default value? ' , '' );
364+ if (true === \is_scalar ($ default )) {
365+ return (string ) $ default ;
366+ }
367+
368+ return null ;
369+ }
370+ }
371+
382372 public function configureDependencies (DependencyBuilder $ dependencies ): void
383373 {
384374 $ dependencies ->addClassDependency (
0 commit comments