r/programminghorror • u/Takeoded • Dec 13 '23
what the hell happened here
// If master_test_connection_id exists, check if adtemplate_id exists, if it does add the ad_id to it, if not, add it to the master_test_connection_id with an array for ad_ids
if (!empty($data["master_test_data"]["master_test_ids"][$val["master_test_id"]]["master_test_connection_ids"][$val["master_test_connection_id"]])) {
if (!empty($val[$testKey]) && empty($data["master_test_data"]["master_test_ids"][$val["master_test_id"]]["master_test_connection_ids"][$val["master_test_connection_id"]][$testKeyPlural][$val[$testKey]])) {
$data["master_test_data"]["master_test_ids"][$val["master_test_id"]]["master_test_connection_ids"][$val["master_test_connection_id"]][$testKeyPlural][$val[$testKey]]["ad_ids"] = [$val["ad_id"]];
}
if (!empty($val["ad_id"]) && !empty($data["master_test_data"]["master_test_ids"][$val["master_test_id"]]["master_test_connection_ids"][$val["master_test_connection_id"]][$testKeyPlural][$val[$testKey]]["ad_ids"]) && !in_array($val["ad_id"], $data["master_test_data"]["master_test_ids"][$val["master_test_id"]]["master_test_connection_ids"][$val["master_test_connection_id"]][$testKeyPlural][$val[$testKey]]["ad_ids"], true)) {
$data["master_test_data"]["master_test_ids"][$val["master_test_id"]]["master_test_connection_ids"][$val["master_test_connection_id"]][$testKeyPlural][$val[$testKey]]["ad_ids"][] = $val["ad_id"];
}
}
14
Upvotes
12
u/drcforbin Dec 13 '23
Except for the fact the indentation is neither "all on the left" nor "higgledy piggledy," that looks pretty normal for PHP.
11
u/nuecontceevitabanul Dec 13 '23
This right here is why objects are good.
But if you're in a hurry and writing a run once script I don't see why this wouldn't do.
6
u/raw_ambots Dec 13 '23
This is where breaking down an array or object into separate variables before running logic on them can make a huge difference in readability.
if($adsById) would be so much cleaner to work with.
4
u/matthewralston Dec 13 '23
I suspect that code like this is why PHP has such a bad reputation.
3
u/v_maria Dec 14 '23
ah yes the classic "bosses nephew" paradigm
2
u/TheBrainStone Dec 14 '23
At least it's commented
-2
u/internet-personality Dec 13 '23
fuck php