Method and Function Arguments

Verze:

18. 06. 2025

Zodpovědná osoba:

Dominik Šlechta

Poslední aktualizace:

30. 07. 2025, dominikslechta.1@gmail.com

No Space Before Each Comma

  • Rule: In the argument list, MUST NOT be a space before each comma.

  • Reason: This maintains clean and consistent formatting for argument lists.

Example (Correct):

function myMethod($a, $b) {
    // Method body
}

Example (Incorrect):

function myMethod($a , $b) {
    // Method body
}

One Space After Each Comma

  • Rule: There MUST be one space after each comma in the argument list.

  • Reason: This improves readability and aligns with PHP coding conventions.

Example (Correct):

function myMethod($a, $b) {
    // Method body
}

Example (Incorrect):

function myMethod($a,$b) {
    // Method body
}

Arguments with Default Values at the End

  • Rule: Method and function arguments with default values MUST go at the end of the argument list.

  • Reason: This ensures that arguments without default values are specified first, which prevents any ambiguity when calling the method or function.

Example (Correct):

function myMethod($a, $b = 10) {
    // Method body
}

Example (Incorrect):

function myMethod($a = 10, $b) {
    // Method body
}

Split Argument Lists Across Multiple Lines

  • Rule: Argument lists MAY be split across multiple lines, where each subsequent line is indented once. When doing so, the first item in the list MUST be on the next line, and there MUST be only one argument per line.

  • Reason: Splitting argument lists improves readability, especially for functions or methods with many arguments.

Example: 

function myMethod(
    $a,
    $b,
    $c
) {
    // Method body
}

Closing Parenthesis and Opening Brace on the Same Line

  • Rule: When the argument list is split across multiple lines, the closing parenthesis and opening brace MUST be placed together on their own line with one space between them.

  • Reason: This improves clarity by maintaining a consistent closing for the argument list and method body.

Example (Correct):

function myMethod(
    $a,
    $b
) {
    // Method body
}

Example (Incorrect):

function myMethod(
    $a,
    $b
){
    // Method body
}

Space After Return Type Declaration

  • Rule: When you have a return type declaration present, there MUST be one space after the colon followed by the type declaration. The colon and declaration MUST be on the same line as the argument list closing parenthesis, with no spaces between the two characters.

  • Reason: Consistent formatting ensures that return types are easy to distinguish and read.

Example (Correct):

function myMethod($a): string {
    // Method body
}

Example (Incorrect):

function myMethod($a)
 :string {
    // Method body
}

No Space in Nullable Type Declarations

  • Rule: In nullable type declarations, there MUST NOT be a space between the question mark (?) and the type.

  • Reason: This is the correct syntax for nullable types in PHP, and it ensures the proper interpretation of the type.

Example (Correct):

function myMethod(?string $a) {
    // Method body
}

Example (Incorrect):

function myMethod(? string $a) {
    // Method body
}

No Space After Reference Operator &

  • Rule: When using the reference operator & before an argument, there MUST NOT be a space after it.

  • Reason: This ensures correct and consistent syntax when passing arguments by reference.

Example (Correct):

function myMethod(&$a) {
    // Method body
}

Example (Incorrect):

function myMethod( & $a) {
    // Method body
}

No Space Between the Variadic Dot Operator and Argument Name

  • Rule: There MUST NOT be a space between the variadic three dot operator (...) and the argument name.

  • Reason: This is the correct syntax for variadic functions in PHP, ensuring the proper use of the operator.

Example (Correct):

function myMethod(...$args) {
    // Method body
}

Example (Incorrect):

function myMethod( ...$args) {
    // Method body
}

No Space Between Reference Operator and Variadic Dot Operator

  • Rule: When combining both the reference operator & and the variadic three dot operator ..., there MUST NOT be any space between the two of them.

  • Reason: This is the correct syntax for passing arguments by reference in a variadic function.

Example (Correct):

function myMethod(&...$args) {
    // Method body
}

Example (Incorrect):

function myMethod( & ...$args) {
    // Method body
}

Summary:

  • No space before each comma in the argument list, and one space after each comma.

  • Arguments with default values must go at the end of the argument list.

  • Argument lists MAY be split across multiple lines, with each subsequent line indented once, and only one argument per line.

  • The closing parenthesis and opening brace must be placed together on their own line with one space between them.

  • Return type declarations must have one space after the colon.

  • Nullable types must have no space between the question mark and the type.

  • No space after the reference operator & before an argument.

  • No space between the variadic ... operator and the argument name.

  • When combining reference operator & and variadic ... operator, no space should be between them.