Easiest way to detect/remove unused `use` statements from PHP codebase

前端 未结 3 1494
有刺的猬
有刺的猬 2021-02-13 05:47

I\'ve searched all over for something like this but I believe the word \"use\" is perhaps too common for any helpful results:

What is the easiest way to remove all un-us

3条回答
  •  遥遥无期
    2021-02-13 06:50

    It would probably depend on the way your code is set up. If your code uses namespaces like so:

    namespace Foo
    {
       
    }
    

    then you're probably fine if you just check each file individually. That still means you would have to parse the PHP code to find the use statements, and then to determine which statements are used.

    The easy way is to use a tool that's already been built. I recently started using PhpStorm IDE (30 day free trail, or the early access program) and that can inform you when you have unused use statements in a file (and you can even specify whether that should come up as warnings or errors). It would still require you to open each file though. But you could also check files you are editing, then eventually your code will be cleaner.

提交回复
热议问题