PHP 8.6 vyšlo. DressCode na něj váš kód přepíše. Přechod na novější PHP →

Importy

Blok use na začátku souboru se čte snadno, jen když má řád. Sekce imports rozhoduje, jak ten řád vypadá: jestli má každé jméno vlastní příkaz, nebo jich víc sdílí jeden, kdy se píše skupinový import use App\{A, B}, jak se importy řadí a co se stane s importem, který nic nepoužívá.

Které jméno se vůbec importuje a které se píše s lomítkem, rozhoduje jiná sekce, qualification. Klíče skupiny grouping říkají pro třídy, funkce a konstanty, jestli má každé jméno svůj use (separated), všechna jména jednoho druhu jeden společný (combined), nebo jména jednoho jmenného prostoru skupinový import (grouped). Seznam, třeba [separated, grouped], nechá projít každý ze svých tvarů a podle prvního přepíše jen příkaz, který neodpovídá žádnému: skupinový import zůstane a use A, B; se rozdělí. Pořadí druhů určuje order.kinds, řazení jmen order.names a prázdné řádky mezi importy a za nimi sekce blankLines.

imports:
	grouping:
		class: separated
		function: combined
	order:
		kinds: [class, function, constant]
		names: alphabetical
	unused: forbidden
	fromCurrentNamespace: forbidden
namespace App\Model;

use App\Model\Order;  // Useless import of `Order`, because it is in the current namespace.
use App\Util\Clock, App\Util\Money;  // Expected one import per `use` statement, 2 found.
use function count;
use function strlen;  // Expected all imports of functions in one `use` statement. // The import of `strlen()` is unused.
use function trim;  // Expected all imports of functions in one `use` statement.

class Cart
{
	public function total(Order $order, Clock $clock): Money
	{
		return new Money(count($order->items), trim($order->note));
	}
}
namespace App\Model;

use App\Util\Clock;
use App\Util\Money;
use function count, trim;

class Cart
{
	public function total(Order $order, Clock $clock): Money
	{
		return new Money(count($order->items), trim($order->note));
	}
}

Jméno zmíněné jen v dokumentačním komentáři (@var Address[]) se počítá jako použité, protože ho čte PHPStan i editor.

imports.grouping.class

How the imports of classes are put into use statements, a name of the global namespace never grouped.

  • separated: a use of its own for every name, use Foo; use Bar;
  • combined: all names of the kind in one use, use Foo, Bar;, one per namespace declaration
  • grouped: the names of one namespace in one group use, use Acme\Shop\{Order, Cart};, from imports.groupMinNames of them on
  • seznam těchto slov, kde projde každé a kód, který neodpovídá žádnému, se napíše podle prvního
  • keep: nic se nevynucuje, kód zůstane, jak je

Výchozí keep · standardy: perCs keep, psr12 keep, nette [separated, grouped], symfony separated, laravel separated · pokrývá group_import, single_import_per_statement, PSR2.Namespaces.UseDeclaration, SlevomatCodingStandard.Namespaces.DisallowGroupUse, SlevomatCodingStandard.Namespaces.MultipleUsesPerLine

Seskupování se vyplatí nejen kvůli vzhledu. Soubor, jehož importy zaberou tři řádky místo třiceti, má méně tokenů. AI agent, který čte začátek souboru, se tak dřív dostane ke kódu, na kterém záleží, a nemusí se nejdřív prokousat dlouhým blokem příkazů use. Krátký blok udrží grouped u tříd a combined u funkcí a konstant.

imports.grouping.function

How the imports of functions are put into use statements, a name of the global namespace never grouped.

  • separated: a use of its own for every name, use Foo; use Bar;
  • combined: all names of the kind in one use, use Foo, Bar;, one per namespace declaration
  • grouped: the names of one namespace in one group use, use Acme\Shop\{Order, Cart};, from imports.groupMinNames of them on
  • seznam těchto slov, kde projde každé a kód, který neodpovídá žádnému, se napíše podle prvního
  • keep: nic se nevynucuje, kód zůstane, jak je

Výchozí keep · standardy: perCs keep, psr12 keep, nette [combined, grouped], symfony separated, laravel separated · pokrývá group_import, single_import_per_statement, PSR2.Namespaces.UseDeclaration, SlevomatCodingStandard.Namespaces.DisallowGroupUse, SlevomatCodingStandard.Namespaces.MultipleUsesPerLine

imports.grouping.constant

How the imports of constants are put into use statements, a name of the global namespace never grouped.

  • separated: a use of its own for every name, use Foo; use Bar;
  • combined: all names of the kind in one use, use Foo, Bar;, one per namespace declaration
  • grouped: the names of one namespace in one group use, use Acme\Shop\{Order, Cart};, from imports.groupMinNames of them on
  • seznam těchto slov, kde projde každé a kód, který neodpovídá žádnému, se napíše podle prvního
  • keep: nic se nevynucuje, kód zůstane, jak je

Výchozí keep · standardy: perCs keep, psr12 keep, nette [combined, grouped], symfony separated, laravel separated · pokrývá group_import, single_import_per_statement, PSR2.Namespaces.UseDeclaration, SlevomatCodingStandard.Namespaces.DisallowGroupUse, SlevomatCodingStandard.Namespaces.MultipleUsesPerLine

imports.groupMinNames

How many names of one namespace make a group use where grouped is written, fewer written in the next shape of the list, as imports of their own where it names none.

  • počet od 2

Výchozí 2 · standardy: perCs 2, psr12 2, nette 2, symfony 2, laravel 2

imports.order.kinds

The order of the kinds among consecutive imports, a kind not listed following in the order classes, functions, constants.

  • seznam v pořadí, ve kterém mají stát, z těchto slov:
    • class: the imports of classes
    • function: the imports of functions
    • constant: the imports of constants
  • keep: nic se nevynucuje, kód zůstane, jak je

Výchozí keep · standardy: perCs [class, function, constant], psr12 [class, function, constant], nette [class, function, constant], symfony [class, function, constant], laravel [constant, class, function] · pokrývá ordered_imports, SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses

imports.order.names

The order of the names of one kind among consecutive imports, the names of a statement poured back into statements of the shapes written.

  • alphabetical: the names of a kind sorted alphabetically
  • keep: nic se nevynucuje, kód zůstane, jak je

Výchozí keep · standardy: perCs keep, psr12 keep, nette alphabetical, symfony alphabetical, laravel alphabetical · pokrývá ordered_imports, SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses

imports.order.caseSensitive

Whether Acme sorts before acme rather than with it.

  • yes, nebo no

Výchozí no · standardy: perCs no, psr12 no, nette no, symfony no, laravel no · pokrývá ordered_imports, SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses

imports.leadingBackslash

The leading backslash of an import, which changes nothing, use \Foo\Bar;.

  • forbidden: never there
  • keep: nic se nevynucuje, kód zůstane, jak je

Výchozí keep · standardy: perCs forbidden, psr12 forbidden, nette forbidden, symfony forbidden, laravel forbidden · pokrývá PhpCsFixerCustomFixers/no_leading_slash_in_global_namespace, no_leading_import_slash, PSR12.Files.ImportStatement, SlevomatCodingStandard.Namespaces.UseDoesNotStartWithBackslash

imports.unused

An import nothing uses is removed, a class name in a doc comment using the import too.

  • forbidden: never there
  • keep: nic se nevynucuje, kód zůstane, jak je

Výchozí keep · standardy: perCs keep, psr12 keep, nette forbidden, symfony forbidden, laravel forbidden · pokrývá no_unused_imports, SlevomatCodingStandard.Namespaces.UnusedUses

Z importu s několika jmény (use A, B;) a ze skupinového importu (use App\{A, B};) se odstraní jen nepoužitá položka.

imports:
	unused: forbidden
namespace App;

use App\Model\User;
use App\Model\Order;  // The import of `Order` is unused.
use App\Model\Address;

class Profile
{
	/** @var Address[] */
	private array $addresses = [];

	public function __construct(
		private User $user,
	) {
	}
}
namespace App;

use App\Model\User;
use App\Model\Address;

class Profile
{
	/** @var Address[] */
	private array $addresses = [];

	public function __construct(
		private User $user,
	) {
	}
}

Address zůstal, protože se vyskytuje v anotaci @var; kdyby import zmizel, PHPStan by přestal typu rozumět.

imports.fromCurrentNamespace

An import of a class of the namespace it stands in, use Acme\Shop\Order; inside namespace Acme\Shop, and in the global namespace a function or a constant too.

  • forbidden: never there
  • keep: nic se nevynucuje, kód zůstane, jak je

Výchozí keep · standardy: perCs keep, psr12 keep, nette forbidden, symfony keep, laravel keep · pokrývá SlevomatCodingStandard.Namespaces.UseFromSameNamespace

imports.aliasEqualToName

use Foo as Foo; is use Foo;.

  • forbidden: never there
  • keep: nic se nevynucuje, kód zůstane, jak je

Výchozí keep · standardy: perCs keep, psr12 keep, nette forbidden, symfony forbidden, laravel forbidden · pokrývá no_unneeded_import_alias, SlevomatCodingStandard.Namespaces.UselessAlias

Zobrazit na GitHubu · Nahlásit problém se stránkou na GitHubu · 7. 11. 2026