Artisan Développeur

Qualité, amélioration continue, agilité.

Add php-cs-fixer support to phpstorm 9

Updated: 2018-07-13 new url for php-cs-fixer-v2

How To add php-cs-fixer from sensio to JetBrain phpStorm 9.

I want to have good code quality, follow best practices and coding standard. Some tools help to achieve that goal.

Php cs fixer is one of them. (Github repository)

You have two big steps :

  • install php cs fixer
  • add it as an phpstorm external tool

1 – Install php-cs-fixer (linux method)

I choose to install it globally and manually (other methods are available, see official website).

Get the tool with wget :

[pastacode lang= »bash » manual= »%24%20wget%20https%3A%2F%2Fcs.sensiolabs.org%2Fdownload%2Fphp-cs-fixer-v2.phar%20-O%20php-cs-fixer%0A » message= »Get php cs fixer » highlight= » » provider= »manual »/]

Add execution permission :

[pastacode lang= »bash » message= » » highlight= » » provider= »manual » manual= »%24%20sudo%20chmod%20a%2Bx%20php-cs-fixer%0A »/]

You have to move it in your bin directory :

[pastacode lang= »bash » message= » » highlight= » » provider= »manual » manual= »%24%20sudo%20mv%20php-cs-fixer%20%2Fusr%2Flocal%2Fbin%2Fphp-cs-fixer »/]

That’s it for this part, you have php-cs-fixer installed globally in your system, you can use it simply with :

[pastacode lang= »bash » message= » » highlight= » » provider= »manual » manual= »%24%20php-cs-fixer »/]

 

Second part, add it as external phpstorm9 tool. (This should work as well for phpstorm8)

In the phpStorm menu:
« File->Settings »
MENU_SETTINGS_redim

« Tools ->External tools » and click the + button :
config_tool

You have to fill the field :

  • the name, is the name visible in the menu, when you use your external tool, you need to fill with something clear:
    • php-cs-fixer
  • description, is for you, to remember the parameters
    • in my example, i use --dry-run and --diff to view all corrections, but rot apply them, description can be : dry run of php-cs-fixer to check without make modifications
  • Program field is for the command tool, we install php-cs-fixer globally, here we just need :
    • php-cs-fixer
  • Parameters are the command tool parameters :
    • I use -vv to verbose, fix (self-explained ^^), --dry-run to not apply fixes but display files list, $FilePrompt$ is phpStorm placeholder to ask for a « browse » window to browse where you want to use php-cs-fixer, to finish --diff show you the fixes.

      (In the official doc A combination of --dry-run and --diff will display summary of proposed fixes, leaving your files unchanged.)

       

  • Working directory :
    • $ProjectFileDir$ phpStorm placeholder

You can select « Ok » now you can use php-cs-fixer directly in phpStorm.

I mostly do Symfony2 project, but, sometimes is « just » php, or WordPress, so my first tool is generic, i use it to only check code.
I want to duplicate this external tool, to have some Symfony2 specific configurations.

Best way is to use the copy button, and modify « Parameters » after.

I use two more configurations :

  • Symfony specific with --config=sf2 parameter
  • Same with the --dry-run and --diff to check without making changes

config_tool_copy_redim

config_tool2_redim

Once you have done your settings, you go to the menu, « Tools -> external Tolls » and choose the one you want to use:

external_tool

If you select one with the --config=sf2 parameter, remember that you need to select the root directory of your symfony2 project, because php-cs-fixer will search for the « src » directory.

 

 

 

 

Add default ROLE to user when registering, with FOS User bundle

Add default when registering a new user with FOS user bundle.

I want to add a default ROLE to all my users when they use registration on my web app, but I had some difficulties.

This is my working files : Listener default ROLE user fos user bundle

You have to add a listener to the REGISTRATION_SUCCESS event dispatch by FOS user bundle. You need to create a class, and make it implements « EventSubscriberInterface » .

[pastacode lang= »php » user= »nicolas-san » repos= »https://github.com/nicolas-san/fosUserDefaultRole/ » path_id= »https://github.com/nicolas-san/fosUserDefaultRole/blob/master/MyBundle/EventListener/RegistrationSuccessListener.php » revision= » » highlight= » » lines= » » provider= »github »/]

When you have your class, you have to register it as a service in service.yaml (or xml).

[pastacode lang= »php » user= »nicolas-san » repos= »https://github.com/nicolas-san/fosUserDefaultRole/ » path_id= »https://github.com/nicolas-san/fosUserDefaultRole/blob/master/MyBundle/Ressources/Config/services.yml » revision= » » highlight= » » lines= » » provider= »github »/]

But, before I succeed, I try to add ROLE_USER by default, and no matter what I code, test, write, try, implements, spell, cast… No way to have this default ROLE to my user, database field was empty.

After some search, I found why in FOS User Bundle code :

yourProjectRoot/vendor/friendsofsymfony/user-bundle/Model/UserInterface.php :

[pastacode lang= »php » message= »Fos user bundle default static role » highlight= » » provider= »manual »]

interface UserInterface extends AdvancedUserInterface, \Serializable
{
    const ROLE_DEFAULT = 'ROLE_USER';
    const ROLE_SUPER_ADMIN = 'ROLE_SUPER_ADMIN';

[/pastacode]

In :

yourProjectRoot/vendor/friendsofsymfony/user-bundle/Model/User.php

 

[pastacode lang= »php » message= » » highlight= » » provider= »manual »]

public function addRole($role)
    {
        $role = strtoupper($role);
        if ($role === static::ROLE_DEFAULT) {
            return $this;
        }

        if (!in_array($role, $this->roles, true)) {
            $this->roles[] = $role;
        }

        return $this;
    }

[/pastacode]

(line of code from FOS userBundle repo on github)

If you try to add ROLE_USER, as ROLE_USER is ROLE_DEFAULT, you role is not added to $this->roles[] and not persisted, it is why even with a good working listener, ROLE_USER is never write into user account details in database.

Resources :

 

Twig filter to generate alt attribute of an img tag from filename

Today I want share a little Twig filter.

To add a Twig filter, you need to :

  • Create an extension class
  • Register as a service
  • ???
  • Enjoy

I worked on a project on which I never had the possibility to manage medias.
I had to take images filenames and render them through twig.
For some reasons, like SEO, I want to add the alt attribute of img tag, but no way to manage some text attached to images.
It was like a frontend to big property management system, I can’t alter content, just get and render.

I write this little twig filter, to take keyword from filename.

I PR this filter to twig extension [GITHUB for twig filter PR for alt image], and Fabpot say :

As the alt attribute is nowadays mostly used by screen readers, using a generated text seems like a bad idea. I’m :-1: on adding this.

I really think he make a point, and it is not the best way to do it, but, I can not do otherwise than have auto generated keyword as alt image.

If you want to use it :

So, even if it’s not such good practice, if you need this, and if it’s useful, use it 🙂