76

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.

Community
  • 1
  • 1
Sizzling Code
  • 5,932
  • 18
  • 81
  • 138
  • 35
    You have to declare them via `@property` in PHPDoc comment that belongs to that class. – LazyOne Aug 30 '14 at 09:33
  • 3
    Is there any way to do this without touching the file that the class is declared in? For example, if the class is part of a third-party library, and I don't want to make custom patches to that code just to add these `@property` comments? – Enno Jul 03 '19 at 09:43
  • @Enno Did you find an answer to your question? I'm also having the same problem as yours. The only way I can think of is to create a class extending the third-party class and then add the `@property`? Not sure if it'll work though. – mackth Jun 09 '20 at 13:50
  • @markandrewkato I think it depends a lot on your situation whether you can extend the class or not. In my case, it wasn't possible, and to be honest, I no longer remember what I did. – Enno Jun 10 '20 at 12:32
  • @Enno No problem. Since I can't add a doc to third party and I don't want to extend neither, I just followed the answer below. Thanks! – mackth Jun 11 '20 at 16:17

4 Answers4

135

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

chrisan
  • 4,152
  • 4
  • 28
  • 32
  • 55
    Not sure whether to laugh or cry. I switched from Idea to stand alone phpstorm recently and having this problem again I googled this notification to find this SO question. I did not like the upvoted answer and found @LazyOne's comment to be what I was looking for. Then realized this answer was mine... – chrisan Apr 03 '17 at 11:53
  • 1
    I made LazyOne's comment a community answer. Feel free to add to it or correct the information. – Emile Bergeron Apr 11 '17 at 17:19
  • Well done. I am facing now this issue. I am little bit used to PyCharm with Python relations, of course. I am new to PhpStorm. It was easy to find out this route and uncheck Undefined property. This is good because as I am working with Laravel...happens all the time! Thank you a lot! – Bitart Jun 13 '21 at 13:41
  • @Bitart check out https://github.com/barryvdh/laravel-ide-helper I don't run into undefineds anymore with that – chrisan Jun 14 '21 at 16:18
  • 2
    Just couldn't help myself from laughing at chrisan's comment. – Jasonfish Oct 31 '21 at 14:18
  • In laravel we can't declare @property because it is relationship behind the scenes. Any tips? – keizah7 Feb 22 '22 at 05:28
  • @keizah7 have you tried laravel ide helper? – chrisan Feb 23 '22 at 10:38
  • I trying find a way without ide helper – keizah7 Feb 23 '22 at 14:44
57

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

Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
  • The link to the article is no longer valid. – Craig London Nov 13 '17 at 13:33
  • 2
    @CraigLondon the article isn't needed as everything is in the answer and the link was only provided as a source. Still, I updated it with a [wayback machine](http://archive.org/web/) snapshot link. – Emile Bergeron Nov 13 '17 at 16:42
  • 2
    I honestly like this a lot more than the chosen answer. It gives a way to explicitly say what we can expect from a class rather than just leaving it all up to magic and possible typing errors – Chris Nov 25 '18 at 11:50
5

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.

Stefan
  • 51
  • 1
  • 1
  • 1
    May be you can refer to the official documentation on this topic to be more helpful : https://www.jetbrains.com/help/phpstorm/code-inspection.html https://www.jetbrains.com/help/phpstorm/disabling-and-enabling-inspections.html#suppress-inspections – djleop Aug 13 '20 at 11:24
4

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

TimothePearce
  • 1,128
  • 1
  • 11
  • 22