If you select a class in your code you want to generate a use statements for, then type Alt-Enter-Enter you will get a menu "Import Class" where you can select the class to include in the use statement.
Can I configure PhpStorm to add use statements automatically for copy and paste code blocks?
-
04-07-2023 - |
Question
When I write code within phpstorm, it will add use statements when I select a class from autocompletion.
So writing $pagerfanta = new Pagerfanta(new DoctrineORMAdapter($query));
will add both use statements:
use Pagerfanta\Pagerfanta;
use Pagerfanta\Adapter\DoctrineORMAdapter;
Yet when I copy-paste or extract the block containing $pagerfanta = new Pagerfanta(new DoctrineORMAdapter($query));
into a new class, the use statements will not get added. PhpStorm shows them as missing by adding a different background color.
Nontheless, I often forget to add the use statements then, and hence am wondering, if I can configure PhpStorm to add those use statements for me when copy-pasting.
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow