phptools-docs icon indicating copy to clipboard operation
phptools-docs copied to clipboard

[feature request] compacting formatting rules

Open jakubmisek opened this issue 5 months ago • 0 comments

Suggested by user

Hi,

I’ve outlined four rules to improve code readability. I’m not yet sure how easy or difficult they'll be to implement.

rule1 – remove all spaces in arrays Remove spaces around commas and the => operator in arrays.

// Input
[1, 2, 3]
['a' => 1, 'b' => 2]

// Output
[1,2,3] 
['a'=>1,'b'=>2]

rule2 – remove spaces in arrays within function arguments Remove spaces around commas in array arguments. Exception: function definitions.

// Input
in_array($a, $b)
preg_match($regex, $lnk, $tmps)

// Output
in_array($a,$b)
preg_match($regex,$lnk,$tmps)

Function definitions like function _my($out, $io = []) should not be altered.

rule3 – trim spaces around ?? inside conditional constructs Remove spaces around the null coalescing operator ??, but only inside if (...) or other conditionals. Do NOT apply this during simple assignment.

// Input
if (($res['errors']['details'] ?? '') !== '')

// Output
if (($res['errors']['details']??'') !== '')

Simple assignment - do not change:

$_q = $pms['q'] ?? [];

rule4 – normalize else if indentation Fix alignment of nested else statements by removing tabs/spaces before if after else.

// Current formatting:
if (true)
{

}
else
    if (false)
    {

    }
    else
    {

    }

// Desired formatting:
if (true)
{

}
else
if (false)
{

}
else
{

}

Please let me know if this is clear or if you'd like me to refine examples further!

jakubmisek avatar Jun 09 '25 13:06 jakubmisek