This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 239986 - "@return this" depends on namespace
Summary: "@return this" depends on namespace
Status: NEW
Alias: None
Product: php
Classification: Unclassified
Component: Editor (show other bugs)
Version: 7.4
Hardware: All All
: P3 normal with 1 vote (vote)
Assignee: Ondrej Brejla
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-01-04 16:02 UTC by Aequiternus
Modified: 2014-01-06 15:32 UTC (History)
0 users

See Also:
Issue Type: ENHANCEMENT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Aequiternus 2014-01-04 16:02:06 UTC
Phpdoc "@return this" works fine when you dont use namespaces. When use namespace, works only if add backslash at beginning of keyword "@return \this". Keywords shouldn't be namespace dependant.
Here is example code:

namespace test;

class A
{
    /**
     * @return this
     */
    public function foo() {}
}

class B extends A
{
    /**
     * @return this
     */
    public function boo() {}
}

namespace test2;

$obj = new \test\B();
$obj->boo()-> // no suggestions here



With backslash work as should:

namespace test;

class A
{
    /**
     * @return \this
     */
    public function foo() {}
}

class B extends A
{
    /**
     * @return \this
     */
    public function boo() {}
}

namespace test2;

$obj = new \test\B();
$obj->boo()->foo()->boo()->foo()-> // works fine
Comment 1 Ondrej Brejla 2014-01-06 12:28:31 UTC
"this" is not keyword and can't work that way you described, because one can declare a class (or any other type) which will be called "this".

This is valid php program:

<?php

class this {
function __construct() {echo "OK";}
}

new this();

?>
Comment 2 Ondrej Brejla 2014-01-06 12:31:20 UTC
The only thing which could probably works is "@return $this", but it has to be implemented, it never worked. It's an enhanecement.
Comment 3 Aequiternus 2014-01-06 14:45:52 UTC
You are right, "this" can not be keyword, but workaround with "@return \this" really work at the moment.



Also, I think, code completion should have same behavior if method contains code "return $this;" without any phpdoc. But at the moment, it works like "@return self":

namespace test;

class A
{
    public function foo()
    {
        return $this;
    }
}

class B extends A
{
    public function boo()
    {
        return $this;
    }
}

namespace test2;

$b = new \test\B();
$b->boo()->foo()-> // CC has only method "foo" here, but should have both "boo" and "foo"

Should I make new enchancement request for this?
Comment 4 Ondrej Brejla 2014-01-06 14:54:04 UTC
You can but imho it's not necessary. It will not be easy to fix, because $this depends on runtime, but return types in IDE are solved "statically", IDE really don't and can't know what will be the exact type of "$this" in runtime.
Comment 5 Aequiternus 2014-01-06 15:32:27 UTC
I have fiew other notices, that might help to solve the problem:
phpdoc "@return static" have close to same behavior as "@return $this" should.

namespace test;

class A
{
    /**
     * @return static
     */
    public function foo()
    {
    }
}

class B extends A
{
    /**
     * @return static
     */
    public function boo()
    {
    }
}

namespace test2;

$b = new \test\B();
$b->boo()->foo()->boo()->foo()->boo()->foo()-> // works fine!

But if you add "return $this;" in methods, looks like phpdoc and return statement start to conflict:


namespace test;

class A
{
    /**
     * @return static
     */
    public function foo()
    {
        return $this;
    }
}

class B extends A
{
    /**
     * @return static
     */
    public function boo()
    {
        return $this;
    }
}

namespace test2;

$b = new \test\B();
$b->boo()->foo()-> // sometimes only "foo", sometimes "no suggestions", sometimes both "foo" and "boo"

Because of that conflict, I tried to use "@return this" phpdoc and found "@return \this" workaround.

I understand that "return $this;" has much complex behavior than phpdoc "@return static", but it will be better than what we have at the moment, if they'll have same behavior for code completion.