0

I have some code with array assignments that look like this:

$myArray['Data'] = [
    'stuff' => [
        'junk' => [
            'my_junk' => [
                'foo' => [
                    'the_goods' => 'some data!';
                ],
            ],
        ],
    ],
];

And other code that has arrays like this:

$otherArray['Data']['stuff']['junk']['other_junk'] = "data!";

What I want (and can't seem to find) is a php-cs-fixer rule (or other tool, if there's something more appropriate for the job) that normalizes the assignment levels themselves (not just changing the indentation or long vs short brace). In my favorite world, the result would be that both examples above end up as "flat" as possible:

$myArray['Data']['stuff']['junk']['my_junk']['foo']['the_goods'] = 'some data!';


$otherArray['Data']['stuff']['junk']['other_junk'] = "data!";

But I would settle for them both being expanded:

$myArray['Data'] = [
    'stuff' => [
        'junk' => [
            'my_junk' => [
                'foo' => [
                    'the_goods' => 'some data!';
                ],
            ],
        ],
    ],
];

$otherArray['Data'] = [
    'stuff' => [
        'junk' => [
            'other_junk' => "data!",
        ],
    ],
];

The end goal being that I could more easily identify differences between two arrays that are effectively the same, but were originally written level by level ( => [) versus using the shorthand notation ( [][] ).

Anthony
  • 36,459
  • 25
  • 97
  • 163
  • if there is a rule that gets rid of explicit but unnecessary array index numbering, I would love that too. – Anthony May 21 '21 at 00:34
  • You can only do this rewrite when there's just one element in each level of the array. That seems like an unusual case to need to deal with. – Barmar May 21 '21 at 00:59
  • I don't think I've ever written a deep array initialization that would fit that criteria. Do you really do this often enough that the tool is useful? – Barmar May 21 '21 at 01:02
  • They usually originate from var_export, I suspect. – Anthony May 21 '21 at 02:00
  • Even then it seems unusual that you would only have one element at each level. Most arrays are more bushy. – Barmar May 21 '21 at 02:05
  • I would want my bushy array to be more consistent between bushes. – Anthony May 21 '21 at 03:13
  • I was being stupid, thinking that the linear assignment would somehow clear all the intermediate arrays. Although I still think that rewriting a complex array assignment until lots of linear assignments would make the code more confusing. – Barmar May 21 '21 at 14:00
  • Then the expansion would be the way to go. Just anything that makes reviewing similar code in different format consistent. – Anthony May 21 '21 at 16:46

0 Answers0