Skip to content

Commit f4c5eb3

Browse files
Merge branch 'additional-fix-added-for-grapql-resolver--task-169' of https://github.com/konarshankar07/magento2-phpstorm-plugin into additional-fix-added-for-grapql-resolver--task-169
2 parents 3a93548 + 5452042 commit f4c5eb3

File tree

31 files changed

+463
-102
lines changed

31 files changed

+463
-102
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
inspection.plugin.duplicateInSameFile=The plugin name already used in this file. For more details see Inspection Description.
2-
inspection.plugin.duplicateInOtherPlaces=The plugin name "{0}" for targeted "{1}" class is already used in the module "{2}" ({3} scope). For more details see Inspection Description.
2+
inspection.plugin.duplicateInOtherPlaces=The plugin name "{0}" for targeted "{1}" class is already used in the module "{2}" ({3} scope). For more details see Inspection Description.
3+
inspection.graphql.resolver.mustImplement=Class must implements any of the following interfaces: \\Magento\\Framework\\GraphQl\\Query\\ResolverInterface, \\Magento\\Framework\\GraphQl\\Query\\Resolver\\BatchResolverInterface, \\Magento\\Framework\\GraphQl\\Query\\Resolver\\BatchServiceContractResolverInterface

src/com/magento/idea/magento2plugin/actions/generation/generator/code/PluginMethodsGenerator.java

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
import com.jetbrains.php.lang.psi.resolve.types.PhpType;
1818
import com.magento.idea.magento2plugin.actions.generation.data.code.PluginMethodData;
1919
import com.magento.idea.magento2plugin.magento.files.Plugin;
20+
import com.magento.idea.magento2plugin.magento.packages.Package;
2021
import org.jetbrains.annotations.NotNull;
2122
import org.jetbrains.annotations.Nullable;
23+
2224
import java.util.*;
2325
import java.util.regex.Pattern;
2426

@@ -152,7 +154,17 @@ private String getParameterDoc(Collection<PsiElement> parameters, @NotNull Plugi
152154
}
153155

154156
sb.append("* @param ");
155-
String typeStr = PhpDocUtil.getTypePresentation(project, element.getType(), PhpCodeInsightUtil.findScopeForUseOperator(element));
157+
158+
String typeStr;
159+
if (i == 0) {
160+
typeStr = PhpDocUtil.getTypePresentation(project, element.getType(), null);
161+
} else {
162+
typeStr = PhpDocUtil.getTypePresentation(project, element.getType(), PhpCodeInsightUtil.findScopeForUseOperator(element));
163+
if (typeStr.indexOf(Package.FQN_SEPARATOR, 1) > 0) {
164+
String[] fqnArray = typeStr.split("\\\\");
165+
typeStr = fqnArray[fqnArray.length - 1];
166+
}
167+
}
156168

157169
if (!typeStr.isEmpty()) {
158170
sb.append(typeStr).append(' ');
@@ -202,9 +214,15 @@ protected String getParameterList(Collection<PsiElement> parameters, @NotNull Pl
202214
}
203215

204216
if (element instanceof Parameter) {
205-
buf.append(PhpCodeUtil.paramToString(element));
217+
String parameterText = PhpCodeUtil.paramToString(element);
218+
if (parameterText.indexOf(Package.FQN_SEPARATOR, 1) > 0) {
219+
String[] fqnArray = parameterText.split("\\\\");
220+
parameterText = fqnArray[fqnArray.length - 1];
221+
}
222+
buf.append(parameterText);
206223
} else {
207-
String typeHint = this.getTypeHint(element);
224+
Boolean globalType = i != 0;
225+
String typeHint = this.getTypeHint(element, globalType);
208226
if (typeHint != null && !typeHint.isEmpty()) {
209227
buf.append(typeHint).append(' ');
210228
}
@@ -257,21 +275,20 @@ protected String getReturnVariables(@NotNull Plugin.PluginType type) {
257275
}
258276

259277
@Nullable
260-
private String getTypeHint(@NotNull PhpNamedElement element) {
278+
private String getTypeHint(@NotNull PhpNamedElement element, Boolean globalType) {
261279
PhpType filedType = element.getType().global(this.pluginClass.getProject());
262280
Set<String> typeStrings = filedType.getTypes();
263281
String typeString = null;
264282
if (typeStrings.size() == 1) {
265-
typeString = this.convertTypeToString(element, typeStrings);
283+
typeString = this.convertTypeToString(element, typeStrings, globalType);
266284
}
267285

268286
if (typeStrings.size() == 2) {
269287
PhpType filteredNullType = filterNullCaseInsensitive(filedType);
270288
if (filteredNullType.getTypes().size() == 1) {
289+
typeString = this.convertTypeToString(element, filteredNullType.getTypes(), globalType);
271290
if (PhpLanguageFeature.NULLABLES.isSupported(element.getProject())) {
272-
typeString = "?" + this.convertTypeToString(element, filteredNullType.getTypes());
273-
} else {
274-
typeString = this.convertTypeToString(element, filteredNullType.getTypes());
291+
typeString = "?" + typeString;
275292
}
276293
}
277294
}
@@ -280,11 +297,11 @@ private String getTypeHint(@NotNull PhpNamedElement element) {
280297
}
281298

282299
@Nullable
283-
private String convertTypeToString(@NotNull PhpNamedElement element, Set<String> typeStrings) {
300+
private String convertTypeToString(@NotNull PhpNamedElement element, Set<String> typeStrings, Boolean globalType) {
284301
String simpleType = typeStrings.iterator().next();
285302
simpleType = StringUtil.trimStart(simpleType, "\\");
286303
if (!PhpType.isPrimitiveType(simpleType) || PhpLanguageFeature.SCALAR_TYPE_HINTS.isSupported(element.getProject()) || "array".equalsIgnoreCase(simpleType) || "callable".equalsIgnoreCase(simpleType)) {
287-
String typeString = simpleType.endsWith("]") ? "array" : this.getFieldTypeString(element, filterNullCaseInsensitive(element.getType()));
304+
String typeString = simpleType.endsWith("]") ? "array" : this.getFieldTypeString(element, filterNullCaseInsensitive(element.getType()), globalType);
288305
if (!typeString.isEmpty()) {
289306
return typeString;
290307
}
@@ -293,8 +310,9 @@ private String convertTypeToString(@NotNull PhpNamedElement element, Set<String>
293310
return null;
294311
}
295312

296-
private String getFieldTypeString(PhpNamedElement element, @NotNull PhpType type) {
297-
return PhpDocUtil.getTypePresentation(this.pluginClass.getProject(), type, PhpCodeInsightUtil.findScopeForUseOperator(element));
313+
private String getFieldTypeString(PhpNamedElement element, @NotNull PhpType type, Boolean globalType) {
314+
PhpPsiElement scope = (globalType) ? PhpCodeInsightUtil.findScopeForUseOperator(element) : null;
315+
return PhpDocUtil.getTypePresentation(this.pluginClass.getProject(), type, scope);
298316
}
299317

300318
private static PhpType filterNullCaseInsensitive(PhpType filedType) {

src/com/magento/idea/magento2plugin/completion/xml/XmlCompletionContributor.java

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -215,51 +215,16 @@ public XmlCompletionContributor() {
215215
new RequireJsMappingCompletionProvider()
216216
);
217217

218-
// mftf selector completion contributor
219-
extend(CompletionType.BASIC,
220-
psiElement(XmlTokenType.XML_ATTRIBUTE_VALUE_TOKEN)
221-
.inside(XmlPatterns.xmlAttribute()
222-
.withName(MftfActionGroup.SELECTOR_ATTRIBUTE))
223-
.inFile(xmlFile().withName(string().endsWith("Test.xml"))),
224-
new SelectorCompletionProvider()
225-
);
226-
227218
// mftf action group completion contributor
228219
extend(
229220
CompletionType.BASIC,
230221
psiElement(XmlTokenType.XML_ATTRIBUTE_VALUE_TOKEN)
231222
.inside(
232223
XmlPatterns.xmlAttribute().withName(string().oneOf("ref", "extends"))
233224
.withParent(XmlPatterns.xmlTag().withName("actionGroup")
234-
)
235-
),
236-
new ActionGroupCompletionProvider()
237-
);
238-
239-
// mftf page url completion contributor
240-
extend(
241-
CompletionType.BASIC,
242-
psiElement(XmlTokenType.XML_ATTRIBUTE_VALUE_TOKEN)
243-
.inside(
244-
XmlPatterns.xmlAttribute().withName(MftfActionGroup.URL_ATTRIBUTE)
245-
.withParent(XmlPatterns.xmlTag().withParent(
246-
XmlPatterns.xmlTag().withName(
247-
string().oneOf(MftfActionGroup.ROOT_TAG, MftfTest.ROOT_TAG)
248-
)))
225+
)
249226
),
250-
new PageCompletionProvider()
251-
);
252-
extend(
253-
CompletionType.BASIC,
254-
psiElement(XmlTokenType.XML_ATTRIBUTE_VALUE_TOKEN)
255-
.inside(
256-
XmlPatterns.xmlAttribute().withName(MftfActionGroup.URL_ATTRIBUTE)
257-
.withParent(XmlPatterns.xmlTag().withParent(
258-
XmlPatterns.xmlTag().withParent(XmlPatterns.xmlTag().withName(
259-
string().oneOf(MftfActionGroup.ROOT_TAG, MftfTest.ROOT_TAG)
260-
))))
261-
),
262-
new PageCompletionProvider()
227+
new ActionGroupCompletionProvider()
263228
);
264229

265230
// mftf data entity completion contributor
@@ -282,5 +247,44 @@ public XmlCompletionContributor() {
282247
),
283248
new DataCompletionProvider()
284249
);
250+
251+
registerCompletionsForDifferentNesting();
252+
}
253+
254+
private void registerCompletionsForDifferentNesting() {
255+
int i = 1;
256+
int maxNesting = 10;
257+
while( i < maxNesting) {
258+
259+
// mftf selector completion contributor
260+
extend(CompletionType.BASIC,
261+
psiElement(XmlTokenType.XML_ATTRIBUTE_VALUE_TOKEN)
262+
.inside(XmlPatterns.xmlAttribute()
263+
.withName(MftfActionGroup.SELECTOR_ATTRIBUTE).withSuperParent(
264+
i,
265+
XmlPatterns.xmlTag().withParent(
266+
XmlPatterns.xmlTag().withName(
267+
string().oneOf(MftfActionGroup.ROOT_TAG, MftfTest.ROOT_TAG)
268+
)))
269+
),
270+
new SelectorCompletionProvider()
271+
);
272+
273+
// mftf page url completion contributor
274+
extend(
275+
CompletionType.BASIC,
276+
psiElement(XmlTokenType.XML_ATTRIBUTE_VALUE_TOKEN)
277+
.inside(
278+
XmlPatterns.xmlAttribute().withName(MftfActionGroup.URL_ATTRIBUTE)
279+
.withSuperParent(i ,XmlPatterns.xmlTag().withParent(
280+
XmlPatterns.xmlTag().withName(
281+
string().oneOf(MftfActionGroup.ROOT_TAG, MftfTest.ROOT_TAG)
282+
)))
283+
),
284+
new PageCompletionProvider()
285+
);
286+
287+
i++;
288+
}
285289
}
286290
}

src/com/magento/idea/magento2plugin/inspections/php/GraphQlResolverInspection.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.jetbrains.php.lang.inspections.PhpInspection;
1313
import com.jetbrains.php.lang.psi.elements.PhpClass;
1414
import com.jetbrains.php.lang.psi.visitors.PhpElementVisitor;
15+
import com.magento.idea.magento2plugin.bundles.InspectionBundle;
1516
import com.magento.idea.magento2plugin.inspections.php.fix.PhpImplementResolverClassQuickFix;
1617
import com.magento.idea.magento2plugin.util.magento.graphql.GraphQlUsagesCollector;
1718
import com.magento.idea.magento2plugin.util.magento.graphql.GraphQlUtil;
@@ -21,7 +22,7 @@
2122

2223
public class GraphQlResolverInspection extends PhpInspection {
2324

24-
public static final String GraphQlResolverProblemDescription = "Class must implements \\Magento\\Framework\\GraphQl\\Query\\ResolverInterface";
25+
private final InspectionBundle inspectionBundle = new InspectionBundle();
2526

2627
@NotNull
2728
@Override
@@ -32,11 +33,13 @@ public void visitPhpClass(PhpClass resolverClass) {
3233
GraphQlUsagesCollector collector = new GraphQlUsagesCollector();
3334
results = collector.getGraphQLUsages(resolverClass);
3435
if (results.size() > 0 ) {
35-
if (GraphQlUtil.isResolver(resolverClass)) {
36+
if (!GraphQlUtil.isResolver(resolverClass)) {
3637
PsiElement currentClassNameIdentifier = resolverClass.getNameIdentifier();
3738
assert currentClassNameIdentifier != null;
3839
problemsHolder.registerProblem(currentClassNameIdentifier,
39-
GraphQlResolverProblemDescription,
40+
inspectionBundle.message(
41+
"inspection.graphql.resolver.mustImplement"
42+
),
4043
ProblemHighlightType.ERROR,
4144
new PhpImplementResolverClassQuickFix());
4245
}

src/com/magento/idea/magento2plugin/inspections/php/PluginInspection.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.jetbrains.php.lang.psi.visitors.PhpElementVisitor;
2121
import com.magento.idea.magento2plugin.inspections.php.util.PhpClassImplementsInterfaceUtil;
2222
import com.magento.idea.magento2plugin.magento.files.Plugin;
23+
import com.magento.idea.magento2plugin.magento.packages.Package;
2324
import com.magento.idea.magento2plugin.util.GetPhpClassByFQN;
2425
import com.magento.idea.magento2plugin.util.magento.plugin.GetTargetClassNamesByPluginClassName;
2526
import org.jetbrains.annotations.NotNull;
@@ -128,7 +129,7 @@ private void checkParametersCompatibility(Method pluginMethod, String pluginPref
128129
String declaredType = pluginMethodParameter.getDeclaredType().toString();
129130

130131
if (index == 1) {
131-
String targetClassFqn = "\\".concat(targetClassName);
132+
String targetClassFqn = Package.FQN_SEPARATOR.concat(targetClassName);
132133
if (!checkTypeIncompatibility(targetClassFqn, declaredType, phpIndex)) {
133134
problemsHolder.registerProblem(pluginMethodParameter, PhpBundle.message("inspection.wrong_param_type", new Object[]{declaredType, targetClassFqn}), ProblemHighlightType.ERROR);
134135
}
@@ -138,7 +139,8 @@ private void checkParametersCompatibility(Method pluginMethod, String pluginPref
138139
continue;
139140
}
140141
if (index == 2 && pluginPrefix.equals(Plugin.PluginType.around.toString())) {
141-
if (!checkTypeIncompatibility("callable", declaredType, phpIndex)) {
142+
if (!checkTypeIncompatibility(Plugin.CALLABLE_PARAM, declaredType, phpIndex) &&
143+
!checkTypeIncompatibility(Package.FQN_SEPARATOR.concat(Plugin.CLOSURE_PARAM), declaredType, phpIndex)) {
142144
problemsHolder.registerProblem(pluginMethodParameter, PhpBundle.message("inspection.wrong_param_type", new Object[]{declaredType, "callable"}), ProblemHighlightType.ERROR);
143145
}
144146
continue;

src/com/magento/idea/magento2plugin/inspections/xml/ObserverDeclarationInspection.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ public void visitFile(PsiFile file) {
7171
}
7272

7373
List<XmlTag> targetObservers = fetchObserverTagsFromEventTag(eventXmlTag);
74+
if (targetObservers.isEmpty()) {
75+
continue;
76+
}
7477

7578
for (XmlTag observerXmlTag: targetObservers) {
7679
XmlAttribute observerNameAttribute = observerXmlTag.getAttribute("name");

src/com/magento/idea/magento2plugin/inspections/xml/PluginDeclarationInspection.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ public void visitFile(PsiFile file) {
9595
for (Pair<String, String> moduleEntry: modulesWithSamePluginName) {
9696
String scope = moduleEntry.getFirst();
9797
String moduleName = moduleEntry.getSecond();
98+
if (scope == null || moduleName == null) {
99+
continue;
100+
}
98101
String problemKey = pluginTypeKey.concat(Package.VENDOR_MODULE_NAME_SEPARATOR)
99102
.concat(moduleName).concat(Package.VENDOR_MODULE_NAME_SEPARATOR).concat(scope);
100103
if (!pluginProblems.containsKey(problemKey)){

src/com/magento/idea/magento2plugin/linemarker/php/GraphQlResolverUsageLineMarkerProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void collectSlowLineMarkers(@NotNull List<PsiElement> psiElements, @NotNu
3939
if (psiElement instanceof PhpClass) {
4040
List<? extends PsiElement> results;
4141

42-
if (GraphQlUtil.isResolver((PhpClass) psiElement)) {
42+
if (!GraphQlUtil.isResolver((PhpClass) psiElement)) {
4343
return;
4444
}
4545
GraphQlUsagesCollector collector = new GraphQlUsagesCollector();

src/com/magento/idea/magento2plugin/magento/files/Plugin.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ public class Plugin implements ModuleFileInterface {
1212
private static final String AROUND_METHOD_TEMPLATE_NAME = "Magento Plugin Around Method";
1313
private static final String AFTER_METHOD_TEMPLATE_NAME = "Magento Plugin After Method";
1414

15+
public static final String CALLABLE_PARAM = "callable";
16+
public static final String CLOSURE_PARAM = "Closure";
17+
1518
public enum PluginType {
1619
before,
1720
after,

src/com/magento/idea/magento2plugin/reference/xml/XmlReferenceContributor.java

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -170,34 +170,6 @@ public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar)
170170
)
171171
);
172172

173-
// <someXmlTag url="{{someValue}}" /> in MFTF Tests and ActionGroups
174-
registrar.registerReferenceProvider(
175-
XmlPatterns.xmlAttributeValue().withValue(
176-
string().matches(RegExUtil.Magento.MFTF_CURLY_BRACES)
177-
).withParent(XmlPatterns.xmlAttribute().withName(
178-
MftfActionGroup.URL_ATTRIBUTE
179-
).withParent(XmlPatterns.xmlTag().withParent(XmlPatterns.xmlTag().withName(
180-
string().oneOf(MftfActionGroup.ROOT_TAG, MftfTest.ROOT_TAG)
181-
)))),
182-
new CompositeReferenceProvider(
183-
new PageReferenceProvider()
184-
)
185-
);
186-
registrar.registerReferenceProvider(
187-
XmlPatterns.xmlAttributeValue().withValue(
188-
string().matches(RegExUtil.Magento.MFTF_CURLY_BRACES)
189-
).withParent(XmlPatterns.xmlAttribute().withName(
190-
MftfActionGroup.URL_ATTRIBUTE
191-
).withParent(XmlPatterns.xmlTag().withParent(XmlPatterns.xmlTag().withParent(
192-
XmlPatterns.xmlTag().withName(
193-
string().oneOf(MftfActionGroup.ROOT_TAG, MftfTest.ROOT_TAG)
194-
)
195-
)))),
196-
new CompositeReferenceProvider(
197-
new PageReferenceProvider()
198-
)
199-
);
200-
201173
// <createData entity="SimpleProduct" />
202174
// <updateData entity="SimpleProduct" />
203175
registrar.registerReferenceProvider(
@@ -273,5 +245,44 @@ public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar)
273245
),
274246
new RequireJsPreferenceReferenceProvider()
275247
);
248+
249+
registerReferenceForDifferentNesting(registrar);
250+
}
251+
252+
private void registerReferenceForDifferentNesting(@NotNull PsiReferenceRegistrar registrar) {
253+
int i = 1;
254+
int maxNesting = 10;
255+
while( i < maxNesting) {
256+
257+
// <someXmlTag url="{{someValue}}" /> in MFTF Tests and ActionGroups
258+
registrar.registerReferenceProvider(
259+
XmlPatterns.xmlAttributeValue().withValue(
260+
string().matches(RegExUtil.Magento.MFTF_CURLY_BRACES)
261+
).withParent(XmlPatterns.xmlAttribute().withName(
262+
MftfActionGroup.URL_ATTRIBUTE
263+
).withParent(XmlPatterns.xmlTag().withSuperParent(i, XmlPatterns.xmlTag().withName(
264+
string().oneOf(MftfActionGroup.ROOT_TAG, MftfTest.ROOT_TAG)
265+
)))),
266+
new CompositeReferenceProvider(
267+
new PageReferenceProvider()
268+
)
269+
);
270+
271+
// <someXmlTag selector="{{someValue}}" /> in MFTF Tests and ActionGroups
272+
registrar.registerReferenceProvider(
273+
XmlPatterns.xmlAttributeValue().withValue(
274+
string().matches(RegExUtil.Magento.MFTF_CURLY_BRACES)
275+
).withParent(XmlPatterns.xmlAttribute().withName(
276+
MftfActionGroup.SELECTOR_ATTRIBUTE
277+
).withParent(XmlPatterns.xmlTag().withSuperParent(i, XmlPatterns.xmlTag().withName(
278+
string().oneOf(MftfActionGroup.ROOT_TAG, MftfTest.ROOT_TAG)
279+
)))),
280+
new CompositeReferenceProvider(
281+
new SectionReferenceProvider()
282+
)
283+
);
284+
285+
i++;
286+
}
276287
}
277288
}

0 commit comments

Comments
 (0)