Skip to content

Commit 6053597

Browse files
committed
reduce public api for TwigExtension
1 parent 56e0020 commit 6053597

File tree

3 files changed

+45
-31
lines changed

3 files changed

+45
-31
lines changed

src/fr/adrienbrault/idea/symfony2plugin/templating/dict/TwigCreateContainer.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package fr.adrienbrault.idea.symfony2plugin.templating.dict;
22

3+
import org.jetbrains.annotations.NotNull;
34
import org.jetbrains.annotations.Nullable;
45

56
import java.util.*;
@@ -8,19 +9,21 @@
89
* @author Daniel Espendiller <daniel@espendiller.net>
910
*/
1011
public class TwigCreateContainer {
12+
@NotNull
13+
final private Map<String, Integer> extendHap = new HashMap<>();
1114

12-
private Map<String, Integer> extendHap = new HashMap<>();
13-
private Map<String, Integer> blockHap = new HashMap<>();
15+
@NotNull
16+
final private Map<String, Integer> blockHap = new HashMap<>();
1417

15-
public void addExtend(String extend) {
18+
public void addExtend(@NotNull String extend) {
1619
if(extendHap.containsKey(extend)) {
1720
extendHap.put(extend, extendHap.get(extend) + 1);
1821
} else {
1922
extendHap.put(extend, 1);
2023
}
2124
}
2225

23-
public void addBlock(String block) {
26+
public void addBlock(@NotNull String block) {
2427
if(blockHap.containsKey(block)) {
2528
blockHap.put(block, blockHap.get(block) + 1);
2629
} else {
@@ -30,28 +33,32 @@ public void addBlock(String block) {
3033

3134
@Nullable
3235
public String getExtend() {
33-
Map<String, Integer> extendsMap = this.getExtends();
36+
Map<String, Integer> extendsMap = getExtends();
3437
return extendsMap.size() > 0 ? extendsMap.keySet().iterator().next() : null;
3538
}
3639

37-
public Map<String, Integer> getExtends() {
40+
@NotNull
41+
private Map<String, Integer> getExtends() {
3842
return sortByValue(extendHap);
3943
}
4044

41-
public Map<String, Integer> getBlocks() {
45+
@NotNull
46+
private Map<String, Integer> getBlocks() {
4247
return sortByValue(blockHap);
4348
}
4449

50+
@NotNull
4551
public Collection<String> getBlockNames(int limit) {
46-
4752
List<String> strings = new ArrayList<>(getBlocks().keySet());
53+
4854
if(strings.size() > limit) {
4955
strings = strings.subList(0, limit);
5056
}
5157

5258
return strings;
5359
}
5460

61+
@NotNull
5562
private static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map )
5663
{
5764
List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
@@ -66,5 +73,4 @@ private static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K,
6673

6774
return result;
6875
}
69-
7076
}

src/fr/adrienbrault/idea/symfony2plugin/templating/dict/TwigExtension.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,28 @@
1212
*/
1313
public class TwigExtension {
1414
@Nullable
15-
private String signature = null;
15+
final private String signature;
1616

1717
@NotNull
18-
private TwigExtensionParser.TwigExtensionType twigExtensionType;
18+
final private TwigExtensionParser.TwigExtensionType twigExtensionType;
1919

2020
@NotNull
21-
private Map<String, String> options = new HashMap<>();
21+
final private Map<String, String> options = new HashMap<>();
2222

2323
public TwigExtension(@NotNull TwigExtensionParser.TwigExtensionType twigExtensionType) {
24-
this.twigExtensionType = twigExtensionType;
24+
this(twigExtensionType, null);
2525
}
2626

2727
public TwigExtension(@NotNull TwigExtensionParser.TwigExtensionType twigExtensionType, @Nullable String signature) {
28-
this(twigExtensionType);
28+
this.twigExtensionType = twigExtensionType;
2929
this.signature = signature;
3030
}
3131

32+
public TwigExtension(@NotNull TwigExtensionParser.TwigExtensionType twigExtensionType, @Nullable String signature, @NotNull Map<String, String> options) {
33+
this(twigExtensionType, signature);
34+
this.options.putAll(options);
35+
}
36+
3237
@NotNull
3338
public TwigExtensionParser.TwigExtensionType getTwigExtensionType() {
3439
return twigExtensionType;
@@ -44,14 +49,8 @@ public String getSignature() {
4449
return signature;
4550
}
4651

47-
@NotNull
48-
public TwigExtension putOption(@NotNull String key, @NotNull String value) {
49-
options.put(key, value);
50-
return this;
51-
}
52-
5352
@Nullable
54-
public String getOption(String key) {
55-
return options.containsKey(key) ? options.get(key) : null;
53+
String getOption(String key) {
54+
return options.getOrDefault(key, null);
5655
}
5756
}

src/fr/adrienbrault/idea/symfony2plugin/templating/util/TwigExtensionParser.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -404,13 +404,15 @@ private void visitNewExpression(@NotNull NewExpression element) {
404404
signature = getCallableSignature(psiElement[1], method);
405405
}
406406

407-
TwigExtension twigExtension = new TwigExtension(TwigExtensionType.FILTER, signature);
408-
407+
// creation options like: needs_environment
408+
Map<String, String> options;
409409
if(psiElement.length > 2 && psiElement[2] instanceof ArrayCreationExpression) {
410-
decorateOptions((ArrayCreationExpression) psiElement[2], twigExtension);
410+
options = getOptions((ArrayCreationExpression) psiElement[2]);
411+
} else {
412+
options = new HashMap<>();
411413
}
412414

413-
filters.put(funcName, twigExtension);
415+
filters.put(funcName, new TwigExtension(TwigExtensionType.FILTER, signature, options));
414416
}
415417
}
416418

@@ -474,16 +476,20 @@ private void visitNewExpression(@NotNull NewExpression element) {
474476
/**
475477
* Add needs_environment, needs_context values to twig extension object
476478
*/
477-
private static void decorateOptions(@NotNull ArrayCreationExpression arrayCreationExpression, @NotNull TwigExtension twigExtension) {
479+
static private Map<String, String> getOptions(@NotNull ArrayCreationExpression arrayCreationExpression) {
480+
Map<String, String> options = new HashMap<>();
481+
478482
for(String optionTrue: new String[] {"needs_environment", "needs_context"}) {
479483
PhpPsiElement phpPsiElement = PhpElementsUtil.getArrayValue(arrayCreationExpression, optionTrue);
480484
if(phpPsiElement instanceof ConstantReference) {
481485
String value = phpPsiElement.getName();
482486
if(value != null && value.toLowerCase().equals("true")) {
483-
twigExtension.putOption(optionTrue, "true");
487+
options.put(optionTrue, "true");
484488
}
485489
}
486490
}
491+
492+
return options;
487493
}
488494

489495
private static class TwigFunctionVisitor extends PsiRecursiveElementWalkingVisitor {
@@ -524,12 +530,15 @@ private void visitNewExpression(@NotNull NewExpression element) {
524530
signature = getCallableSignature(psiElement[1], method);
525531
}
526532

527-
TwigExtension twigExtension = new TwigExtension(TwigExtensionType.SIMPLE_FUNCTION, signature);
533+
// creation options like: needs_environment
534+
Map<String, String> options;
528535
if(psiElement.length > 2 && psiElement[2] instanceof ArrayCreationExpression) {
529-
decorateOptions((ArrayCreationExpression) psiElement[2], twigExtension);
536+
options = getOptions((ArrayCreationExpression) psiElement[2]);
537+
} else {
538+
options = new HashMap<>();
530539
}
531540

532-
filters.put(funcName, twigExtension);
541+
filters.put(funcName, new TwigExtension(TwigExtensionType.SIMPLE_FUNCTION, signature, options));
533542
}
534543
}
535544

0 commit comments

Comments
 (0)