@@ -87,7 +87,7 @@ private function renderTable(string $className, array $rows, string $filename):
8787 */
8888 private function getTableHeaders (): array
8989 {
90- return [
90+ $ fields = [
9191 "Method Name " ,
9292 "Lines " ,
9393 "Arguments " ,
@@ -98,11 +98,40 @@ private function getTableHeaders(): array
9898 "If Nesting \nLevel " ,
9999 "Else " ,
100100 "Cognitive \nComplexity " ,
101- "Halstead \nVolume " ,
102- "Halstead \nDifficulty " ,
103- "Halstead \nEffort " ,
104- "Cyclomatic \nComplexity "
105101 ];
102+
103+ $ fields = $ this ->addHalsteadHeaders ($ fields );
104+ $ fields = $ this ->addCyclomaticHeaders ($ fields );
105+
106+ return $ fields ;
107+ }
108+
109+ /**
110+ * @param array<string> $fields
111+ * @return array<string>
112+ */
113+ private function addHalsteadHeaders (array $ fields ): array
114+ {
115+ if ($ this ->configService ->getConfig ()->showHalsteadComplexity ) {
116+ $ fields [] = "Halstead \nVolume " ;
117+ $ fields [] = "Halstead \nDifficulty " ;
118+ $ fields [] = "Halstead \nEffort " ;
119+ }
120+
121+ return $ fields ;
122+ }
123+
124+ /**
125+ * @param array<string> $fields
126+ * @return array<string>
127+ */
128+ private function addCyclomaticHeaders (array $ fields ): array
129+ {
130+ if ($ this ->configService ->getConfig ()->showCyclomaticComplexity ) {
131+ $ fields [] = "Cyclomatic \nComplexity " ;
132+ }
133+
134+ return $ fields ;
106135 }
107136
108137 /**
@@ -160,10 +189,7 @@ private function getKeys(): array
160189 */
161190 private function metricsToArray (CognitiveMetrics $ metrics ): array
162191 {
163- $ halstead = $ metrics ->getHalstead ();
164- $ cyclomatic = $ metrics ->getCyclomatic ();
165-
166- return [
192+ $ fields = [
167193 'methodName ' => $ metrics ->getMethod (),
168194 'lineCount ' => $ metrics ->getLineCount (),
169195 'argCount ' => $ metrics ->getArgCount (),
@@ -174,11 +200,42 @@ private function metricsToArray(CognitiveMetrics $metrics): array
174200 'ifNestingLevel ' => $ metrics ->getIfNestingLevel (),
175201 'elseCount ' => $ metrics ->getElseCount (),
176202 'score ' => $ this ->formatScore ($ metrics ->getScore ()),
177- 'halsteadVolume ' => $ this ->formatHalsteadVolume ($ halstead ),
178- 'halsteadDifficulty ' => $ this ->formatHalsteadDifficulty ($ halstead ),
179- 'halsteadEffort ' => $ this ->formatHalsteadEffort ($ halstead ),
180- 'cyclomaticComplexity ' => $ this ->formatCyclomaticComplexity ($ cyclomatic ),
181203 ];
204+
205+ $ fields = $ this ->addHalsteadFields ($ fields , $ metrics ->getHalstead ());
206+ $ fields = $ this ->addCyclomaticFields ($ fields , $ metrics ->getCyclomatic ());
207+
208+ return $ fields ;
209+ }
210+
211+ /**
212+ * @param array<string, mixed> $fields
213+ * @param HalsteadMetrics|null $halstead
214+ * @return array<string, mixed>
215+ */
216+ private function addHalsteadFields (array $ fields , ?HalsteadMetrics $ halstead ): array
217+ {
218+ if ($ this ->configService ->getConfig ()->showHalsteadComplexity ) {
219+ $ fields ['halsteadVolume ' ] = $ this ->formatHalsteadVolume ($ halstead );
220+ $ fields ['halsteadDifficulty ' ] = $ this ->formatHalsteadDifficulty ($ halstead );
221+ $ fields ['halsteadEffort ' ] = $ this ->formatHalsteadEffort ($ halstead );
222+ }
223+
224+ return $ fields ;
225+ }
226+
227+ /**
228+ * @param array<string, mixed> $fields
229+ * @param CyclomaticMetrics|null $cyclomatic
230+ * @return array<string, mixed>
231+ */
232+ private function addCyclomaticFields (array $ fields , ?CyclomaticMetrics $ cyclomatic ): array
233+ {
234+ if ($ this ->configService ->getConfig ()->showCyclomaticComplexity ) {
235+ $ fields ['cyclomaticComplexity ' ] = $ this ->formatCyclomaticComplexity ($ cyclomatic );
236+ }
237+
238+ return $ fields ;
182239 }
183240
184241 private function formatScore (float $ score ): string
@@ -188,28 +245,21 @@ private function formatScore(float $score): string
188245 : '<info> ' . $ score . '</info> ' ;
189246 }
190247
191- /**
192- * @param HalsteadMetrics|null $halstead
193- * @return string
194- */
195248 private function formatHalsteadVolume (?HalsteadMetrics $ halstead ): string
196249 {
197250 if (!$ halstead ) {
198251 return '- ' ;
199252 }
253+
200254 $ value = round ($ halstead ->getVolume (), 3 );
201- if ($ value >= 1000 ) {
202- return '<error> ' . $ value . '</error> ' ;
203- }
204- if ($ value >= 100 ) {
205- return '<comment> ' . $ value . '</comment> ' ;
206- }
207- return (string )$ value ;
255+
256+ return match (true ) {
257+ $ value >= 1000 => '<error> ' . $ value . '</error> ' ,
258+ $ value >= 100 => '<comment> ' . $ value . '</comment> ' ,
259+ default => (string )$ value ,
260+ };
208261 }
209262
210- /**
211- * @param HalsteadMetrics|null $halstead
212- */
213263 private function formatHalsteadDifficulty (?HalsteadMetrics $ halstead ): string
214264 {
215265 if (!$ halstead ) {
@@ -225,9 +275,6 @@ private function formatHalsteadDifficulty(?HalsteadMetrics $halstead): string
225275 return (string )$ value ;
226276 }
227277
228- /**
229- * @param HalsteadMetrics|null $halstead
230- */
231278 private function formatHalsteadEffort (?HalsteadMetrics $ halstead ): string
232279 {
233280 if (!$ halstead ) {
@@ -243,9 +290,6 @@ private function formatHalsteadEffort(?HalsteadMetrics $halstead): string
243290 return (string )$ value ;
244291 }
245292
246- /**
247- * @param CyclomaticMetrics|null $cyclomatic
248- */
249293 private function formatCyclomaticComplexity (?CyclomaticMetrics $ cyclomatic ): string
250294 {
251295 if (!$ cyclomatic ) {
@@ -262,14 +306,11 @@ private function formatCyclomaticComplexity(?CyclomaticMetrics $cyclomatic): str
262306
263307 private function colorCyclomaticRisk (string $ risk ): string
264308 {
265- $ riskLower = strtolower ($ risk );
266- if ($ riskLower === 'medium ' ) {
267- return '<comment> ' . $ risk . '</comment> ' ;
268- }
269- if ($ riskLower === 'high ' ) {
270- return '<error> ' . $ risk . '</error> ' ;
271- }
272- return $ risk ;
309+ return match (strtolower ($ risk )) {
310+ 'medium ' => '<comment> ' . $ risk . '</comment> ' ,
311+ 'high ' => '<error> ' . $ risk . '</error> ' ,
312+ default => $ risk ,
313+ };
273314 }
274315
275316 /**
0 commit comments