PhpStorm 2019.1 Help

Auto import

When you reference a PHP class that is defined outside the current file, PhpStorm locates the class definition and lets you do one of the following:

  • Automatically complete the fully qualified class name, including the namespace the class is defined in.

  • Automatically complete the short class name and import the namespace the class is defined in.

  • Import the namespace manually using a quick-fix.

The use statement is added to the imports section, but the caret does not move from the current position, and your current editing session does not suspend. This feature is known as the Import Assistant.

In JavaScript and TypeScript files, PhpStorm automatically adds import statements for modules, classes, components, and any other symbols that can be exported, as well as for XML namespaces. Learn more from Auto import in JavaScript, Auto import in TypeScript and Importing an XML namespace.

Automatically add import statements

You can configure the IDE to automatically add import statements if there are no options to choose from.

  1. In the Settings/Preferences dialog (Ctrl+Alt+S), click Editor | General | Auto Import.

  2. In the PHP section, configure automatic namespace import.

    • To have PhpStorm automatically add use statements for classes and methods in pasted blocks of code, choose the desired behavior from the Insert imports on paste list:

      • All: import statements will be added automatically for all missing classes and methods found in pasted blocks of code.

      • Ask: PhpStorm will prompt you to select which classes and methods you want to import.

        If the pasted class is already referenced in the target code via an alias, PhpStorm will prompt you to reuse this alias instead of creating a new import statement.

        the Select Classes To Import dialog: reuse existing alias
      • None: no import statements will be added, you won't be asked about unresolved references.

      Note that adding imports on paste is only possible if the copied element is properly resolved in code (that is, not highlighted by the Undefined class or Undefined method inspections), and project indexing is finished.

    • To have automatic namespace import applied when you are typing in a file that does not belong to any specific namespace, select the Enable auto-import in file scope checkbox.

    • To have PhpStorm automatically import PHP namespaces, add use statements, and complete short class names on the fly when you are typing in a class or file that belongs to a certain namespace, select the Enable auto-import in namespace scope checkbox. This checkbox is selected by default.

    • If necessary, configure auto-import from the global namespace separately for classes, functions, and constants.

      • Prefer FQN: If selected, PhpStorm automatically inserts the fully-qualified name of a symbol from the global namespace, prepended with a backslash, for example:

        namespace A; $myException = new \Exception(); $a = \strlen("Test"); echo \PHP_EOL;
      • Prefer Import: If selected, PhpStorm automatically adds use statements for symbols from the global namespace if this doesn’t result in a conflict, for example:

        namespace A; use Exception; use const PHP_EOL; use function strlen; $myException = new Exception(); $a = strlen("Test"); echo PHP_EOL;
      • Prefer Fallback: If selected, PhpStorm neither inserts a fully-qualified name of a function or a constant, nor imports them by means of the use statement. The fallback global functions or constants are preferred in this case, for example:

        namespace A; use Exception; $myException = new Exception(); $a = strlen("Test"); echo PHP_EOL;

Import a PHP namespace on-the-fly

  1. Enable on-the-fly namespace import.

  2. Open the desired file for editing and start typing the short name of a class.

  3. From the code completion suggestion list, select the desired class name. PhpStorm will complete the short class name and insert the use statement with the namespace where the selected class is defined.

Import a class by using a quick fix

  1. Open a file for editing and reference a PHP class. If the referenced class is not bound, PhpStorm will highlight it:

    ps_undefined_class.png
  2. Press Alt+Enter and accept the suggestion to import the namespace where the declaration of the class is detected.

    PhpStorm inserts a namespace declaration statement (use statement).

Shorten fully qualified class names with Code Cleanup

PhpStorm provides the following inspections and quick-fixes for shortening fully qualified class names:

  • Fully qualified name usage inspection highlights the fully qualified class names that can be removed by adding a use statement.

  • Unnecessary fully qualified name inspection highlights the fully qualified class names that can be removed without adding a use statement.

You can apply the corresponding quick-fixes to a given scope automatically by using Code Cleanup.

Clean up code on a given scope

  1. From the main menu, select Code | Code Cleanup.

  2. In the Specify Code Cleanup Scope dialog that opens, select ths scope to which you want the inspection profile to be applied.

  3. Select the inspection profile from the list, or click the Browse button to configure a new profile in the Code Cleanup Inspections dialog that opens. You can also click the Browse button to check which fixes will be applied and make sure that the Unnecessary fully qualified name and Fully qualified name usage inspections are enabled.

    the Code Cleanup Inspections dialog: FQN Inspections
  4. Click OK to launch code cleanup.

Clean up code in the current file

  1. In the editor, position the caret at a fully qualified class name highlighted by the Unnecessary fully qualified name or Fully qualified name usage inspection.

  2. Click the Intention action button or press Alt+Enter, and select Cleanup code from the popup menu.

Optimize imports

The Optimize Imports feature helps you remove unused imports, add missing imports, and organize import statements in the current file or in all files in a directory at once.

  1. Select a file or a directory in the Project tool window.

  2. Do any of the following:

    • From the main menu, select Code | Optimize Imports (or press Ctrl+Alt+O).

    • From the context menu, select Optimize Imports.

  3. (If you've selected a directory) Choose whether you want to optimize imports in all files in the directory, or only in locally modified files (if yor project is under version control), and click Run.

When optimizing imports, PhpStorm can automatically sort the use statements either alphabetically or by their length. To choose the preferred option, in the Settings/Preferences dialog (Ctrl+Alt+S), go to Editor | Code Style | PHP and switch to the Code Conversion tab. Then select the Sort use statements checkbox and choose how the use statements should be sorted.

Optimize imports in a single file

  1. Place the caret at the import statement and press Alt+Enter (or use the intention action the Intention action button icon).

  2. Select Remove use statement.

Automatically optimize imports in modified files

If your project is under version control, you can instruct PhpStorm to optimize imports in modified files before committing them to VCS.

  1. From the main menu, select VCS | Commit (or press Ctrl+K).

  2. In the Before commit area of the Commit Changes dialog, select the Optimize imports checkbox.

Last modified: 17 June 2019