Skip to content

Style Guidelines

Tom Udding edited this page Apr 27, 2023 · 9 revisions

To make sure the code stays clean and not everyone does things their own way, we have decided to setup style guidelines regarding conventions and standards when contributing to our codebase.

PHP Standards Recommendations (PSR)

PSRs are the de facto standard when it comes to programming in PHP. We follow PSR-1 and PSR-12 with some additional MUSTs.

Argument/Parameter Lists (trailing_comma_in_multiline)

When argument/parameter lists are split across multiple lines they MUST have a trailing comma. This applies to function/method calls and declarations:

<?php

namespace Vendor\Package;

class ClassName
{
    public function aVeryLongMethodName(
        ClassTypeHint $arg1,
        &$arg2,
        array $arg3 = [],
    ): void {
        $arg1->anotherLongMethodName(
            $arg3,
            'value',
        );
    }
}

And the following would not be allowed:

<?php

namespace Vendor\Package;

class ClassName
{
    public function aVeryLongMethodName(
        ClassTypeHint $arg1,
        &$arg2,
        array $arg3 = []
    ): void {
        $arg1->anotherLongMethodName(
            $arg3,
            'value'
        );
    }
}

When using the #[SensitiveParameter] attribute, it MUST be on a separate line:

<?php

namespace Vendor\Package;

class ClassName
{
    public function sensitiveMethod(
        #[SensitiveParameter]
        string $someParameter,
        #[SensitiveParameter]
        object $object,
    ): void {
<?php

namespace Vendor\Package;

class ClassName
{
    public function sensitiveMethod(
        #[SensitiveParameter]
        string $parameter,
    ): bool {

Named Arguments

When using named arguments for a function call, arrays MUST not be expanded. This preserves readability:

$this->acl->allow(
    roles: 'guest',
    resources: 'company',
    privileges: ['viewBanner', 'viewFeatured'],
);
#[OneToOne(
    targetEntity: CompanyLocalisedText::class,
    cascade: ['persist', 'remove'],
    orphanRemoval: true,
)]
#[JoinColumn(
    name: 'description_id',
    referencedColumnName: 'id',
    nullable: false,
)]
protected CompanyLocalisedText $description;

Control Structures

Conditions in control structures MUST be written in Yoda style:

if (null === $value) {

And the following would not be allowed:

if ($value === null) {

Operators

Identical comparison MUST be used, unless type juggling is necessary:

if (null === $value) {

And the following would not be allowed:

if (null == $value) {