r/PHPhelp • • Apr 12 '23

Bulk Fix for Undefined Array Key Issues

My team maintains a very old PHP codebase. The original developer wrote about a million lines of PHP code which rely on the fact that PHP will only raise a notice when you reference an undefined array key. In PHP 8.0 this was raised to a warning, filling our syslog with crud. We're upgrading to PHP 8.1 and so while we're doing refactoring to upgrade the codebase to support the new runtime, I want to take the time to clean this up.

I'm using Rector and I don't see a rule that will clean these up. I haven't found a php-cs-fixer rule or anything else out there either. Any advice on to change code like $foo = $_POST['bar']; to $foo = $_POST['bar'] ?? false;?

13 Upvotes

22 comments sorted by

8

u/Plastonick Apr 12 '23

Just a quick note, accessing an undefined element will return null not false.

If you're changing a 1m loc code base, I'd hazard a guess there's a few places that would be sensitive to that.

1

u/Falabu Apr 12 '23

You can use PHPStorm regex replace feature, it searches and replaces all occurance in the codebase.

1

u/breich Apr 12 '23

We don't use PHPStorm but I supposed in VSCode I could do something like regex search \$_POST[('(\s+)'] and replace with $_POST[$0] ?? false or something like that. Was hoping there was something more contextually intelligent for this purpose than a regex search and replace, but I'm sure we could make it work.

3

u/RandyHoward Apr 12 '23 edited Apr 12 '23

I wouldn't advise that, that's going to create one hell of a mess. I don't have any great ideas for you, but this option would probably be my very last resort.

Edit: Here's an idea... create a method like this:

public function get_post_data($key = NULL) {
    if (!isset($_POST[$key]) {
        $value = FALSE;
    }
    else {
        $value = $_POST[$key];
    }
    return $value;
}

Then you can do a regex to replace all your $_POST['key'] with get_post_data('key').

1

u/breich Apr 12 '23

Agreed :) That's what led me here to "crowd-source" a better approach.

Rector does relatively safe refactors provided you know what you're doing with it. I was really hoping I could find a rule in there that would apply this in a more context-aware way but so far I'm coming up empty.

1

u/RandyHoward Apr 12 '23

Check my edit

0

u/[deleted] Apr 12 '23

[deleted]

1

u/breich Apr 12 '23

What about:

public function get_post_data(string $key) {
return $_POST[$key] ?? false;
}

I think that is the same code.

1

u/RandyHoward Apr 12 '23 edited Apr 12 '23

It can get far simpler, I like to be long-winded with code when trying to explain something though. If I were doing this in a production environment, I'd probably do something more like this:

public function get_post_data($key = NULL) {
    return $key === NULL ? $_POST : $_POST[$key] ?? FALSE;
}

This also makes it more obvious why I originally had $key as optional - because if $key wasn't passed in I'd expect to get all post data returned. I just didn't take it that far because it wasn't necessary in this use case.

0

u/[deleted] Apr 12 '23

[deleted]

1

u/RandyHoward Apr 12 '23

It will not

0

u/[deleted] Apr 12 '23

[deleted]

1

u/RandyHoward Apr 12 '23

I've got a production codebase littered with $_POST[$key] ?? FALSE that I've inherited that has no problems at all. That's exactly what the null coalescing operator (??) is for.

0

u/[deleted] Apr 12 '23

[deleted]

→ More replies (0)

1

u/SomeOtherGuySits Apr 12 '23

Check out rector

2

u/breich Apr 12 '23

I use rector. As far as I can tell there is no rule for this.

1

u/SomeOtherGuySits Apr 13 '23

Write your own one

2

u/breich Apr 13 '23

That is an option. Didn't want to reinvent the wheel if a solution exists.

2

u/Big-Dragonfly-3700 Apr 12 '23

If the most immediate problem is just for $_POST data, only unchecked checkbox/radio fields won't be set after the form has been submitted. All other form field types will be set.

If you are getting php errors for the always-set field types, that indicates that the post method form processing code is being unconditionally executed when the page is being requested. Depending on how good the server-side validation logic is, the code could be operating on empty data in the processing logic.

The fix in this case is to detect if a post method form has been submitted before executing any of the post method form processing code - if($_SERVER['REQUEST_METHOD'] === 'POST'){ post method form processing code }

If any of the form fields are arrays, the current search/replace fix will still produce errors, because an empty array, rather than a false value, would need to be supplied.

1

u/kAlvaro Apr 12 '23

For completeness (though I presume you don't want to go that route), you could also write a custom sniff for PHP_CodeSniffer and provide a fixable warning. That way, phpcs can report it and phpcbf can apply your fix.

1

u/PeaScary2144 Apr 12 '23

One of my colleagues has just suppressed the warning via error_reporting(). But always cringe as I see that.

3

u/DmC8pR2kZLzdCQZu3v Apr 12 '23

god, please don't do this. did you tell your colleague he or she is a jackass?

1

u/PeaScary2144 Apr 13 '23

Unfortunately, I can't

1

u/[deleted] Apr 17 '23

I'm pretty sure you can!

Most things can be done. Some only once, but still ...

1

u/aikaradora May 05 '23

Stop using false, use null, don't return booleans for non boolean values. ?? null keeps behavior the same without warning l.