Skip to content

Commit 5b30ba0

Browse files
committedSep 19, 2022
1139: Code refactoring
1 parent 62feb60 commit 5b30ba0

File tree

7 files changed

+49
-20
lines changed

7 files changed

+49
-20
lines changed
 

‎resources/fileTemplates/internal/Magento Module Setup Patch File.php.ft

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use Magento\Framework\Setup\ModuleDataSetupInterface;
77
use Magento\Framework\Setup\Patch\DataPatchInterface;
88

99
/**
10-
* Patch is mechanism, that allows to do atomic upgrade data changes
10+
* Patch is mechanism, that allows to do atomic upgrade data changes.
1111
*/
1212
class ${CLASS_NAME} implements DataPatchInterface
1313
{
@@ -26,7 +26,7 @@ class ${CLASS_NAME} implements DataPatchInterface
2626
}
2727

2828
/**
29-
* Do Upgrade
29+
* Do Upgrade.
3030
*
3131
* @return void
3232
*/

‎src/com/magento/idea/magento2plugin/actions/generation/dialog/NewSetupDataPatchDialog.form

+3-7
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
<grid id="ad0b1" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
5757
<margin top="0" left="0" bottom="0" right="0"/>
5858
<constraints>
59-
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
59+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="1" fill="1" indent="0" use-parent-layout="false"/>
6060
</constraints>
6161
<properties/>
6262
<border type="none"/>
@@ -71,19 +71,15 @@
7171
</component>
7272
<component id="662ad" class="javax.swing.JLabel" binding="classNameLabel">
7373
<constraints>
74-
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false">
75-
<preferred-size width="113" height="16"/>
76-
</grid>
74+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
7775
</constraints>
7876
<properties>
7977
<text value="Class Name"/>
8078
</properties>
8179
</component>
8280
<component id="292c" class="javax.swing.JTextField" binding="className">
8381
<constraints>
84-
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
85-
<preferred-size width="150" height="-1"/>
86-
</grid>
82+
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
8783
</constraints>
8884
<properties/>
8985
</component>

‎src/com/magento/idea/magento2plugin/actions/generation/dialog/NewSetupDataPatchDialog.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ private void generateFile() {
142142
}
143143

144144
public String getClassName() {
145-
return className.getText();
145+
return className.getText().trim();
146146
}
147147

148148
private boolean validateFields() {
@@ -151,10 +151,14 @@ private boolean validateFields() {
151151
if (NewSetupDataPatchAction.DATA_DIRECTORY.equals(targetDirectory.getName())) {
152152
final PsiFile[] files = targetDirectory.getFiles();
153153
for (final PsiFile file : files) {
154-
if (file.getName().equals(getClassName() + ModuleSetupDataPatchFile.FILE_NAME)) {
154+
final String className = ModuleSetupDataPatchFile.resolveClassNameFromInput(
155+
getClassName()
156+
);
157+
158+
if (file.getName().equals(className + ".php")) {
155159
showErrorMessage(
156160
fieldsValidationsList.get(0).getField(),
157-
"Class name " + getClassName() + " already exist."
161+
"Class name `" + className + "` already exist."
158162
);
159163

160164
return false;

‎src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleSetupDataPatchGenerator.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ public PsiFile generate(final String actionName) {
5757
*/
5858
@Override
5959
protected void fillAttributes(final Properties attributes) {
60-
attributes.setProperty("CLASS_NAME", moduleSetupPatchData.getClassName() + "Patch");
60+
attributes.setProperty("CLASS_NAME", ModuleSetupDataPatchFile.resolveClassNameFromInput(
61+
moduleSetupPatchData.getClassName()
62+
));
6163
attributes.setProperty(
6264
"MODULE_NAME",
6365
moduleSetupPatchData.getPackageName() + "\\" + moduleSetupPatchData.getModuleName()

‎src/com/magento/idea/magento2plugin/magento/files/ModuleSetupDataPatchFile.java

+29-3
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,45 @@
1111

1212
public class ModuleSetupDataPatchFile implements ModuleFileInterface {
1313

14-
public static final String FILE_NAME = "Patch.php";
14+
public static final String FILE_SUFFIX = "Patch";
15+
public static final String EXTENSION = ".php";
1516
public static final String TEMPLATE = "Magento Module Setup Patch File";
1617
private final String className;
1718

19+
/**
20+
* Constructor.
21+
*
22+
* @param className String
23+
*/
1824
public ModuleSetupDataPatchFile(
1925
final @NotNull String className
2026
) {
21-
this.className = className;
27+
this.className = resolveClassNameFromInput(className);
28+
}
29+
30+
/**
31+
* Resolve class name from user input.
32+
*
33+
* @param input String
34+
*
35+
* @return String
36+
*/
37+
public static String resolveClassNameFromInput(final String input) {
38+
if (input.length() <= FILE_SUFFIX.length()) {
39+
return input + FILE_SUFFIX;
40+
}
41+
String suffix = input.substring(input.length() - FILE_SUFFIX.length());
42+
43+
if (FILE_SUFFIX.equals(suffix)) {
44+
return input;
45+
} else {
46+
return input + FILE_SUFFIX;
47+
}
2248
}
2349

2450
@Override
2551
public String getFileName() {
26-
return className + FILE_NAME;
52+
return className + EXTENSION;
2753
}
2854

2955
@Override

‎testData/actions/generation/generator/ModuleSetupDataPatchGenerator/generateModuleSetupDataPatchFile/TestClassPatch.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Magento\Framework\Setup\Patch\DataPatchInterface;
77

88
/**
9-
* Patch is mechanism, that allows to do atomic upgrade data changes
9+
* Patch is mechanism, that allows to do atomic upgrade data changes.
1010
*/
1111
class TestClassPatch implements DataPatchInterface
1212
{
@@ -26,7 +26,7 @@ public function __construct(
2626
}
2727

2828
/**
29-
* Do Upgrade
29+
* Do Upgrade.
3030
*
3131
* @return void
3232
*/

‎tests/com/magento/idea/magento2plugin/actions/generation/generator/ModuleSetupDataPatchGeneratorTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010
import com.magento.idea.magento2plugin.magento.files.ModuleSetupDataPatchFile;
1111

1212
public final class ModuleSetupDataPatchGeneratorTest extends BaseGeneratorTestCase {
13-
private static final String CLASS_NAME = "TestClass";
13+
14+
private static final String CLASS_NAME = "TestClassPatch";
1415

1516
/**
1617
* Test module README.md file generation.
1718
*/
1819
public void testGenerateModuleSetupDataPatchFile() {
1920
final PsiFile expectedFile = myFixture.configureByFile(
20-
getFixturePath(CLASS_NAME + ModuleSetupDataPatchFile.FILE_NAME)
21+
getFixturePath(CLASS_NAME + ModuleSetupDataPatchFile.EXTENSION)
2122
);
2223
final ModuleSetupDataPatchGenerator generator = new ModuleSetupDataPatchGenerator(
2324
new ModuleSetupDataPatchData(

0 commit comments

Comments
 (0)