Skip to content

Commit becfcfd

Browse files
committed
Improved tests logic
1 parent 4bf8869 commit becfcfd

8 files changed

+123
-85
lines changed

Diff for: src/com/magento/idea/magento2plugin/reference/provider/EventDispatchReferenceProvider.java

+15-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright © Magento, Inc. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
5+
56
package com.magento.idea.magento2plugin.reference.provider;
67

78
import com.intellij.ide.highlighter.XmlFileType;
@@ -13,17 +14,22 @@
1314
import com.intellij.util.ProcessingContext;
1415
import com.magento.idea.magento2plugin.indexes.EventIndex;
1516
import com.magento.idea.magento2plugin.reference.xml.PolyVariantReferenceBase;
16-
import org.jetbrains.annotations.NotNull;
17-
17+
import java.util.ArrayList;
1818
import java.util.Collection;
19+
import java.util.List;
20+
import org.jetbrains.annotations.NotNull;
1921

2022
public class EventDispatchReferenceProvider extends PsiReferenceProvider {
2123

2224
@NotNull
2325
@Override
24-
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
25-
String value = StringUtil.unquoteString(element.getText());
26-
Collection<PsiElement> targets = EventIndex.getInstance(element.getProject())
26+
public PsiReference[] getReferencesByElement(
27+
@NotNull final PsiElement element,
28+
@NotNull final ProcessingContext context
29+
) {
30+
final String value = StringUtil.unquoteString(element.getText());
31+
final List<PsiReference> psiReferences = new ArrayList<>();
32+
final Collection<PsiElement> targets = EventIndex.getInstance(element.getProject())
2733
.getEventElements(
2834
value,
2935
GlobalSearchScope.getScopeRestrictedByFileTypes(
@@ -32,9 +38,10 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNu
3238
)
3339
);
3440

35-
if (targets.size() > 0) {
36-
return new PsiReference[] {new PolyVariantReferenceBase(element, targets)};
41+
if (!targets.isEmpty()) {
42+
psiReferences.add(new PolyVariantReferenceBase(element, targets));
3743
}
38-
return PsiReference.EMPTY_ARRAY;
44+
45+
return psiReferences.toArray(new PsiReference[0]);
3946
}
4047
}

Diff for: src/com/magento/idea/magento2plugin/reference/provider/LayoutBlockReferenceProvider.java

+15-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright © Magento, Inc. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
5+
56
package com.magento.idea.magento2plugin.reference.provider;
67

78
import com.intellij.openapi.util.text.StringUtil;
@@ -12,20 +13,26 @@
1213
import com.intellij.util.ProcessingContext;
1314
import com.magento.idea.magento2plugin.indexes.LayoutIndex;
1415
import com.magento.idea.magento2plugin.reference.xml.PolyVariantReferenceBase;
15-
import org.jetbrains.annotations.NotNull;
16-
16+
import java.util.ArrayList;
1717
import java.util.List;
18+
import org.jetbrains.annotations.NotNull;
1819

1920
public class LayoutBlockReferenceProvider extends PsiReferenceProvider {
2021

2122
@NotNull
2223
@Override
23-
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
24-
String value = StringUtil.unquoteString(element.getText());
25-
List<XmlTag> targets = LayoutIndex.getBlockDeclarations(value, element.getProject());
26-
if (targets.size() > 0) {
27-
return new PsiReference[] {new PolyVariantReferenceBase(element, targets)};
24+
public PsiReference[] getReferencesByElement(
25+
@NotNull final PsiElement element,
26+
@NotNull final ProcessingContext context
27+
) {
28+
final String value = StringUtil.unquoteString(element.getText());
29+
final List<XmlTag> targets = LayoutIndex.getBlockDeclarations(value, element.getProject());
30+
final List<PsiReference> psiReferences = new ArrayList<>();
31+
32+
if (!targets.isEmpty()) {
33+
psiReferences.add(new PolyVariantReferenceBase(element, targets));
2834
}
29-
return PsiReference.EMPTY_ARRAY;
35+
36+
return psiReferences.toArray(new PsiReference[0]);
3037
}
3138
}

Diff for: src/com/magento/idea/magento2plugin/reference/provider/LayoutContainerReferenceProvider.java

+16-11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright © Magento, Inc. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
5+
56
package com.magento.idea.magento2plugin.reference.provider;
67

78
import com.intellij.openapi.util.text.StringUtil;
@@ -12,23 +13,27 @@
1213
import com.intellij.util.ProcessingContext;
1314
import com.magento.idea.magento2plugin.indexes.LayoutIndex;
1415
import com.magento.idea.magento2plugin.reference.xml.PolyVariantReferenceBase;
15-
import org.jetbrains.annotations.NotNull;
16-
16+
import java.util.ArrayList;
1717
import java.util.List;
18+
import org.jetbrains.annotations.NotNull;
1819

19-
/**
20-
* Created by dkvashnin on 11/18/15.
21-
*/
2220
public class LayoutContainerReferenceProvider extends PsiReferenceProvider {
2321

2422
@NotNull
2523
@Override
26-
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
27-
String value = StringUtil.unquoteString(element.getText());
28-
List<XmlTag> targets = LayoutIndex.getContainerDeclarations(value, element.getProject());
29-
if (targets.size() > 0) {
30-
return new PsiReference[] {new PolyVariantReferenceBase(element, targets)};
24+
public PsiReference[] getReferencesByElement(
25+
@NotNull final PsiElement element,
26+
@NotNull final ProcessingContext context
27+
) {
28+
final String value = StringUtil.unquoteString(element.getText());
29+
final List<XmlTag> targets
30+
= LayoutIndex.getContainerDeclarations(value, element.getProject());
31+
final List<PsiReference> psiReferences = new ArrayList<>();
32+
33+
if (!targets.isEmpty()) {
34+
psiReferences.add(new PolyVariantReferenceBase(element, targets));
3135
}
32-
return PsiReference.EMPTY_ARRAY;
36+
37+
return psiReferences.toArray(new PsiReference[0]);
3338
}
3439
}

Diff for: src/com/magento/idea/magento2plugin/reference/provider/LayoutUpdateReferenceProvider.java

+15-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright © Magento, Inc. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
5+
56
package com.magento.idea.magento2plugin.reference.provider;
67

78
import com.intellij.openapi.util.text.StringUtil;
@@ -12,20 +13,26 @@
1213
import com.intellij.util.ProcessingContext;
1314
import com.magento.idea.magento2plugin.indexes.LayoutIndex;
1415
import com.magento.idea.magento2plugin.reference.xml.PolyVariantReferenceBase;
15-
import org.jetbrains.annotations.NotNull;
16-
16+
import java.util.ArrayList;
1717
import java.util.List;
18+
import org.jetbrains.annotations.NotNull;
1819

1920
public class LayoutUpdateReferenceProvider extends PsiReferenceProvider {
2021

2122
@NotNull
2223
@Override
23-
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
24-
String value = StringUtil.unquoteString(element.getText());
25-
List<XmlFile> targets = LayoutIndex.getLayoutFiles(element.getProject(), value);
26-
if (targets.size() > 0) {
27-
return new PsiReference[] {new PolyVariantReferenceBase(element, targets)};
24+
public PsiReference[] getReferencesByElement(
25+
@NotNull final PsiElement element,
26+
@NotNull final ProcessingContext context
27+
) {
28+
final String value = StringUtil.unquoteString(element.getText());
29+
final List<XmlFile> targets = LayoutIndex.getLayoutFiles(element.getProject(), value);
30+
final List<PsiReference> psiReferences = new ArrayList<>();
31+
32+
if (!targets.isEmpty()) {
33+
psiReferences.add(new PolyVariantReferenceBase(element, targets));
2834
}
29-
return PsiReference.EMPTY_ARRAY;
35+
36+
return psiReferences.toArray(new PsiReference[0]);
3037
}
3138
}

Diff for: src/com/magento/idea/magento2plugin/reference/provider/PhpClassMemberReferenceProvider.java

+19-14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright © Magento, Inc. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
5+
56
package com.magento.idea.magento2plugin.reference.provider;
67

78
import com.intellij.openapi.util.TextRange;
@@ -16,46 +17,50 @@
1617
import com.magento.idea.magento2plugin.reference.xml.PolyVariantReferenceBase;
1718
import com.magento.idea.magento2plugin.util.RegExUtil;
1819
import gnu.trove.THashSet;
19-
import org.jetbrains.annotations.NotNull;
2020
import java.util.ArrayList;
2121
import java.util.Collection;
2222
import java.util.List;
2323
import java.util.regex.Matcher;
2424
import java.util.regex.Pattern;
25+
import org.jetbrains.annotations.NotNull;
2526

2627
public class PhpClassMemberReferenceProvider extends PsiReferenceProvider {
2728

2829
@NotNull
2930
@Override
30-
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
31-
List<PsiReference> psiReferences = new ArrayList<>();
32-
final Collection<PhpClassMember> members = new THashSet<>();
31+
public PsiReference[] getReferencesByElement(
32+
@NotNull final PsiElement element,
33+
@NotNull final ProcessingContext context
34+
) {
35+
final String value = StringUtil.unquoteString(element.getText());
36+
final Matcher matcher
37+
= Pattern.compile(RegExUtil.XmlRegex.CLASS_MEMBER_NAME).matcher(value);
3338

34-
String origValue = element.getText();
35-
String value = StringUtil.unquoteString(element.getText());
36-
37-
Matcher matcher = Pattern.compile(RegExUtil.XmlRegex.CLASS_MEMBER_NAME).matcher(value);
3839
if (!matcher.find()) {
3940
return PsiReference.EMPTY_ARRAY;
4041
}
4142

42-
String elementName = matcher.group(1);
43-
String classFQN = value.substring(0, value.lastIndexOf("::"));
43+
final List<PsiReference> psiReferences = new ArrayList<>();
44+
final Collection<PhpClassMember> members = new THashSet<>();
45+
final String elementName = matcher.group(1);
46+
final String classFQN = value.substring(0, value.lastIndexOf("::"));
47+
final PhpIndex phpIndex = PhpIndex.getInstance(element.getProject());
4448

45-
PhpIndex phpIndex = PhpIndex.getInstance(element.getProject());
4649
for (final PhpClass phpClass : phpIndex.getAnyByFQN(classFQN)) {
4750
members.addAll(phpClass.getFields());
4851
members.addAll(phpClass.getMethods());
4952
members.removeIf(c -> !c.getName().equals(elementName));
5053
}
5154

52-
if (members.size() > 0) {
53-
TextRange range = new TextRange(
55+
if (!members.isEmpty()) {
56+
final String origValue = element.getText();
57+
final TextRange range = new TextRange(
5458
origValue.indexOf(elementName),
5559
origValue.indexOf(elementName) + elementName.length()
5660
);
5761
psiReferences.add(new PolyVariantReferenceBase(element, range, members));
5862
}
59-
return psiReferences.toArray(new PsiReference[psiReferences.size()]);
63+
64+
return psiReferences.toArray(new PsiReference[0]);
6065
}
6166
}

Diff for: src/com/magento/idea/magento2plugin/reference/provider/PhpConstructorArgumentReferenceProvider.java

+21-19
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright © Magento, Inc. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
5+
56
package com.magento.idea.magento2plugin.reference.provider;
67

78
import com.intellij.openapi.util.text.StringUtil;
@@ -16,36 +17,37 @@
1617
import com.magento.idea.magento2plugin.indexes.DiIndex;
1718
import com.magento.idea.magento2plugin.reference.xml.PolyVariantReferenceBase;
1819
import gnu.trove.THashSet;
19-
import org.jetbrains.annotations.NotNull;
20-
20+
import java.util.ArrayList;
2121
import java.util.Arrays;
2222
import java.util.Collection;
23+
import java.util.List;
24+
import org.jetbrains.annotations.NotNull;
2325

2426
public class PhpConstructorArgumentReferenceProvider extends PsiReferenceProvider {
2527

2628
@NotNull
2729
@Override
28-
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
29-
30-
String parameterName = StringUtil.unquoteString(element.getText());
31-
if (parameterName.isEmpty() || !(element instanceof XmlElement)) {
32-
return PsiReference.EMPTY_ARRAY;
33-
}
34-
35-
36-
DiIndex diIndex = DiIndex.getInstance(element.getProject());
37-
PhpClass phpClass = diIndex.getPhpClassOfArgument((XmlElement) element);
38-
if (phpClass != null) {
39-
Method constructor = phpClass.getConstructor();
40-
if (constructor != null) {
41-
Collection<Parameter> parameterList = new THashSet<>(Arrays.asList(constructor.getParameters()));
30+
public PsiReference[] getReferencesByElement(
31+
@NotNull final PsiElement element,
32+
@NotNull final ProcessingContext context
33+
) {
34+
final List<PsiReference> psiReferences = new ArrayList<>();
35+
final String parameterName = StringUtil.unquoteString(element.getText());
36+
37+
if (!parameterName.isEmpty()) {
38+
final DiIndex diIndex = DiIndex.getInstance(element.getProject());
39+
final PhpClass phpClass = diIndex.getPhpClassOfArgument((XmlElement) element);
40+
if (phpClass != null && phpClass.getConstructor() != null) {
41+
final Method constructor = phpClass.getConstructor();
42+
final Collection<Parameter> parameterList
43+
= new THashSet<>(Arrays.asList(constructor.getParameters()));
4244
parameterList.removeIf(p -> !p.getName().contains(parameterName));
43-
if (parameterList.size() > 0) {
44-
return new PsiReference[] {new PolyVariantReferenceBase(element, parameterList)};
45+
if (!parameterList.isEmpty()) {// NOPMD
46+
psiReferences.add(new PolyVariantReferenceBase(element, parameterList));
4547
}
4648
}
4749
}
4850

49-
return PsiReference.EMPTY_ARRAY;
51+
return psiReferences.toArray(new PsiReference[0]);
5052
}
5153
}

Diff for: src/com/magento/idea/magento2plugin/reference/provider/UIComponentReferenceProvider.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
import com.intellij.util.ProcessingContext;
1414
import com.magento.idea.magento2plugin.indexes.UIComponentIndex;
1515
import com.magento.idea.magento2plugin.reference.xml.PolyVariantReferenceBase;
16+
import java.util.ArrayList;
1617
import java.util.List;
1718
import org.jetbrains.annotations.NotNull;
1819

19-
2020
public class UIComponentReferenceProvider extends PsiReferenceProvider {
2121

2222
@NotNull
@@ -30,9 +30,12 @@ public PsiReference[] getReferencesByElement(
3030
element.getProject(),
3131
value
3232
);
33+
final List<PsiReference> psiReferences = new ArrayList<>();
34+
3335
if (!targets.isEmpty()) {
34-
return new PsiReference[] {new PolyVariantReferenceBase(element, targets)};
36+
psiReferences.add(new PolyVariantReferenceBase(element, targets));
3537
}
36-
return PsiReference.EMPTY_ARRAY;
38+
39+
return psiReferences.toArray(new PsiReference[0]);
3740
}
3841
}

Diff for: src/com/magento/idea/magento2plugin/reference/provider/VirtualTypeReferenceProvider.java

+16-14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright © Magento, Inc. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
5+
56
package com.magento.idea.magento2plugin.reference.provider;
67

78
import com.intellij.openapi.util.text.StringUtil;
@@ -11,27 +12,28 @@
1112
import com.intellij.util.ProcessingContext;
1213
import com.magento.idea.magento2plugin.indexes.DiIndex;
1314
import com.magento.idea.magento2plugin.reference.xml.PolyVariantReferenceBase;
14-
import org.jetbrains.annotations.NotNull;
15-
15+
import java.util.ArrayList;
1616
import java.util.Collection;
17+
import java.util.List;
18+
import org.jetbrains.annotations.NotNull;
1719

1820
public class VirtualTypeReferenceProvider extends PsiReferenceProvider {
1921

2022
@NotNull
2123
@Override
22-
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
23-
24-
String value = StringUtil.unquoteString(element.getText());
25-
26-
DiIndex index = DiIndex.getInstance(element.getProject());
27-
Collection<PsiElement> targets = index.getVirtualTypeElements(value, element.getResolveScope());
28-
29-
if (!(targets.size() > 0)) {
30-
return PsiReference.EMPTY_ARRAY;
24+
public PsiReference[] getReferencesByElement(
25+
@NotNull final PsiElement element,
26+
@NotNull final ProcessingContext context
27+
) {
28+
final String value = StringUtil.unquoteString(element.getText());
29+
final DiIndex index = DiIndex.getInstance(element.getProject());
30+
final Collection<PsiElement> targets = index.getVirtualTypeElements(value, element.getResolveScope());
31+
final List<PsiReference> psiReferences = new ArrayList<>();
32+
33+
if (!targets.isEmpty()) {
34+
psiReferences.add(new PolyVariantReferenceBase(element, targets));
3135
}
3236

33-
return new PsiReference[] {
34-
new PolyVariantReferenceBase(element, targets)
35-
};
37+
return psiReferences.toArray(new PsiReference[0]);
3638
}
3739
}

0 commit comments

Comments
 (0)