PhpStorm Field accessed via magic method

后端 未结 3 2085
心在旅途
心在旅途 2021-02-02 05:07

I have ignited datatables Library in my CodeIgniter library folder.

Some Code from Library

class Datatables
{
    /**
     * Global container variables for         


        
相关标签:
3条回答
  • 2021-02-02 06:01

    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.

    0 讨论(0)
  • 2021-02-02 06:07

    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

    0 讨论(0)
  • 2021-02-02 06:08

    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".

    0 讨论(0)
提交回复
热议问题