22 * Copyright © Magento, Inc. All rights reserved.
33 * See COPYING.txt for license details.
44 */
5+
56package com .magento .idea .magento2plugin .actions .generation .generator ;
67
78import com .google .gson .JsonElement ;
89import com .google .gson .JsonParser ;
910import com .intellij .openapi .project .Project ;
11+ import com .intellij .openapi .util .Pair ;
1012import com .intellij .openapi .vfs .VirtualFile ;
1113import com .intellij .psi .PsiFile ;
1214import com .magento .idea .magento2plugin .actions .generation .data .ModuleComposerJsonData ;
1618import com .magento .idea .magento2plugin .indexes .ModuleIndex ;
1719import com .magento .idea .magento2plugin .magento .files .ComposerJson ;
1820import com .magento .idea .magento2plugin .util .CamelCaseToHyphen ;
19- import com .intellij .openapi .util .Pair ;
20- import org .jetbrains .annotations .NotNull ;
2121import java .io .FileNotFoundException ;
2222import java .io .FileReader ;
23- import java .util .Properties ;
2423import java .util .List ;
24+ import java .util .Properties ;
25+ import org .jetbrains .annotations .NotNull ;
2526
2627public class ModuleComposerJsonGenerator extends FileGenerator {
2728
2829 private final ModuleComposerJsonData moduleComposerJsonData ;
2930 private final FileFromTemplateGenerator fileFromTemplateGenerator ;
3031 private final DirectoryGenerator directoryGenerator ;
3132 private final CamelCaseToHyphen camelCaseToHyphen ;
32- private final Project project ;
3333 private final ModuleIndex moduleIndex ;
3434
35- public ModuleComposerJsonGenerator (@ NotNull ModuleComposerJsonData moduleComposerJsonData , Project project ) {
35+ /**
36+ * Constructor.
37+ *
38+ * @param moduleComposerJsonData ModuleComposerJsonData
39+ * @param project Project
40+ */
41+ public ModuleComposerJsonGenerator (
42+ final @ NotNull ModuleComposerJsonData moduleComposerJsonData ,
43+ final Project project
44+ ) {
3645 super (project );
3746 this .moduleComposerJsonData = moduleComposerJsonData ;
3847 this .fileFromTemplateGenerator = FileFromTemplateGenerator .getInstance (project );
3948 this .directoryGenerator = DirectoryGenerator .getInstance ();
4049 this .camelCaseToHyphen = CamelCaseToHyphen .getInstance ();
41- this .project = project ;
4250 this .moduleIndex = ModuleIndex .getInstance (project );
4351 }
4452
45- public PsiFile generate (String actionName ) {
53+ @ Override
54+ public PsiFile generate (final String actionName ) {
4655 if (moduleComposerJsonData .getCreateModuleDirs ()) {
47- ModuleDirectoriesData moduleDirectoriesData = directoryGenerator .createOrFindModuleDirectories (moduleComposerJsonData .getPackageName (), moduleComposerJsonData .getModuleName (), moduleComposerJsonData .getBaseDir ());
48- return fileFromTemplateGenerator .generate (ComposerJson .getInstance (), getAttributes (), moduleDirectoriesData .getModuleDirectory (), actionName );
56+ final ModuleDirectoriesData moduleDirectoriesData =
57+ directoryGenerator .createOrFindModuleDirectories (
58+ moduleComposerJsonData .getPackageName (),
59+ moduleComposerJsonData .getModuleName (),
60+ moduleComposerJsonData .getBaseDir ()
61+ );
62+ return fileFromTemplateGenerator .generate (
63+ ComposerJson .getInstance (),
64+ getAttributes (),
65+ moduleDirectoriesData .getModuleDirectory (),
66+ actionName
67+ );
4968 }
50- return fileFromTemplateGenerator .generate (ComposerJson .getInstance (), getAttributes (), moduleComposerJsonData .getBaseDir (), actionName );
69+ return fileFromTemplateGenerator .generate (
70+ ComposerJson .getInstance (),
71+ getAttributes (),
72+ moduleComposerJsonData .getBaseDir (),
73+ actionName
74+ );
5175 }
5276
53- protected void fillAttributes (Properties attributes ) {
77+ @ Override
78+ protected void fillAttributes (final Properties attributes ) {
5479 attributes .setProperty ("PACKAGE" , moduleComposerJsonData .getPackageName ());
5580 attributes .setProperty ("MODULE_NAME" , moduleComposerJsonData .getModuleName ());
5681 attributes .setProperty ("MODULE_DESCRIPTION" , moduleComposerJsonData .getModuleDescription ());
57- attributes .setProperty ("COMPOSER_PACKAGE_NAME" , moduleComposerJsonData .getComposerPackageName ());
82+ attributes .setProperty (
83+ "COMPOSER_PACKAGE_NAME" ,
84+ moduleComposerJsonData .getComposerPackageName ()
85+ );
5886 attributes .setProperty ("MODULE_VERSION" , moduleComposerJsonData .getModuleVersion ());
59- attributes .setProperty ("LICENSE" , this .getLicensesString (moduleComposerJsonData .getModuleLicense ()));
60- attributes .setProperty ("DEPENDENCIES" , this .getDependenciesString (moduleComposerJsonData .getModuleDependencies ()));
87+ attributes .setProperty (
88+ "LICENSE" ,
89+ this .getLicensesString (moduleComposerJsonData .getModuleLicense ())
90+ );
91+ attributes .setProperty (
92+ "DEPENDENCIES" ,
93+ this .getDependenciesString (moduleComposerJsonData .getModuleDependencies ())
94+ );
6195 }
6296
63- protected String getLicensesString (List licensesList ) {
97+ protected String getLicensesString (final List licensesList ) {
6498 String license = "[\n " ;
65- Object [] licenses = licensesList .toArray ();
99+ final Object [] licenses = licensesList .toArray ();
66100
67101 for (int i = 0 ; i < licenses .length ; i ++) {
68102 license = license .concat ("\" " );
69103 license = license .concat (licenses [i ].toString ());
70104 license = license .concat ("\" " );
71105
72- if (licenses .length != (i + 1 )) license = license .concat ("," );
106+ if (licenses .length != i + 1 ) {
107+ license = license .concat ("," );
108+ }
73109
74110 license = license .concat ("\n " );
75111 }
@@ -79,11 +115,14 @@ protected String getLicensesString(List licensesList) {
79115 return license ;
80116 }
81117
82- private String getDependenciesString (List dependenciesList ) {
118+ private String getDependenciesString (final List dependenciesList ) {
83119 String result = "" ;
84- Object [] dependencies = dependenciesList .toArray ();
120+ final Object [] dependencies = dependenciesList .toArray ();
85121 result = result .concat (ComposerJson .DEFAULT_DEPENDENCY );
86- boolean noDependency = dependencies .length == 1 && dependencies [0 ].equals (ComposerJson .NO_DEPENDENCY_LABEL );
122+ final boolean noDependency =
123+ dependencies .length == 1 && dependencies [0 ].equals (
124+ ComposerJson .NO_DEPENDENCY_LABEL
125+ );
87126 if (dependencies .length == 0 || noDependency ) {
88127 result = result .concat ("\n " );
89128 } else {
@@ -95,14 +134,14 @@ private String getDependenciesString(List dependenciesList) {
95134 }
96135
97136 for (int i = 0 ; i < dependencies .length ; i ++) {
98- String dependency = dependencies [i ].toString ();
99- Pair <String , String > dependencyData = getDependencyData (dependency );
137+ final String dependency = dependencies [i ].toString ();
138+ final Pair <String , String > dependencyData = getDependencyData (dependency );
100139 result = result .concat ("\" " );
101140 result = result .concat (dependencyData .getFirst ());
102141 result = result .concat ("\" " );
103142 result = result .concat (": \" " + dependencyData .getSecond () + "\" " );
104143
105- if (dependencies .length != ( i + 1 ) ) {
144+ if (dependencies .length != i + 1 ) {
106145 result = result .concat ("," );
107146 }
108147
@@ -112,20 +151,23 @@ private String getDependenciesString(List dependenciesList) {
112151 return result ;
113152 }
114153
115- private Pair <String , String > getDependencyData (String dependency ) {
154+ private Pair <String , String > getDependencyData (
155+ final String dependency
156+ ) {
116157 String version = "*" ;
117158 String moduleName = camelCaseToHyphen .convert (dependency ).replace ("_-" , "/" );
118159 try {
119- VirtualFile virtualFile = moduleIndex .getModuleDirectoryByModuleName (dependency )
160+ final VirtualFile virtualFile = moduleIndex .getModuleDirectoryByModuleName (dependency )
120161 .findFile (ComposerJson .FILE_NAME )
121- .getVirtualFile ();
162+ .getVirtualFile ();//NOPMD
122163 if (virtualFile .exists ()) {
123- JsonElement jsonElement = new JsonParser ().parse (new FileReader (virtualFile .getPath ()));
124- JsonElement versionJsonElement = jsonElement .getAsJsonObject ().get ("version" );
125- JsonElement nameJsonElement = jsonElement .getAsJsonObject ().get ("name" );
164+ final JsonElement jsonElement =
165+ new JsonParser ().parse (new FileReader (virtualFile .getPath ()));//NOPMD
166+ final JsonElement versionJsonElement = jsonElement .getAsJsonObject ().get ("version" );
167+ final JsonElement nameJsonElement = jsonElement .getAsJsonObject ().get ("name" );
126168 if (versionJsonElement != null ) {
127169 version = versionJsonElement .getAsString ();
128- int minorVersionSeparator = version .lastIndexOf ("." );
170+ final int minorVersionSeparator = version .lastIndexOf ('.' );
129171 version = new StringBuilder (version )
130172 .replace (minorVersionSeparator + 1 , version .length (),"*" )
131173 .toString ();
@@ -134,7 +176,7 @@ private Pair<String, String> getDependencyData(String dependency) {
134176 moduleName = nameJsonElement .getAsString ();
135177 }
136178 }
137- } catch (FileNotFoundException e ) {
179+ } catch (FileNotFoundException e ) { //NOPMD
138180 // It's fine
139181 }
140182
0 commit comments