php-fig/per-coding-style

Spaces in type casting

dmotitsk opened this issue · 7 comments

Are there any requirements regarding spaces in type casting? There is the requirement:

Type casting operators MUST NOT have any space within the parentheses

But what about the space between the type and the expression? I.e.

$intValue = (int) $input;

or

$intValue = (int)$input;

Currently there's no requirement for it.

vjik commented

Maybe create this requirement?

(int) $input looks good.

Crell commented

I've only ever seen it without a space: (int)$x.

guvra commented

I've only ever seen it without a space: (int)$x.

php-cs-fixer adds a space by default: https://cs.symfony.com/doc/rules/cast_notation/cast_spaces.html.
Both laravel and symfony use a single space between cast and variable.

PSR-12, Laminas, and the PHP docs all also add spaces. I can't really think of a good reason to omit this so I'd be okay with adding this for typecasts. Something like

Typecasts MUST be separated from the variable they are operating on by exactly one space.

Crell commented

If that's the de facto consensus, I don't have a strong argument to not follow it on this one. Sounds like we need a PR.