Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PSR-12 Potential Features and Details of Approaches

Points that can be made

  • PSR-2 Errata Inclusion ✓
  • Scalar Type Hint Declarations ✓
  • Return Type Declarations ✓
  • Uniform Variable Syntax
  • Anonymous Classes ✓
  • declare() ✓
  • Constant Expressions
  • Grouped use statements ✓
  • finally blocks ✓
  • EOL @ EOF clarification ✓
  • Where do opening braces for functions go? (not methods, not closures) ✓
  • Allow spaces after opening parenthesis if the following character is !
  • Method names SHOULD NOT be prefixed with a single underscore to indicate protected or private visibility. <- Does this mean they cannot be prefixed by a single underscore at all OR that this cannot be a means to indicate visibility.

Points that could be made but could break BC

These points will be surveyed and if they break BC in more than 25% of FIG projects using PSR-2 we will not do them.

  • Operators ✓

Selected Approaches

It will later be put into words in this file, ready for later translation in spec wording.

  • Type hinting should be done in parameters through type hints where possible and when available in minimum required version
  • When available through means of a minimum project requirement, all files should have strict type hinting except in the case of automated tests
  • Declare() for strict types must be on the first line after the first <?php in the file. No preceding comments and not on the same line as <?php unless <?php is not on the first line of the file
  • Allow spaces after opening parenthesis if the following character is !
  • Regarding PSR-2 (2.2), ammendment: All PHP files MUST end with a single containing only a single newline (LF) character
  • Ammendement: Where not regarding class-specific/OOP functionality replace methods with methods & functions
  • Errata as defined in https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide-meta.md#3-errata
  • Finally blocks will be exactly as catch blocks are in try...catch...finally control flow. Replacement expample will be used.
  • Use statements must be grouped by type with an empty line (containing only a single line feed character) between them
  • Use statements should be ordered as such that: classes, functions, constants.
  • Anonymous classes already merged
  • All comparison operators, including the spaceship operator, must be both preceded and suceeded by a space
  • When using the null coalesce operator or the ternary operator, there much be a space before and after each operator allowing for grouping of operators together into a single operator. e.g. (expr1) ? (expr2) : (expr3) or (expr1) ?: (expr2) or $a ?? $b ?? $c
<?php
// Chosen approach is to have finally used exactly as catch is.
try {
// try body
} catch (FirstExceptionType $e) {
// catch body
} catch (OtherExceptionType $e) {
// catch body
} finally {
// finally body
}
// Alternatively
try {
// try body
} catch (FirstExceptionType $e) {
// catch body
} catch (OtherExceptionType $e) {
// catch body
}
finally {
// finally body
}
// Alternatively
try {
// try body
} catch (FirstExceptionType $e) {
// catch body
} catch (OtherExceptionType $e) {
// catch body
}
finally
{
// finally body
}
// Alternatively
try {
// try body
} catch (FirstExceptionType $e) {
// catch body
} catch (OtherExceptionType $e) {
// catch body
} finally
{
// finally body
}
<?php
// Chosen approach is to consider the closing parameter bracket and
// colon look visually as one character so no space between parenthesis and colon
// and have a preceding space before the type declaration. Demonstrated in method
// named `this()`
class ReturnTypeVariations
{
public function this(): string
{
return;
}
public function func() :string
{
return;
}
public function func() : string
{
return;
}
public function func():string
{
return;
}
public function func()
:string{
return;
}
public function func()
:string {
return;
}
public function func()
: string {
return;
}
public function func()
:string
{
return;
}
public function func()
: string
{
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.