We are upgrading our laravel project, and it includes a language upgrade from php7 to php8. Consider this code:
$a = new stdClass()
count($a)
Will result in:
// php7
==> 1
// php8
==> TypeError count(): Argument #1 ($value) must be of type Countable|array
Now our app is pretty big, there are a lot of small bits and pieces to it, and count was used in many places on things that it probably shouldn't have been used. I covered most of the ground, but ideally I'd like to have some kind of fallback. Is there a way to globally override the built in count function within the laravel application, hopefully to do something like this :
// very general pseudocode
function newCount($foo) {
try {
return count($foo);
} except {
\Log::warning('used count on non Countable|array')
return 1;
}
}
Then bind it somehow so:
// anywhere in the app code
count($a) // ==> will call newCount($a) instead
You have a multiple approaches to achieve this, using some third-party extension, one approach will be using uopz which allow you to hook into a predefined function and edit it's behavior.
the other method is to use some static code analyzer - something like PHPStan - or any other alternative, to scan & update your code easily.