per-coding-style icon indicating copy to clipboard operation
per-coding-style copied to clipboard

Spaces in type casting

Open dmotitsk opened this issue 3 years ago • 5 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;

dmotitsk avatar Jul 20 '22 05:07 dmotitsk

Currently there's no requirement for it.

samdark avatar Jul 20 '22 11:07 samdark

Maybe create this requirement?

(int) $input looks good.

vjik avatar Aug 11 '22 18:08 vjik

@Crell, @KorvinSzanto thoughts?

samdark avatar Aug 11 '22 20:08 samdark

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

Crell avatar Aug 11 '22 21:08 Crell

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.

guvra avatar Aug 11 '22 21:08 guvra

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.

KorvinSzanto avatar Aug 30 '22 23:08 KorvinSzanto

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.

Crell avatar Aug 30 '22 23:08 Crell