2
2
* Copyright © Magento, Inc. All rights reserved.
3
3
* See COPYING.txt for license details.
4
4
*/
5
+
5
6
package com .magento .idea .magento2plugin .magento .packages ;
6
7
7
- import com .intellij .json .psi .*;
8
- import org .apache .commons .lang .StringUtils ;
9
- import org .jetbrains .annotations .NotNull ;
10
- import org .jetbrains .annotations .Nullable ;
8
+ import com .intellij .json .psi .JsonArray ;
9
+ import com .intellij .json .psi .JsonObject ;
10
+ import com .intellij .json .psi .JsonProperty ;
11
+ import com .intellij .json .psi .JsonStringLiteral ;
12
+ import com .intellij .json .psi .JsonValue ;
11
13
import java .util .ArrayList ;
12
14
import java .util .HashMap ;
13
15
import java .util .List ;
14
16
import java .util .Map ;
17
+ import org .apache .commons .lang3 .StringUtils ;
18
+ import org .jetbrains .annotations .NotNull ;
19
+ import org .jetbrains .annotations .Nullable ;
15
20
16
21
public class ComposerPackageModelImpl implements ComposerPackageModel {
17
- private JsonObject sourceComposerJson ;
22
+ private final JsonObject sourceComposerJson ;
23
+ private static final int VENDOR_AND_PACKAGE_PARTS_LENGTH = 2 ;
18
24
19
25
public static final String NAME = "name" ;
20
26
public static final String TYPE = "type" ;
@@ -23,7 +29,7 @@ public class ComposerPackageModelImpl implements ComposerPackageModel {
23
29
public static final String PSR4 = "psr4" ;
24
30
public static final String FILES = "file" ;
25
31
26
- public ComposerPackageModelImpl (@ NotNull JsonObject sourceComposerJson ) {
32
+ public ComposerPackageModelImpl (@ NotNull final JsonObject sourceComposerJson ) {
27
33
this .sourceComposerJson = sourceComposerJson ;
28
34
}
29
35
@@ -42,10 +48,10 @@ public String getType() {
42
48
@ Nullable
43
49
@ Override
44
50
public String getVendor () {
45
- String nameProperty = getStringPropertyValue (NAME );
51
+ final String nameProperty = getStringPropertyValue (NAME );
46
52
if (nameProperty != null ) {
47
- String [] vendorAndPackage = nameProperty .split ("/" );
48
- if (vendorAndPackage .length == 2 ) {
53
+ final String [] vendorAndPackage = nameProperty .split ("/" );
54
+ if (vendorAndPackage .length == VENDOR_AND_PACKAGE_PARTS_LENGTH ) {
49
55
return vendorAndPackage [0 ];
50
56
}
51
57
}
@@ -62,63 +68,69 @@ public String getVersion() {
62
68
@ Nullable
63
69
@ Override
64
70
public String [] getAutoloadFiles () {
65
- JsonObject autoloadObject = getPropertyValueOfType (AUTOLOAD , JsonObject .class );
71
+ final JsonObject autoloadObject = getPropertyValueOfType (AUTOLOAD , JsonObject .class );
66
72
if (autoloadObject != null ) {
67
- JsonArray jsonArray = getPropertyValueOfType (FILES , JsonArray .class );
68
- if (jsonArray != null ) {
69
- List <String > files = new ArrayList <>();
70
- for (JsonValue value : jsonArray .getValueList ()) {
71
- if (value instanceof JsonStringLiteral ) {
72
- files .add (StringUtils .strip (value .getText (), "\" " ));
73
- }
73
+ return new String [0 ];
74
+ }
75
+
76
+ final JsonArray jsonArray = getPropertyValueOfType (FILES , JsonArray .class );
77
+ if (jsonArray != null ) {
78
+ final List <String > files = new ArrayList <>();
79
+ for (final JsonValue value : jsonArray .getValueList ()) {
80
+ if (value instanceof JsonStringLiteral ) {
81
+ files .add (StringUtils .strip (value .getText (), "\" " ));
74
82
}
75
- return files .size () > 0 ? files .toArray (new String [files .size ()]) : null ;
76
83
}
84
+ return files .isEmpty () ? new String [0 ] : files .toArray (new String [0 ]);
77
85
}
78
86
79
- return null ;
87
+ return new String [ 0 ] ;
80
88
}
81
89
82
90
@ Nullable
83
91
@ Override
84
92
public Map <String , String > getAutoloadPsr4 () {
85
- JsonObject autoloadObject = getPropertyValueOfType (AUTOLOAD , JsonObject .class );
86
- if (autoloadObject != null ) {
87
- JsonObject jsonObject = getPropertyValueOfType (PSR4 , JsonObject .class );
88
- if (jsonObject != null ) {
89
- Map <String , String > map = new HashMap <String , String >();
90
- for (JsonProperty property : jsonObject .getPropertyList ()) {
91
- JsonValue value = property .getValue ();
92
-
93
- if (value != null && value instanceof JsonStringLiteral ) {
94
- map .put (property .getName (), StringUtils .strip (value .getText (), "\" " ));
95
- }
96
- }
93
+ final JsonObject autoloadObject = getPropertyValueOfType (AUTOLOAD , JsonObject .class );
94
+ final Map <String , String > map = new HashMap <>();
95
+ if (autoloadObject == null ) {
96
+ return map ;
97
+ }
97
98
98
- return map .size () > 0 ? map : null ;
99
+ final JsonObject jsonObject = getPropertyValueOfType (PSR4 , JsonObject .class );
100
+ if (jsonObject != null ) {
101
+ for (final JsonProperty property : jsonObject .getPropertyList ()) {
102
+ final JsonValue value = property .getValue ();
103
+ if (value instanceof JsonStringLiteral ) {
104
+ map .put (property .getName (), StringUtils .strip (value .getText (), "\" " ));
105
+ }
99
106
}
100
107
}
101
108
102
- return null ;
109
+ return map ;
103
110
}
104
111
105
112
@ Nullable
106
- public <T extends JsonValue > T getPropertyValueOfType (String propertyName , @ NotNull Class <T > aClass ) {
107
- JsonProperty property = sourceComposerJson .findProperty (propertyName );
113
+ @ Override
114
+ public <T extends JsonValue > T getPropertyValueOfType (final String propertyName ,
115
+ @ NotNull final Class <T > thisClass ) {
116
+ final JsonProperty property = sourceComposerJson .findProperty (propertyName );
108
117
if (property == null ) {
109
118
return null ;
110
119
}
111
- JsonValue value = property .getValue ();
112
- if (value != null && aClass .isInstance (value )) {
113
- return aClass .cast (value );
120
+ final JsonValue value = property .getValue ();
121
+ if (thisClass .isInstance (value )) {
122
+ return thisClass .cast (value );
114
123
}
115
124
116
125
return null ;
117
126
}
118
127
119
128
@ Nullable
120
- private String getStringPropertyValue (String propertyName ) {
121
- JsonStringLiteral stringLiteral = getPropertyValueOfType (propertyName , JsonStringLiteral .class );
129
+ private String getStringPropertyValue (final String propertyName ) {
130
+ final JsonStringLiteral stringLiteral = getPropertyValueOfType (
131
+ propertyName ,
132
+ JsonStringLiteral .class
133
+ );
122
134
123
135
if (stringLiteral != null ) {
124
136
return StringUtils .strip (stringLiteral .getText (), "\" " );
0 commit comments