23

[CMS of Doom™]
Ah yes, when you find a beautifully written array access at line 3187... ๐Ÿ™Œ๐ŸŒˆ๐Ÿ˜Š

Comments
  • 4
  • 3
    Kill it.
    Make sure it doesn't hurt another soul.
  • 3
    Wow, someone really wanted to avoid writing a generic path-based getter for arrays.
  • 2
  • 11
    #WCGW

    This seems appropriate
  • 2
  • 1
  • 0
    @magicMirror ๐Ÿ™Œ๐Ÿ’ฅ
  • 1
    function getMenuPath(array $menu, int ...$indexPath) {
    $reduce = function(array $elements, $idx) {
    return $elements[$idx] ?? null;
    };

    return array_reduce($indexPath, $reduce, $menu);
    }

    Splat Operator for variadic args with strict type hinting, should be array.

    array reduce approach explained e.g. here : https://baohx2000.medium.com/reachi...

    Just a bit neater without array_key_exist.

    You could make the variadic arg a string of course.qk

    By the way: I'm drunk, 10 hours of work and my brain is shrooms shrooms.

    Might be wrong. I dunno.
  • 4
    @IntrusionCM Nah, it'll be fine! Gonna use your code in Prod tomorrow!
    kthxbye โค๏ธ

    In all seriousness though, the array_reduce is a great idea, but I have actual anxiety to refactor this method for now... There are about 150k LOC more of such utter shit. The thing is beyond saving. For now my job is just to fix the bugs that appear, until I replace the whole thing in summer with a new software using mircoservices.
  • 1
    @PonySlaystation

    Yeah. Variadic args are imho a nicer approach.

    getMenuPath($menu, 1, $menu['path'][0], $menu['path'][1])

    as an example.

    Of course you can push to production.

    See the image.xD

    Regarding microservices: Again, see the image. ๐Ÿคฃ๐Ÿคฃ๐Ÿคฃ
  • 0
    @IntrusionCM Which image are you referring to? ๐Ÿ˜„
  • 5
    @PonySlaystation

    Forgot devrants special ability to ignore uloads of images.
  • 2
    @IntrusionCM Thank you so mucg! This image fits perfectly in my current situation...
  • 2
    @IntrusionCM this hits home right now.

    3rd party vendor gave incorrect documentation and now I don't know whether to cry or laugh at this insanity of fixing their shit.
  • 0
    The fuck is the point in using a null coalesce with a null as the second argument? ๐Ÿคจ
  • 0
    @kwilliams The null-coalescing operator is there to catch non-existing array index access. Without this operator, an error would be thrown.
  • 1
    @C0D4 Yeah... I installed and configured 5 proxmox nodes, 22 VMs, and had a few meetings...

    Meetings were like: Why is this darker shade of green it needs to be less darker shade of green?

    Why is the number there different from the number here (one order - one from a shop, the other from the booking system that interacts with the shop...)

    Why does this not work?
    - me having a look -
    Never mind, restarted everything. Do you know why it didn:t work?
    - yeah... When you restart things I can look back in the past and find out what happened, of course. Naarf.

    Yesterday was the epitome of "If I stop laughing, I'll drive a shotgun up my arse to feel alive again".
  • 0
    I'm more surprised by the PHP 7+ ??
    You just don't expect someone to be aware of such code shortening operators at that line count and left hand...
  • 1
    @hjk101 Can you elaborate? ๐Ÿค”
  • 0
    @IntrusionCM
    I would expect the original style ternary here
    isset($menu [1]...) ? $menu [1]... : null;

    Instead of the null coalescing ternary that specifically deduplicates the item you are testing for existence.

    In code like that you just don't expect any kind of efficiency
Add Comment