Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhpDoc adding notation for self in a file included inside a class

Tags:

php

phpdoc

I have a class which includes a file in a method like below:

In class.php file:

class A {

const CONST1 = 5;

/** @var int $a */
var $a = 5;

public function call()
{
    include( 'b.php' );
}

public function do_some_magic()
{
    // magic stuff...
}

public static function static_func()
{
    // some code...
}

}

file b.php:

<?php
/** @var A $this */
/** @var A self */ // This doesn't work

$this->do_some_magic();

echo '['.$this->a.']';

self::static_func();

echo '['.self::CONST1.']';

I use PhpStorm as IDE and in b.php file if I want to go to definition of do_some_magic() or definition of a variable it will correctly go to corresponding method or variable definition in class.php file, but if I want to go to definition of constant CONST1 or to definition of static method static_func() it says "Cannot find definition to go to", so I think /** @var A self */ is not a valid notation in included file.

My question is: Is there any way in PhpDoc to tell IDE of what type self is in included file?

like image 461
Tipul07 Avatar asked Oct 19 '22 08:10

Tipul07


1 Answers

I searched for an answer, but as it seems at the moment only way to have auto-complete in an included file you will have to make static calls using class name. Of course, this would fail if you include the file in different classes and you want self to mean that specific class where the file was included.

So b.php would look like:

<?php
/** @var A $this */
/** @var A self */ // This doesn't work

$this->do_some_magic();

echo '['.$this->a.']';

A::static_func();

echo '['.A::CONST1.']';

If however you find a better answer please let me know.

Regards,

Andy

like image 156
Tipul07 Avatar answered Oct 24 '22 06:10

Tipul07