Hi there, fellow Neosians,
since the Coding Guidelines have been published, a lot has happened in the PHP world, most prominently a proper type system
As already mentioned in Slack, it might be time for a larger overhaul of our guidelines, and since I’m interested in this topic, I’d like to start the discussion with a basic proposal, based on PHP 8 and later on 8.1:
Base would be PSR-12 PSR-12: Extended Coding Style - PHP-FIG
Doc blocks
SHOULD only be present when adding information.
Comments should not describe what a method does or a variable is, that should be in the name. But they should add valuable information like why a method is implemented and how the result is achieved or what a variable is intended to be used for.
Type annotations should be used when and only when extending PHP’s type system to support static analysis.
Bad:
/**
* @param array $value
* @return string
*/
public function foo(array $value): string;
Good:
/**
* This method foos an array to a string
* to support general usage of the FooInterface in use cases A, B and C
*
* @param array<int,string> $value
*/
public function foo(array $value): string;
Testcase documentation
(PHP Coding Guidelines & Best Practices — Flow Framework 7.3.x documentation)
… should follow newer PHPUnit style:
public function testFooReturnsBarForQuux(): void
{
...
}
Additional to interface names etc:
Command Class Names
Commands are written in present imperative:
final class DoSomething {}
Event Class Names
Events are written in simple past:
final class SomethingHappened {}
Exceptions
Exceptions
- are written in (negative) simple past (suffix “Exception” optional when deemed necessary)
- should be created via named constructor to better describe the cause and centralize default messages and codes
final class SomethingFailed extends \DomainException {
public static function becauseADomainInvariantWasNotSatisfied(string $attemptedValue): self {
return new self('Message using ' . $attemptedValue, 1234567890)
}
}
[to be continued…]
What do you think?