maker-bundle icon indicating copy to clipboard operation
maker-bundle copied to clipboard

Inconsistent controller name validation in make:controller command

Open dsentker opened this issue 4 years ago • 2 comments

Symfony version(s) affected: 5.*

Description The make:controller command is able to receive the Controller Name as an argument. If not provided, the command asks for it interactively. However, if the Developer wants to place the controller in a subdirectory / sub-namespace (e.g. App/Controller/Foo/BarController) there seems to be an inconsistency with the escaping for the PHP Namespace Separator.

  • If the Controller name is provided as an command argument, the double-Backslash is the best way to go.
  • If the controller name is provided as an command argument, a single Backslash will ignore the Namespace separator and concatenate the two words
  • If the argument is not provided (provided as answer ) the double--Backslash will throw an error:

[ERROR] "App\Controller\Namespaced2\Controller" 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)

  • If the argument is not provided and the developer enters a single-Backslash as namespace separator, the Namespace is correct.

How to reproduce

daniel@daniel-tux:/srv/www/test$ bin/console make:controller Namespaced1\\Controller

 created: src/Controller/Namespaced1/Controller.php
 created: templates/namespaced1//index.html.twig

           
  Success! 
           

 Next: Open your new controller class and add some pages!
daniel@daniel-tux:/srv/www/test$ bin/console make:controller

 Choose a name for your controller class (e.g. DeliciousPuppyController):
 > Namespaced2\\Controller


                                                                                                                        
 [ERROR] "App\Controller\Namespaced2\\Controller" 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)                                        
                                                                                                                        

daniel@daniel-tux:/srv/www/test$ bin/console make:controller

 Choose a name for your controller class (e.g. GrumpyGnomeController):
 > Namespaced3\Controller

 created: src/Controller/Namespaced3/Controller.php
 created: templates/namespaced3//index.html.twig

           
  Success! 
           

 Next: Open your new controller class and add some pages!

Possible Solution

  • Maybe the validation for the controller name is different when provided as an argument. This should be fixed
  • Additionally, the error is not very helpful as it does not provides a helpful message.

dsentker avatar Jun 21 '21 11:06 dsentker

Would you be able to send a PR to have a look at the code and send a PR?

nicolas-grekas avatar Jun 21 '21 12:06 nicolas-grekas

Yes, i would like to create my first PR (ever). I already found the reason for that behaviour: A double-Backslash is sometimes interpreted as \\, sometimes as \. I created a POC fix and could submit a PR. Can you tell which branch i should target for the PR?

dsentker avatar Jun 21 '21 15:06 dsentker