PHPCSStandards / composer-installer

Composer installer for PHP_CodeSniffer coding standards
https://packagist.org/packages/dealerdirect/phpcodesniffer-composer-installer
MIT License
549 stars 36 forks source link

Feature Request: auto add phpcs location to PATH #135

Closed mbomb007 closed 3 years ago

mbomb007 commented 3 years ago

Problem/Motivation

I think it would be great if this package would also automatically add phpcs to the PATH (e.g. vendor/bin) if it isn't already.

jrfnl commented 3 years ago

@mbomb007 Could you explain your usecase a little more ? I just want to make sure I understand what you are suggesting.

If I read the issue now, my understanding would be that you'd like this Composer plugin to add the phpcs command - wherever it is installed (Composer project local or global) to the operating system PATH.

While for a global install I can sort of understand the request (though don't support it), for project local installs this will cause havoc.

Let me illustrate what I mean. Imagine a developer who regularly works on three different projects:

Now, if this plugin would register the paths to the vendor/bin of all three projects to the system PATH, the order in which the developer ran the composer install commands for each project will determine the order of the paths registered in the system PATH.

For the purpose of this example, let's say the developer has run the composer install for these projects in the order in which I listed them. Also for the purpose of this example, let's presume the plugin would append the paths, not prepend.

In the system path you would now have ...;ProjectA/vendor/bin;ProjectB/vendor/bin;ProjectC/vendor/bin.

When the developer would subsequently be working on one of the projects and would run phpcs, the operating system will run through all the folders in the path in the order they are registered until it finds the phpcs command in one of the folders. In this case, the first folder in which it will find the phpcs command would be ProjectA/vendor/bin.

Now if the developer happens to be working on Project A, all is good and will work as expected.

However, if the developer is working on Project B or C, the phpcs command in ProjectA/vendor/bin would still be called and as project A did not install PHPCSExtra, PHPCompatibility or WordPressCS, the phpcs command will fail on a Error: referencing an unknown sniff for any reference to any of these standards.

In other words, for project local installs this is really not a viable feature request.

As for a Composer global install, I still don't think this is a good idea as it will not only make the phpcs command available globally, but any commands which may have come with projects installed via composer global install.

This should be a deliberate choice for an individual developer about their system set-up, not something a plugin which is very specific to one development tool (PHPCS) should do automatically.

There are installers available for Composer, which will register the Composer global vendor/bin folder to the system PATH. Alternatively, there are plenty of tutorials available how to adjust the system PATH manually. I suggest you have a look at those options instead.

As an aside: Personally I would not want to support any such feature anyway as the amount of permutations which would need to be taken into account for all the different operating systems, including permission issues for adjusting the system PATH are huge and ever changing. And that's not even taking into account the potential security risks which a Composer plugin changing the system PATH unbeknowns to the developer running a composer install would create.

mbomb007 commented 3 years ago

Yeah, sounds reasonable.