If you haven’t seen it yet, work is being done on Zend Coding Standard 2. Most rules we have so far are based on the coding style used in the newer zend-expressive* packages. However there is a big question left and we can’t agree on it:
To align or not to align?
Should we align equal statements, arrays and phpdoc comment tags or not? There is no right or wrong here, it’s personal preference, but we do like to know your opinion. Examples are below the poll.
- Yes, align equal statements, arrays and phpdoc comment tags.
- No, do not align anything.
Arguments I’ve seen so far:
The readability benefits are typically not huge, and enforcing it means a lot of extra changes for what are often trivial additions.
Aligning brings back extra line changes in commits and that’s exactly what the trailing comma prevents.
Readability is king, and
git diff
,git blame/annotate
as well as the github and gitlab UI have flags for ignoring whitespace changes and so do modern IDEs like PhpStorm.
Examples:
Statement not aligned:
$shortVar = (1 + 2);
$veryLongVarName = 'string';
Statement aligned:
$shortVar = (1 + 2);
$veryLongVarName = 'string';
Array not aligned:
$serializedRequest = [
'method' => 'POST',
'request_target' => '/foo/bar?baz=bat',
'uri' => 'http://example.com/foo/bar?baz=bat',
'protocol_version' => '1.1',
'headers' => [
'Host' => ['example.com'],
'Accept' => ['application/json'],
'X-Foo-Bar' => [
'Baz',
'Bat',
],
],
'body' => '{"test":"value"}',
];
Array aligned:
$serializedRequest = [
'method' => 'POST',
'request_target' => '/foo/bar?baz=bat',
'uri' => 'http://example.com/foo/bar?baz=bat',
'protocol_version' => '1.1',
'headers' => [
'Host' => ['example.com'],
'Accept' => ['application/json'],
'X-Foo-Bar' => [
'Baz',
'Bat',
],
],
'body' => '{"test":"value"}',
];
Function comment not aligned:
/**
* @param array $serverParams Server parameters, typically from $_SERVER
* @param array $uploadedFiles Upload file information, a tree of UploadedFiles
* @param null|string|UriInterface $uri URI for the request, if any.
* @param null|string $method HTTP method for the request, if any.
* @param string|resource|StreamInterface $body Message body, if any.
* @param array $headers Headers for the message, if any.
* @param array $cookies Cookies for the message, if any.
* @param array $queryParams Query params for the message, if any.
* @param null|array|object $parsedBody The deserialized body parameters, if any.
* @param string $protocol HTTP protocol version.
* @throws Exception\InvalidArgumentException for any invalid value.
*/
Function comment aligned:
/**
* @param array $serverParams Server parameters, typically from $_SERVER
* @param array $uploadedFiles Upload file information, a tree of UploadedFiles
* @param null|string|UriInterface $uri URI for the request, if any.
* @param null|string $method HTTP method for the request, if any.
* @param string|resource|StreamInterface $body Message body, if any.
* @param array $headers Headers for the message, if any.
* @param array $cookies Cookies for the message, if any.
* @param array $queryParams Query params for the message, if any.
* @param null|array|object $parsedBody The deserialized body parameters, if any.
* @param string $protocol HTTP protocol version.
* @throws Exception\InvalidArgumentException for any invalid value.
*/
If you have additional thoughts or comments, please let us know.