77import com .jetbrains .php .PhpIndex ;
88import com .jetbrains .php .lang .documentation .phpdoc .psi .PhpDocComment ;
99import com .jetbrains .php .lang .documentation .phpdoc .psi .tags .PhpDocParamTag ;
10- import com .jetbrains .php .lang .psi .elements .Field ;
11- import com .jetbrains .php .lang .psi .elements .PhpClass ;
12- import com .jetbrains .php .lang .psi .elements .PhpNamedElement ;
10+ import com .jetbrains .php .lang .psi .elements .*;
1311import com .jetbrains .php .lang .psi .resolve .types .PhpType ;
1412import com .jetbrains .php .lang .psi .resolve .types .PhpTypeProvider3 ;
1513import org .jetbrains .annotations .Nullable ;
@@ -35,27 +33,32 @@ public PhpType getType(PsiElement psiElement) {
3533 return null ;
3634 }
3735
38- if (!(psiElement instanceof Field )) {
36+ if (!(psiElement instanceof Field ) && ! isGetter ( psiElement ) ) {
3937 return null ;
4038 }
4139
42- PhpClass containingClass = ((Field ) psiElement ).getContainingClass ();
43- if (containingClass == null ) {
40+ if (!isEntityClass (psiElement )) {
4441 return null ;
4542 }
4643
47- Collection <PhpClass > classesByFQN = PhpIndex .getInstance (psiElement .getProject ()).getClassesByFQN (TYPO3_CMS_EXTBASE_DOMAIN_OBJECT_ABSTRACT_ENTITY );
48- if (classesByFQN .isEmpty ()) {
49- return null ;
50- }
44+ return extractReturnType (psiElement );
45+ }
5146
52- PhpClass abstractEntityClass = classesByFQN .iterator ().next ();
47+ private PhpType extractReturnType (PsiElement psiElement ) {
48+ Field field ;
49+ if (psiElement instanceof MethodReference ) {
50+ field = extractFieldFromGetter ((MethodReference ) psiElement );
51+ } else if (psiElement instanceof Method ) {
52+ field = extractFieldFromGetter ((Method ) psiElement );
53+ } else {
54+ field = ((Field ) psiElement );
55+ }
5356
54- if (! PhpClassHierarchyUtils . isSuperClass ( abstractEntityClass , containingClass , true ) ) {
57+ if (field == null ) {
5558 return null ;
5659 }
5760
58- PhpDocComment docComment = (( Field ) psiElement ) .getDocComment ();
61+ PhpDocComment docComment = field .getDocComment ();
5962 if (docComment == null ) {
6063 return null ;
6164 }
@@ -82,6 +85,62 @@ public PhpType getType(PsiElement psiElement) {
8285 return phpType ;
8386 }
8487
88+ private Field extractFieldFromGetter (MethodReference methodReference ) {
89+ String substring = methodReference .getName ().substring (2 );
90+ char [] cArr = substring .toCharArray ();
91+ cArr [0 ] = Character .toLowerCase (cArr [0 ]);
92+
93+ String propertyName = new String (cArr );
94+
95+ PsiElement method = methodReference .resolve ();
96+ if (!(method instanceof Method )) {
97+ return null ;
98+ }
99+
100+ PhpClass containingClass = ((Method ) method ).getContainingClass ();
101+
102+ if (containingClass == null ) {
103+ return null ;
104+ }
105+
106+ return containingClass .findFieldByName (propertyName , true );
107+ }
108+
109+ private Field extractFieldFromGetter (Method method ) {
110+ String substring = method .getName ().substring (3 );
111+ char [] cArr = substring .toCharArray ();
112+ cArr [0 ] = Character .toLowerCase (cArr [0 ]);
113+
114+ String propertyName = new String (cArr );
115+
116+ PhpClass containingClass = method .getContainingClass ();
117+
118+ if (containingClass == null ) {
119+ return null ;
120+ }
121+
122+ return containingClass .findFieldByName (propertyName , false );
123+ }
124+
125+ private boolean isEntityClass (PsiElement psiElement ) {
126+ PhpClass containingClass = ((PhpClassMember ) psiElement ).getContainingClass ();
127+ if (containingClass == null ) {
128+ return false ;
129+ }
130+
131+ Collection <PhpClass > classesByFQN = PhpIndex .getInstance (psiElement .getProject ()).getClassesByFQN (TYPO3_CMS_EXTBASE_DOMAIN_OBJECT_ABSTRACT_ENTITY );
132+ if (classesByFQN .isEmpty ()) {
133+ return false ;
134+ }
135+
136+ PhpClass abstractEntityClass = classesByFQN .iterator ().next ();
137+ return PhpClassHierarchyUtils .isSuperClass (abstractEntityClass , containingClass , true );
138+ }
139+
140+ private boolean isGetter (PsiElement psiElement ) {
141+ return (psiElement instanceof Method ) && ((Method ) psiElement ).getName ().startsWith ("get" );
142+ }
143+
85144 @ Override
86145 public Collection <? extends PhpNamedElement > getBySignature (String expression , Set <String > visited , int depth , Project project ) {
87146 return PhpIndex .getInstance (project ).getBySignature (expression );
0 commit comments