0

We have a project created in CodeIgniter that was started with PHP 5.6, then migrated to PHP 7.0 and now we are migrating to PHP 8.

However, our code has several places that use the sizeof function to find out if the Model functions are returning an empty value or not.

When not empty, it returns an object.

The problem is that with Visual Studio Code we cannot easily locate the functions that will present the problem, we can only identify it by running the code, even using PHP Intelephense.

Is there any way to be able to identify these possible failures via VS Code?

This is sample code that will generate an error.

//Model.php
public function exampleFunction()
{
   /**
    *
    */
   $query = $this->db->get();
   return $query->first_row();
}

//Controller.php
$result = $this->ModelM->exampleFunction();

if (sizeof($result) === 0) {
if (sizeof($result) != 0) {
if (sizeof($result) > 0) {
Tom
  • 641
  • 2
  • 8
  • 21
  • 2
    Can't you just do a "find in project" for "sizeof"? VSCode definitely has a feature for searching all the files in a project. That said, `sizeof` is just an alias for `count`, and its behavior shouldn't have changed significantly from PHP 7 to PHP 8. What exact error are you getting? – ceejayoz Apr 18 '23 at 18:44
  • Sounds like a fundamental design failure in your model methods. CI has `result()` and `result_array()` which return 2d arrays unconditionally. `row()` will return a nullable object. `row_array()` will return a flat array. `first_row()` returns a nullable object. You should be explicitly declaring return types on every single method in your models. Do this. Your IDE will be work much better. – mickmackusa Apr 18 '23 at 21:11
  • Nullable data types are a pleasure to work with in PHP because of the null coaleasing operator. In my CI model methods, my return values are never a mix of array and object data types. If the can be an array, it will always be an array -- this way the payload can be unconditionally passed directly into a loop. For objects coming from a model method, these will always be objects or nullable objects depending on context. Methods that return numeric data should typed as int or float. Methods which return booleans should be bool and the method name should start with is/has/was etc. – mickmackusa Apr 18 '23 at 21:23
  • More food for thought: https://stackoverflow.com/a/71641778/2943403 and https://stackoverflow.com/a/66517590/2943403 and https://stackoverflow.com/a/72315951/2943403 and https://stackoverflow.com/a/64923732/2943403 and https://stackoverflow.com/a/70111959/2943403 – mickmackusa Apr 18 '23 at 21:33

0 Answers0