Bug 533136 - Wrong relation between MultiTypeType and AmbiguousType in array access and iterators
Summary: Wrong relation between MultiTypeType and AmbiguousType in array access and it...
Status: NEW
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: PDT (show other bugs)
Version: unspecified   Edit
Hardware: All All
: P3 normal with 1 vote (vote)
Target Milestone: ---   Edit
Assignee: PHP Core CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-04-02 05:18 EDT by Dawid Pakula CLA
Modified: 2020-05-14 10:17 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 Dawid Pakula CLA 2018-04-02 05:18:12 EDT
Example:
/**
 * @return ArrayAccess|DateTime[]
 */
function foo() {
   return "";
}

Type inference detect:
AmbigousType:
  PHPClass: ArrayAccess
  MultyTypyType: DateTime


And it's correct, unfortunately code assist is incorrect:

foo()->| // <- I expect ArrayAccess functions, see mix
foo()[0]->| <- I expect Datetime functions, see mix
foo()[0][0]->| // < I expect empty result, see mix
foreach (foo() as $item) {
   $item->| //<- I expect datetime functions
}

Another simpler example:
/**
 * @return DateTime[]
 */
function foo() {
   return "";
}

foo()->| // <- I expect empty , see datetime
foo()[0]->| <- I expect Datetime functions
foo()[0][0]->| // < I expect empty result, see datetime


We fixed in past ArrayAccess AST for fields, so in theory should be simple to improve  this.
Comment 1 Thierry BLIND CLA 2018-04-02 12:31:10 EDT
I'll add also another problem I've seen with arrays having such kind of indexes:

$myArrayOfDateTime[count($myArrayOfDateTime) - 1]->|

will return no datetime methods :(
Comment 2 Dawid Pakula CLA 2018-04-03 09:53:05 EDT
(In reply to Thierry BLIND from comment #1)
> I'll add also another problem I've seen with arrays having such kind of
> indexes:
> 
> $myArrayOfDateTime[count($myArrayOfDateTime) - 1]->|
> 
> will return no datetime methods :(

This is different issue. 

Our parser isn't as good as JDT parser on Error-Recovery level. Due this CA trying to detect what you have before cursor and interpret this (wrongly this time). This is also introduces unnecessary complexity and code duplications (inferring before cursor vs inferring normal AST).
Comment 3 Thierry BLIND CLA 2018-04-03 15:12:44 EDT
(In reply to Dawid Pakula from comment #2)
> (In reply to Thierry BLIND from comment #1)
> > I'll add also another problem I've seen with arrays having such kind of
> > indexes:
> > 
> > $myArrayOfDateTime[count($myArrayOfDateTime) - 1]->|
> > 
> > will return no datetime methods :(
> 
> This is different issue. 
> 
> Our parser isn't as good as JDT parser on Error-Recovery level. Due this CA
> trying to detect what you have before cursor and interpret this (wrongly
> this time). This is also introduces unnecessary complexity and code
> duplications (inferring before cursor vs inferring normal AST).

ok thank you for the answer, Dawid :) You mean we should de-duplicate and only use (for example) normal AST by doing better error recovery (and no code to detect parenthesis, brackets,... in text)? so build better partial AST nodes tree?