PhpStorm Field accessed via magic method

PhpCodeigniterPhpstorm

Php Problem Overview


I have ignited datatables Library in my CodeIgniter library folder.

Some Code from Library

class Datatables
{
    /**
     * Global container variables for chained argument results
     *
     */
    protected $ci;
    protected $table;
    protected $distinct;
    protected $group_by;
    protected $select         = array();
    protected $joins          = array();
    protected $columns        = array();
    protected $where          = array();
    protected $filter         = array();
    protected $add_columns    = array();
    protected $edit_columns   = array();
    protected $unset_columns  = array();

    /**
     * Copies an instance of CI
     */
    public function __construct()
    {
        $this->ci =& get_instance();
    }

Then I called the library in model

class Common_Model extends MY_Model{

    function __construct(){
        parent::__construct();
        $this->load->library('Datatables.php');
    }

then I tried to call the library functions

function select_fields_joined_DT($data, $PTable, $joins = '', $where = '', $addColumn = '',$unsetColumn='')
{
    /**
     *
     */
    $this->datatables->select($data);
    if ($unsetColumn != '') {
        unset_column($unsetColumn);
    }
        $this->datatables->from($PTable);
    if ($joins != '') {
        foreach ($joins as $k => $v) {
            //$this->datatables->join($v['table'], $v['condition'], $v['type']);
        }
    }

    if ($addColumn != '') {
        $this->datatables->add_column("Actions", $addColumn);
    }

    $result = $this->datatables->generate();
    return $result;
}

and everything works fine, except that the phpstorm shows me this error:

Field Accessed via magic method

enter image description here

I tried to remove this error with document comments but can't figure out how can I do that.. any help will be appreciated.

Php Solutions


Solution 1 - Php

Edit: Just because I keep getting upvotes for this, I want to preface this answer with a caveat. I inherited an old project that I would not be working on long term nor be paid to properly type everything. I consider the below a nuclear option and would only do so under similar conditions. If this is a project you own or will at least be working with a long time, especially in modern PHP 7/8 and beyond era, please don't do this and tidy up your code instead with actual types or at least a docblock :) Original answer follows below.

If you want to remove this without document comments you can uncheck Notify about access to a property via magic method which is found in

Project Settings > Inspections > PHP > Undefined > Undefined property

Notify about access to a field via magic method

PhpStorm preferences screenshot

Solution 2 - Php

As mentioned by LazyOne in the question comments:

> You have to declare them via @property in PHPDoc comment that > belongs to that class.

/**
 * @property string $bar
 */
class Foo {

    public function __get($name) {
        if ($name == 'bar') {
            return 'bar';
        }
        return NULL;
    }
}

Snippet from Dmitry Dulepov's article "Quick tip: magic methods and PhpStorm".

Solution 3 - Php

If you don't want to disable the inspection for the whole project, and can't modify the class file to add a @property tag, this is how to suppress the warning at the location where it is used:

/** @noinspection PhpUndefinedFieldInspection */

Just add it in a new line right before the line where the magic field is used.

Solution 4 - Php

@christian answer is still valid, but the path for disabling the inspection recently changed.

Here is the new one:

Preferences > Editor > Inspections > PHP > Undefined Symbols > Undefied property

Then disable the "Notify of access to a property via magic method".

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSizzling CodeView Question on Stackoverflow
Solution 1 - PhpchrisanView Answer on Stackoverflow
Solution 2 - PhpEmile BergeronView Answer on Stackoverflow
Solution 3 - PhpStefanView Answer on Stackoverflow
Solution 4 - PhpTimothePearceView Answer on Stackoverflow