77
88import com .intellij .codeInspection .InspectionManager ;
99import com .intellij .codeInspection .ProblemDescriptor ;
10- import com .intellij .codeInspection .ProblemsHolder ;
1110import com .intellij .openapi .project .Project ;
1211import com .intellij .openapi .vfs .VfsUtil ;
1312import com .intellij .openapi .vfs .VirtualFile ;
2322import com .jetbrains .php .lang .psi .elements .PhpPsiElement ;
2423import com .jetbrains .php .lang .psi .elements .PhpUseList ;
2524import com .jetbrains .php .lang .psi .resolve .types .PhpTypeAnalyserVisitor ;
26- import com .jetbrains .php .lang .psi .visitors .PhpElementVisitor ;
2725import com .magento .idea .magento2plugin .util .GetFirstClassOfFile ;
2826import com .magento .idea .magento2uct .execution .process .OutputWrapper ;
27+ import com .magento .idea .magento2uct .execution .scanner .ModuleFilesScanner ;
28+ import com .magento .idea .magento2uct .execution .scanner .ModuleScanner ;
29+ import com .magento .idea .magento2uct .execution .scanner .data .ComponentData ;
30+ import com .magento .idea .magento2uct .inspections .UctInspectionManager ;
31+ import com .magento .idea .magento2uct .inspections .UctProblemsHolder ;
2932import com .magento .idea .magento2uct .inspections .php .DeprecationInspection ;
33+ import com .magento .idea .magento2uct .packages .SupportedIssue ;
3034import java .io .File ;
3135import java .nio .file .Paths ;
3236import java .util .ArrayList ;
3337import java .util .List ;
34-
35- import com .magento .idea .magento2uct .inspections .php .deprecation .ExtendingDeprecatedClass ;
36- import com .magento .idea .magento2uct .inspections .php .deprecation .ImportingDeprecatedType ;
37- import com .magento .idea .magento2uct .versioning .processors .DeprecationIndexProcessor ;
38- import com .magento .idea .magento2uct .versioning .scanner .ModuleFilesScanner ;
39- import com .magento .idea .magento2uct .versioning .scanner .ModuleScanner ;
40- import com .magento .idea .magento2uct .versioning .scanner .data .ComponentData ;
4138import org .jetbrains .annotations .NotNull ;
4239
4340public class GenerateUctReportCommand {
@@ -63,6 +60,8 @@ public GenerateUctReportCommand(
6360 */
6461 public void execute () {
6562 output .write ("Upgrade compatibility tool\n \n " );
63+ final UctInspectionManager inspectionManager = new UctInspectionManager (project );
64+ final UctReportOutputUtil outputUtil = new UctReportOutputUtil (output );
6665
6766 for (final ComponentData componentData : new ModuleScanner (getTargetPsiDirectory ())) {
6867 boolean isModuleHeaderPrinted = false ;
@@ -71,94 +70,31 @@ public void execute() {
7170 if (!(psiFile instanceof PhpFile )) {
7271 continue ;
7372 }
74- final PhpClass phpClass =
75- GetFirstClassOfFile
76- .getInstance ()
77- .execute ((PhpFile ) psiFile );
78-
79- if (phpClass != null ) {
80- final ExtendingDeprecatedClass extendingDeprecatedClass =
81- new ExtendingDeprecatedClass ();
82- final ProblemsHolder extendingDeprecatedClassProblemsHolder = new ProblemsHolder (
83- InspectionManager .getInstance (project ),
84- psiFile ,
85- false
86- );
87- final PhpElementVisitor extendingDeprecatedClassVisitor =
88- (PhpElementVisitor ) extendingDeprecatedClass .buildVisitor (
89- extendingDeprecatedClassProblemsHolder ,
90- false
91- );
92- extendingDeprecatedClassVisitor .visitPhpClass (phpClass );
93-
94- final ImportingDeprecatedType importingDeprecatedType =
95- new ImportingDeprecatedType ();
96- final ProblemsHolder importingDeprecatedTypeProblemsHolder = new ProblemsHolder (
97- InspectionManager .getInstance (project ),
98- psiFile ,
99- false
100- );
101- final PhpElementVisitor importingDeprecatedTypeVisitor =
102- (PhpElementVisitor ) importingDeprecatedType .buildVisitor (
103- importingDeprecatedTypeProblemsHolder ,
104- false
105- );
106- importingDeprecatedTypeVisitor .visitPhpClass (phpClass );
107-
108- final PhpPsiElement scopeForUseOperator =
109- PhpCodeInsightUtil .findScopeForUseOperator (phpClass );
110-
111- if (scopeForUseOperator != null ) {
112- final List <PhpUseList > imports =
113- PhpCodeInsightUtil .collectImports (scopeForUseOperator );
114-
115- for (final PhpUseList phpUseList : imports ) {
116- importingDeprecatedTypeVisitor .visitPhpUseList (phpUseList );
117- }
118- }
73+ final UctProblemsHolder fileProblemsHolder = inspectionManager .run (psiFile );
11974
120- if (extendingDeprecatedClassProblemsHolder .hasResults ()
121- || importingDeprecatedTypeProblemsHolder .hasResults ()) {
122- if (!isModuleHeaderPrinted ) {
123- final String moduleName = "Module Name: " .concat (componentData .getName ());
124- output .print ("<info>" + moduleName + "</info>\n " );
125- output .print ("<info>" + "-" .repeat (moduleName .length ()) + "</info>\n \n " );
126- isModuleHeaderPrinted = true ;
127- }
128-
129- final String file = "File: " + psiFile .getVirtualFile ().getPath ();
130- output .print ("<info>" + file + "</info>\n " );
131- output .print ("<info>" + "-" .repeat (file .length ()) + "</info>\n \n " );
132- }
75+ if (fileProblemsHolder == null ) {
76+ continue ;
77+ }
13378
134- // temporary output
135- for ( final ProblemDescriptor descriptor
136- : extendingDeprecatedClassProblemsHolder . getResults ()) {
137- printErrorMessage ( descriptor ) ;
79+ if ( fileProblemsHolder . hasResults ()) {
80+ if (! isModuleHeaderPrinted ) {
81+ outputUtil . printModuleName ( componentData . getName ());
82+ isModuleHeaderPrinted = true ;
13883 }
139- for (final ProblemDescriptor descriptor
140- : importingDeprecatedTypeProblemsHolder .getResults ()) {
141- printErrorMessage (descriptor );
84+ outputUtil .printProblemFile (psiFile .getVirtualFile ().getPath ());
85+ }
86+
87+ for (final ProblemDescriptor descriptor : fileProblemsHolder .getResults ()) {
88+ final Integer code = fileProblemsHolder .getErrorCodeForDescriptor (descriptor );
89+
90+ if (code != null ) {
91+ outputUtil .printIssue (descriptor , code );
14292 }
14393 }
14494 }
14595 }
14696 }
14797
148- private void printErrorMessage (final ProblemDescriptor descriptor ) {
149- final String errorCode =
150- descriptor .getDescriptionTemplate ().substring (0 , 5 );
151- final String errorMessage =
152- descriptor .getDescriptionTemplate ().substring (5 ).trim ();
153-
154- final String outputText = " * <warning>[WARNING]</warning>"
155- + errorCode + " Line "
156- + descriptor .getLineNumber () + ": "
157- + errorMessage ;
158-
159- output .print (outputText + "\n " );
160- }
161-
16298 /**
16399 * Visit directory recursively.
164100 *
@@ -176,7 +112,7 @@ private void visitDirectory(final @NotNull PsiDirectory directory) {
176112 if (phpClass != null ) {
177113 final DeprecationInspection deprecationInspection =
178114 new DeprecationInspection (phpClass );
179- final ProblemsHolder problemsHolder = new ProblemsHolder (
115+ final UctProblemsHolder problemsHolder = new UctProblemsHolder (
180116 InspectionManager .getInstance (project ),
181117 phpClass .getContainingFile (),
182118 false
@@ -215,7 +151,6 @@ private void visitDirectory(final @NotNull PsiDirectory directory) {
215151 visitor .visitPhpField (field );
216152 }
217153
218- // temporary output
219154 for (final ProblemDescriptor descriptor : problemsHolder .getResults ()) {
220155 output .print (descriptor .getDescriptionTemplate ());
221156 }
@@ -244,4 +179,65 @@ private PsiDirectory getTargetPsiDirectory() {
244179 .getInstance (project )
245180 .findDirectory (targetDirVirtualFile );
246181 }
182+
183+ private static final class UctReportOutputUtil {
184+
185+ private static final String ISSUE_FORMAT = " * {SEVERITY}[{code}] Line {line}: {message}" ;
186+ private final OutputWrapper stdout ;
187+
188+ /**
189+ * UCT report styled output util.
190+ *
191+ * @param output OutputWrapper
192+ */
193+ public UctReportOutputUtil (final @ NotNull OutputWrapper output ) {
194+ stdout = output ;
195+ }
196+
197+ /**
198+ * Print module name header.
199+ *
200+ * @param moduleName String
201+ */
202+ public void printModuleName (final @ NotNull String moduleName ) {
203+ final String moduleNameLine = "Module Name: " .concat (moduleName );
204+ stdout .print (stdout .wrapInfo (moduleNameLine ).concat ("\n " ));
205+ stdout .print (stdout .wrapInfo ("-" .repeat (moduleNameLine .length ())).concat ("\n " ));
206+ }
207+
208+ /**
209+ * Print problem file header.
210+ *
211+ * @param filePath String
212+ */
213+ public void printProblemFile (final @ NotNull String filePath ) {
214+ final String file = "File: " .concat (filePath );
215+ stdout .print ("\n " .concat (stdout .wrapInfo (file )).concat ("\n " ));
216+ stdout .print (stdout .wrapInfo ("-" .repeat (file .length ())).concat ("\n \n " ));
217+ }
218+
219+ /**
220+ * Print issue message.
221+ *
222+ * @param descriptor ProblemDescriptor
223+ * @param code int
224+ */
225+ public void printIssue (final @ NotNull ProblemDescriptor descriptor , final int code ) {
226+ final String errorMessage = descriptor .getDescriptionTemplate ().substring (6 ).trim ();
227+ final SupportedIssue issue = SupportedIssue .getByCode (code );
228+
229+ if (issue == null ) {
230+ return ;
231+ }
232+
233+ final String output = ISSUE_FORMAT
234+ .replace ("{SEVERITY}" , issue .getLevel ().getFormattedLabel ())
235+ .replace ("{code}" , Integer .toString (code ))
236+ .replace ("{line}" , Integer .toString (descriptor .getLineNumber () + 1 ))
237+ .replace ("{message}" , errorMessage )
238+ .concat ("\n " );
239+
240+ stdout .print (output );
241+ }
242+ }
247243}
0 commit comments