1

I'm trying to get PhpStorm to stop warning me about methods for a dynamically initialized class.

Here is the code for my method:

function create_or_update_objects ($object_name, $method_callbacks = []) {
    // grab duplicate field names used to check for existing objects
    $field = new Field();
    $duplicate_field_names = $field->fetch_duplicate_names();

    foreach ($_SESSION['objects'] as $session_object) {
        $object = new $object_name();

        // set object properties
        foreach ($session_object as $key => $value) {
            if (property_exists($object, $this->data[$key]['name'])) {
                $object->{$this->data[$key]['name']} = $value;
            }
            else {
                $object->fields[$this->data[$key]['name']] = $value;
            }
        }

        // perform object method callbacks
        foreach ($method_callbacks as $method_callback) {
            $object->$method_callback();
        }

        if ($object->exists_by_duplicate_fields($duplicate_field_names)) {
            // object already exists, update by data field names
            $object->set_id_by_duplicate_fields($duplicate_field_names);
            $object->update_by_data_names($this->data);
        }
        else {
            // object does not exist, create it
            $object->create();
        }
    }
}

PhpStorm is giving me warnings about the following methods not found:

  • exists_by_duplicate_fields
  • set_id_by_duplicate_fields
  • update_by_data_names
  • create

I am aware that I can use /** @noinspection PhpUndefinedMethodInspection */ above every single line - but I'm wondering if there is a better way of doing this?

1 Answer 1

6

What I would do:

  1. Create an abstract class.

  2. Add missing methods in your abstract class. They should be abstract or put real implementation with a correct return.

  3. Above line $object = new $object_name(); add this PHPDoc instruction: /** @var PARENT_CLASS $object */ - just replace PARENT_CLASS with a real class

Example:

abstract class MyParent {
   public $fields;

   abstract public function exists_by_duplicate_fields($duplicate_field_names);
   abstract public function set_id_by_duplicate_fields($duplicate_field_names);
   abstract public function update_by_data_names($data);
   abstract public function create();
}

And then somewhere in your code:

/** @var MyParent $object */
$object = new $object_name();

Notes:

  1. You can also add /** @var Field[] */ above public $fields; It should help PhpStorm to understand how to treat $fields member every time, when you use it.

  2. It would be good if all that classes with names $object_name could actually be inherited from a PARENT_CLASS.

  3. You can replace abstract methods with implementation if needed.

1
  • 1
    Yeah this is what I'm going with. It's very helpful to have this abstract class because it gives me a clear indication of what methods and properties are required for any object utilized by this helper method.
    – kjdion84
    Commented Jan 29, 2017 at 21:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.