Bug 519290 - @return static[] doesnt' work
Summary: @return static[] doesnt' work
Status: NEW
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: PDT (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows 10
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: PHP Core CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-07-06 05:36 EDT by Laurent Lyaudet CLA
Modified: 2020-05-14 10:16 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Laurent Lyaudet CLA 2017-07-06 05:36:54 EDT
Hi,

I tested that for a method that returns an object the PHP doc "@return static" works.
But for a method that returns an array of objects the PHP doc "@return static[]" doesn't give any autocompletion.
It would be useful if it worked also.

Thanks, best regards,
  Laurent Lyaudet
Comment 1 Thierry BLIND CLA 2017-07-06 08:47:24 EDT
Hi Laurent,
please could you provide us a little example of a working case (for "@return static") and a non-working case (for "@return static[]") ?
Comment 2 Laurent Lyaudet CLA 2017-07-07 09:49:53 EDT
Hi Thierry,

Here is a sample code to test the autocompletion.

Best regards,
   Laurent Lyaudet

<?php

class A{
  
  public $tabOChildren;
  
  
  
  /**
   * @return static
   */
  public static function make(){
    return new static();
  }

  
  
  /**
   * @return static[]
   */
  public function getTabOChildren(){
    return $this->tabOChildren;
  }
  
}

class B extends A{
  
  public function willAppearOnBAutocompleteNotA(){
    
  }
  
}


$oB = B::make();

$oB->willAppearOnBAutocompleteNotA();//autocomplete sucessfull
$tabOB = $oB->getTabOChildren();

$oB2 = $tabOB[0];
//$oB2-> no autocompletion

$oB3 = reset($tabOB);
//$oB3-> no autocompletion



?>
Comment 3 Thierry BLIND CLA 2017-07-21 10:30:43 EDT
Hi Laurent,

actually "@return static" and "@return static[]" are not valid (see https://manual.phpdoc.org/HTMLSmartyConverter/HandS/phpDocumentor/tutorial_tags.return.pkg.html), it should be "@return A" and "@return A[]", because "static" is seen in the PHPDoc as a class name.
"static" has no special meaning in phpdocs (same for "self", ...).

And yes, there could be some improvements for "reset($tabOB);", maybe the day when php uses same syntax as the java language and its Generic Types, 
for example "<T> T reset(T $param)" so erasure would offer more powerful autocompletion hints ;)
Comment 4 Laurent Lyaudet CLA 2017-07-21 11:42:25 EDT
Hi Thierry,
Indeed the documentation of PHPDoc is out of date compared to what has been implemented in many IDEs (such as eclipse for @return static).
I wanted to do a feature request there : https://sourceforge.net/projects/phpdoc/?source=directory
But the project is deprecated.

I assume Eclipse and other IDEs are free to implement the best possible improvements to the original specifications of PHPDoc.

Best regards,
 Laurent
Comment 5 Laurent Lyaudet CLA 2017-07-21 11:48:57 EDT
I forgot to say that reset($tabOB) doesn't erase the array.
It just resets the iterator to loop through the array.
You can do more complicated loops than foreach by using reset() and each().
I used it occasionally where foreach was inappropriate.

I also often use reset alone since it returns the first element of the array, which is useful when its key in the array is not 0.

Best regards,
   Laurent
Comment 6 Thierry BLIND CLA 2017-07-21 14:43:41 EDT
(In reply to Laurent Lyaudet from comment #4)
> Hi Thierry,
> Indeed the documentation of PHPDoc is out of date compared to what has been
> implemented in many IDEs (such as eclipse for @return static).
> I wanted to do a feature request there :
> https://sourceforge.net/projects/phpdoc/?source=directory
> But the project is deprecated.
> 
> I assume Eclipse and other IDEs are free to implement the best possible
> improvements to the original specifications of PHPDoc.
> 
> Best regards,
>  Laurent

Yes, sorry, you're actually right, it seems that "self", "static" and "$this" can be used as phpdoc keywords: https://www.phpdoc.org/docs/latest/guides/types.html
Comment 7 Thierry BLIND CLA 2017-07-21 14:59:55 EDT
(In reply to Laurent Lyaudet from comment #5)
> I forgot to say that reset($tabOB) doesn't erase the array.
> It just resets the iterator to loop through the array.
> You can do more complicated loops than foreach by using reset() and each().
> I used it occasionally where foreach was inappropriate.
> 
> I also often use reset alone since it returns the first element of the
> array, which is useful when its key in the array is not 0.
> 
> Best regards,
>    Laurent

Yes maybe we should handle current(), reset(), next() and prev() in a special way, or create some special phpdoc tag to use with functions that return same type than their unique (input) parameter...
Comment 8 Eclipse Genie CLA 2017-08-24 07:11:17 EDT
New Gerrit change created: https://git.eclipse.org/r/103622
Comment 10 Thierry BLIND CLA 2017-08-25 10:20:19 EDT
For info:
my patch makes "@return static[]" work like "@return static" now.
But there is still room for improvement (this will be another patch), because actually keyword "self" does the same as "static" or "$this", but it shouldn't.

In phpdocumentor, it is said that:

self: An object of the class where this type was used, if inherited it will still represent the class where it was originally defined.

static: An object of the class where this value was consumed, if inherited it will represent the child class. (see late static binding in the PHP manual).

$this: This exact object instance, usually used to denote a fluent interface.

Also http://php.net/manual/en/language.oop5.late-static-bindings.php explains the difference between "self" and "static"