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 therekeep: 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 therekeep: 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 therekeep: 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 thereforbidden: never therekeep: 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 thereforbidden: never therekeep: 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 therekeep: 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 therekeep: 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:abstractorfinal, visibility, set visibility,static,readonlykeep: 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 therekeep: 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 functionkeep: 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 methodprotected: a protected method, where no child and no ancestor can declare itpublic: 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 useenumCase: a case of an enumconstant: a constant of any visibilitypublicConstant: a public constantprotectedConstant: a protected constantprivateConstant: a private constantproperty: a property of any visibilitypublicProperty: a public propertyprotectedProperty: a protected propertyprivateProperty: a private propertypublicStaticProperty: a public static propertyprotectedStaticProperty: a protected static propertyprivateStaticProperty: a private static propertymethod: a method of any visibilitypublicMethod: a public methodprotectedMethod: a protected methodprivateMethod: a private methodpublicStaticMethod: a public static methodprotectedStaticMethod: a protected static methodprivateStaticMethod: a private static methodconstructor: the constructordestructor: the destructormagicMethod: 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 originalkeep: 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 originalkeep: 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 originalkeep: 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 therekeep: 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 therekeep: 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 therekeep: 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 therekeep: 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 therekeep: 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 therekeep: 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 Fookeep: 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 classkeep: 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 thererequired: always therekeep: 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