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

Třídy

Sekce classes rozhoduje, jak třída vypadá a co o sobě říká: v jakém pořadí stojí její členové, jestli každý uvádí viditelnost, kolik jich smí sdílet jednu deklaraci, kdy se za new a za atributem píšou prázdné závorky a co se stane s modifikátorem, metodou nebo atributem, které nic nepřidávají.

Některá rozhodnutí se týkají čistě vzhledu, třeba pořadí členů nebo závorky za new Foo. Jiná hlídají, že třída říká, co má: viditelnost u každého člena, final u třídy označené @internal, static u privátní metody bez $this, signaturu přepsané metody shodnou s předkem. Poslední tři mohou změnit chování kódu, a proto jsou jejich opravy rizikové (risky fixes); kdy se provedou, vysvětluje stránka Konfigurace.

classes:
	members:
		order: [traitUse, constant, property]
		grouping: separated
	visibility:
		constant: required
		property: required
		method: required
	emptyParentheses:
		instantiation: required
class Cart
{
	private array $items = [];  // The constant must come before the property.
	const LIMIT = 10, MIN = 1;  // Expected one constant per declaration, 2 found. // The constant `LIMIT` must declare its visibility. // The constant `MIN` must declare its visibility.

	public function add(Item $item): void
	{
		$this->items[] = new Entry;  // Expected `()` after `new Entry`.
	}
}
class Cart
{
	public const LIMIT = 10;
	public const MIN = 1;
	private array $items = [];

	public function add(Item $item): void
	{
		$this->items[] = new Entry();
	}
}

classes.visibility.constant

The visibility of a constant of a class, an interface, a trait or an enum.

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

Výchozí keep · standardy: perCs required, psr12 required, nette required, symfony required, laravel keep · pokrývá modifier_keywords, visibility_required, PSR12.Properties.ConstantVisibility, PSR2.Classes.PropertyDeclaration, SlevomatCodingStandard.Classes.ClassConstantVisibility, Squiz.Scope.MethodScope

classes.visibility.property

The visibility of a property, a promoted one included, var becoming public and a set visibility counting as one.

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

Výchozí keep · standardy: perCs required, psr12 required, nette required, symfony required, laravel required · pokrývá modifier_keywords, visibility_required, PSR12.Properties.ConstantVisibility, PSR2.Classes.PropertyDeclaration, SlevomatCodingStandard.Classes.ClassConstantVisibility, Squiz.Scope.MethodScope

classes.visibility.method

The visibility of a method outside an interface, whose methods take interfaceMethod.

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

Výchozí keep · standardy: perCs required, psr12 required, nette required, symfony required, laravel required · pokrývá modifier_keywords, visibility_required, PSR12.Properties.ConstantVisibility, PSR2.Classes.PropertyDeclaration, SlevomatCodingStandard.Classes.ClassConstantVisibility, Squiz.Scope.MethodScope

classes.visibility.interfaceMethod

The public of a method of an interface, which is public whatever it says.

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

Výchozí keep · standardy: perCs required, psr12 required, nette forbidden, symfony required, laravel required · pokrývá modifier_keywords, visibility_required, Squiz.Scope.MethodScope

classes.visibility.publicWithSet

The public of a property with a set visibility, public protected(set) int $x, which reads the same without it.

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

Výchozí keep · PHP 8.4 · standardy: perCs keep, psr12 required, nette required, symfony required, laravel keep

classes.protectedInEnum

A protected member of an enum, which is private.

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

Výchozí keep · standardy: perCs forbidden, psr12 keep, nette forbidden, symfony forbidden, laravel keep · pokrývá protected_to_private

classes.protectedInFinalClass

A protected member of a final class with no parent and no trait, which is private.

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

Výchozí keep · standardy: perCs keep, psr12 keep, nette keep, symfony forbidden, laravel keep · pokrývá protected_to_private

classes.modifiers.order

The order of the modifiers of a property, a promoted one included, a method, a constant and a class, final readonly class.

  • canonical: abstract or final, visibility, set visibility, static, readonly
  • keep: nic se nevynucuje, kód zůstane, jak je

Výchozí keep · standardy: perCs canonical, psr12 canonical, nette canonical, symfony canonical, laravel canonical · pokrývá modifier_keywords, visibility_required, PSR2.Classes.PropertyDeclaration, PSR2.Methods.MethodDeclaration, SlevomatCodingStandard.Classes.PropertyDeclaration

classes.modifiers.implied

A modifier PHP implies anyway, final in a final class, readonly in a readonly class, protected(set) of a public readonly property.

  • 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á no_redundant_readonly_property, no_unneeded_final_method, Generic.CodeAnalysis.UnnecessaryFinalModifier

classes.staticMethod

The static keyword of a method that does not use $this, of the visibilities classes.staticMethodVisibilities names, its calls $this->method() written self::method(); a magic method and one whose body could reach the object otherwise are left.

  • required: always there, static function
  • keep: nic se nevynucuje, kód zůstane, jak je

Výchozí keep · standardy: perCs keep, psr12 keep, nette keep, symfony keep, laravel keep · pokrývá static_private_method

classes.staticMethodVisibilities

The visibilities of the methods made static.

  • seznam z těchto slov:
    • private: a private method
    • protected: a protected method, where no child and no ancestor can declare it
    • public: a public method, where no child and no ancestor can declare it

Výchozí [private] · standardy: perCs [private], psr12 [private], nette [private], symfony [private], laravel [private]

classes.members.order

The order of the members of a class by kind, a member taking the most particular kind listed (the constructor, the destructor or a magic method, then its visibility with static, its visibility, its kind) and the kinds not listed following in the order written.

  • seznam v pořadí, ve kterém mají stát, z těchto slov:
    • traitUse: a trait use
    • enumCase: a case of an enum
    • constant: a constant of any visibility
    • publicConstant: a public constant
    • protectedConstant: a protected constant
    • privateConstant: a private constant
    • property: a property of any visibility
    • publicProperty: a public property
    • protectedProperty: a protected property
    • privateProperty: a private property
    • publicStaticProperty: a public static property
    • protectedStaticProperty: a protected static property
    • privateStaticProperty: a private static property
    • method: a method of any visibility
    • publicMethod: a public method
    • protectedMethod: a protected method
    • privateMethod: a private method
    • publicStaticMethod: a public static method
    • protectedStaticMethod: a protected static method
    • privateStaticMethod: a private static method
    • constructor: the constructor
    • destructor: the destructor
    • magicMethod: a method whose name begins with __
  • keep: nic se nevynucuje, kód zůstane, jak je

Výchozí keep · standardy: perCs [traitUse], psr12 [traitUse], nette [traitUse, enumCase, publicConstant, protectedConstant, privateConstant, publicProperty, protectedProperty, privateProperty], symfony [traitUse], laravel keep · pokrývá ordered_class_elements, PSR12.Traits.UseDeclaration

Členové, jejichž druh seznam nejmenuje, zůstanou za vyjmenovanými v pořadí, v jakém stojí. Platí vždy nejkonkrétnější druh: s [constant, privateConstant] se privátní konstanta řadí jako privateConstant. Druh, na který vedle jeho konkrétnějších druhů nic nezbude, seznam nepřijme: [constant, publicConstant, protectedConstant, privateConstant] je chyba konfigurace. Nette Coding Standard řadí konstanty a vlastnosti podle viditelnosti a metody nechává tak, jak je autor napsal:

classes:
	members:
		order: [traitUse, enumCase, publicConstant, protectedConstant, privateConstant, publicProperty, protectedProperty, privateProperty]

classes.members.grouping.constant

A declaration of several constants of a class, const A = 1, B = 2;.

  • separated: one member per declaration, each sharing the modifiers and the type of the original
  • keep: nic se nevynucuje, kód zůstane, jak je

Výchozí keep · standardy: perCs separated, psr12 keep, nette keep, symfony separated, laravel separated · pokrývá single_class_element_per_statement, SlevomatCodingStandard.Classes.DisallowMultiConstantDefinition

classes.members.grouping.property

A declaration of several properties, public $a, $b;.

  • separated: one member per declaration, each sharing the modifiers and the type of the original
  • keep: nic se nevynucuje, kód zůstane, jak je

Výchozí keep · standardy: perCs separated, psr12 separated, nette separated, symfony separated, laravel separated · pokrývá single_class_element_per_statement, SlevomatCodingStandard.Classes.DisallowMultiPropertyDefinition

classes.members.grouping.traitUse

A use of several traits, use A, B;.

  • separated: one member per declaration, each sharing the modifiers and the type of the original
  • keep: nic se nevynucuje, kód zůstane, jak je

Výchozí keep · standardy: perCs separated, psr12 separated, nette separated, symfony separated, laravel keep · pokrývá single_trait_insert_per_statement, PSR12.Traits.UseDeclaration, SlevomatCodingStandard.Classes.TraitUseDeclaration

classes.untypedPropertyNullDefault

public $a = null; says what public $a; says.

  • 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 keep · pokrývá no_null_property_initialization

classes.overridingMethod.signature

The return type, the types of the parameters, the parameters added with a default, the visibility and static of a method overriding one of a parent class or an interface, as the ancestor declares them.

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

Výchozí keep · jen s typy · standardy: perCs keep, psr12 keep, nette keep, symfony keep, laravel keep

classes.overridingMethod.parameterName

The names of the parameters of a method overriding one of a parent class or an interface, as the ancestor names them.

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

Výchozí keep · jen s typy · standardy: perCs keep, psr12 keep, nette keep, symfony keep, laravel keep

classes.overridingMethod.parentCallOnly

A method that only forwards to the parent with the same arguments adds nothing.

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

Výchozí keep · jen s typy · standardy: perCs keep, psr12 keep, nette forbidden, symfony keep, laravel keep

classes.overridingMethod.ReturnTypeWillChange

The attribute where the method declares the return type of what it implements.

  • 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

classes.internalClass.final

The final of a class its author marked as internal, which nothing outside the package may extend.

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

Výchozí keep · standardy: perCs keep, psr12 keep, nette keep, symfony keep, laravel keep · pokrývá final_internal_class

classes.internalClass.markers

The annotations, with their @, and the attributes, by their fully qualified classes, of which one marks a class internal.

  • seznam jmen

Výchozí [@internal] · standardy: perCs [@internal], psr12 [@internal], nette [@internal], symfony [@internal], laravel [@internal]

classes.internalClass.except

The annotations and the attributes that keep an internal class as it is, an entity for instance.

  • seznam jmen

Výchozí [@final, @Entity, '@ORM\Entity', '@ORM\Mapping\Entity', '@Mapping\Entity', @Document, '@ODM\Document', 'Doctrine\ORM\Mapping\Entity', 'Doctrine\ODM\MongoDB\Mapping\Annotations\Document'] · standardy: perCs [@final, @Entity, '@ORM\Entity', '@ORM\Mapping\Entity', '@Mapping\Entity', @Document, '@ODM\Document', 'Doctrine\ORM\Mapping\Entity', 'Doctrine\ODM\MongoDB\Mapping\Annotations\Document'], psr12 [@final, @Entity, '@ORM\Entity', '@ORM\Mapping\Entity', '@Mapping\Entity', @Document, '@ODM\Document', 'Doctrine\ORM\Mapping\Entity', 'Doctrine\ODM\MongoDB\Mapping\Annotations\Document'], nette [@final, @Entity, '@ORM\Entity', '@ORM\Mapping\Entity', '@Mapping\Entity', @Document, '@ODM\Document', 'Doctrine\ORM\Mapping\Entity', 'Doctrine\ODM\MongoDB\Mapping\Annotations\Document'], symfony [@final, @Entity, '@ORM\Entity', '@ORM\Mapping\Entity', '@Mapping\Entity', @Document, '@ODM\Document', 'Doctrine\ORM\Mapping\Entity', 'Doctrine\ODM\MongoDB\Mapping\Annotations\Document'], laravel [@final, @Entity, '@ORM\Entity', '@ORM\Mapping\Entity', '@Mapping\Entity', @Document, '@ODM\Document', 'Doctrine\ORM\Mapping\Entity', 'Doctrine\ODM\MongoDB\Mapping\Annotations\Document']

classes.emptyParentheses.instantiation

The empty parentheses of new with a class, new static and new $class alike, which new Foo()->bar() keeps whatever this says, PHP 8.4 needing them there.

  • required: always there, new Foo()
  • forbidden: never there, new Foo
  • keep: nic se nevynucuje, kód zůstane, jak je

Výchozí keep · standardy: perCs required, psr12 required, nette forbidden, symfony required, laravel forbidden · pokrývá new_with_braces, new_with_parentheses, PSR12.Classes.ClassInstantiation, SlevomatCodingStandard.ControlStructures.NewWithParentheses, SlevomatCodingStandard.ControlStructures.NewWithoutParentheses

classes.emptyParentheses.anonymousClass

The empty parentheses of an anonymous class.

  • required: always there, new class()
  • forbidden: never there, new class
  • keep: nic se nevynucuje, kód zůstane, jak je

Výchozí keep · standardy: perCs forbidden, psr12 keep, nette forbidden, symfony forbidden, laravel forbidden · pokrývá new_with_braces, new_with_parentheses

classes.emptyParentheses.attribute

The empty parentheses of an attribute.

  • required: always there, #[Foo()]
  • forbidden: never there, #[Foo]
  • keep: nic se nevynucuje, kód zůstane, jak je

Výchozí keep · standardy: perCs forbidden, psr12 keep, nette forbidden, symfony keep, laravel keep · pokrývá attribute_empty_parentheses

classes.instantiationWrapping

The parentheses around an instantiation a member is accessed on, new Foo()->bar() or (new Foo())->bar(); one without the parentheses of its arguments stays wrapped.

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

Výchozí keep · PHP 8.4 · standardy: perCs forbidden, psr12 keep, nette forbidden, symfony keep, laravel keep · pokrývá new_expression_parentheses

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