Skip to content

Commit 9553ab6

Browse files
author
MarkBaker
committed
Factor ternary operator, null coalescence (and assignment) operators into CyclomaticComplexity calculation
This is for Issue #3469, which identified that these operators were not being included in the count, when they should have been (treated as an if/else condition). Unit tests provided.
1 parent 24af403 commit 9553ab6

File tree

3 files changed

+259
-10
lines changed

3 files changed

+259
-10
lines changed

src/Standards/Generic/Sniffs/Metrics/CyclomaticComplexitySniff.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,19 @@ public function process(File $phpcsFile, $stackPtr)
7171

7272
// Predicate nodes for PHP.
7373
$find = [
74-
T_CASE => true,
75-
T_DEFAULT => true,
76-
T_CATCH => true,
77-
T_IF => true,
78-
T_FOR => true,
79-
T_FOREACH => true,
80-
T_WHILE => true,
74+
T_CASE => true,
75+
T_DEFAULT => true,
76+
T_CATCH => true,
77+
T_IF => true,
78+
T_FOR => true,
79+
T_FOREACH => true,
80+
T_WHILE => true,
8181
// T_DO is not required for incrementing CYC, as the terminating while in a do/while loop triggers the branch.
8282
// T_DO => true.
83-
T_ELSEIF => true,
83+
T_ELSEIF => true,
84+
T_INLINE_THEN => true,
85+
T_COALESCE => true,
86+
T_COALESCE_EQUAL => true,
8487
];
8588

8689
$complexity = 1;

src/Standards/Generic/Tests/Metrics/CyclomaticComplexityUnitTest.inc

Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,245 @@ function complexityTwentyOne()
161161
}
162162
}
163163

164+
165+
function complexityTenWithTernaries()
166+
{
167+
$value1 = (empty($condition1)) ? $value1A : $value1B;
168+
$value2 = (empty($condition2)) ? $value2A : $value2B;
169+
170+
switch ($condition) {
171+
case '1':
172+
if ($condition) {
173+
} else if ($cond) {
174+
}
175+
break;
176+
case '2':
177+
while ($cond) {
178+
echo 'hi';
179+
}
180+
break;
181+
case '3':
182+
break;
183+
default:
184+
break;
185+
}
186+
}
187+
188+
189+
function complexityElevenWithTernaries()
190+
{
191+
$value1 = (empty($condition1)) ? $value1A : $value1B;
192+
$value2 = (empty($condition2)) ? $value2A : $value2B;
193+
$value3 = (empty($condition3)) ? $value3A : $value3B;
194+
195+
switch ($condition) {
196+
case '1':
197+
if ($condition) {
198+
} else if ($cond) {
199+
}
200+
break;
201+
case '2':
202+
while ($cond) {
203+
echo 'hi';
204+
}
205+
break;
206+
case '3':
207+
break;
208+
default:
209+
break;
210+
}
211+
}
212+
213+
214+
function complexityTenWithNestedTernaries()
215+
{
216+
$value1 = true ? $value1A : false ? $value1B : $value1C;
217+
218+
switch ($condition) {
219+
case '1':
220+
if ($condition) {
221+
} else if ($cond) {
222+
}
223+
break;
224+
case '2':
225+
while ($cond) {
226+
echo 'hi';
227+
}
228+
break;
229+
case '3':
230+
break;
231+
default:
232+
break;
233+
}
234+
}
235+
236+
237+
function complexityElevenWithNestedTernaries()
238+
{
239+
$value1 = (empty($condition1)) ? $value1A : $value1B;
240+
$value2 = true ? $value2A : false ? $value2B : $value2C;
241+
242+
switch ($condition) {
243+
case '1':
244+
if ($condition) {
245+
} else if ($cond) {
246+
}
247+
break;
248+
case '2':
249+
while ($cond) {
250+
echo 'hi';
251+
}
252+
break;
253+
case '3':
254+
break;
255+
default:
256+
break;
257+
}
258+
}
259+
260+
261+
function complexityTenWithNullCoalescence()
262+
{
263+
$value1 = $value1A ?? $value1B;
264+
$value2 = $value2A ?? $value2B;
265+
266+
switch ($condition) {
267+
case '1':
268+
if ($condition) {
269+
} else if ($cond) {
270+
}
271+
break;
272+
case '2':
273+
while ($cond) {
274+
echo 'hi';
275+
}
276+
break;
277+
case '3':
278+
break;
279+
default:
280+
break;
281+
}
282+
}
283+
284+
285+
function complexityElevenWithNullCoalescence()
286+
{
287+
$value1 = $value1A ?? $value1B;
288+
$value2 = $value2A ?? $value2B;
289+
$value3 = $value3A ?? $value3B;
290+
291+
switch ($condition) {
292+
case '1':
293+
if ($condition) {
294+
} else if ($cond) {
295+
}
296+
break;
297+
case '2':
298+
while ($cond) {
299+
echo 'hi';
300+
}
301+
break;
302+
case '3':
303+
break;
304+
default:
305+
break;
306+
}
307+
}
308+
309+
310+
function complexityTenWithNestedNullCoalescence()
311+
{
312+
$value1 = $value1A ?? $value1B ?? $value1C;
313+
314+
switch ($condition) {
315+
case '1':
316+
if ($condition) {
317+
} else if ($cond) {
318+
}
319+
break;
320+
case '2':
321+
while ($cond) {
322+
echo 'hi';
323+
}
324+
break;
325+
case '3':
326+
break;
327+
default:
328+
break;
329+
}
330+
}
331+
332+
333+
function complexityElevenWithNestedNullCoalescence()
334+
{
335+
$value1 = $value1A ?? $value1B;
336+
$value2 = $value2A ?? $value2B ?? $value2C;
337+
338+
switch ($condition) {
339+
case '1':
340+
if ($condition) {
341+
} else if ($cond) {
342+
}
343+
break;
344+
case '2':
345+
while ($cond) {
346+
echo 'hi';
347+
}
348+
break;
349+
case '3':
350+
break;
351+
default:
352+
break;
353+
}
354+
}
355+
356+
357+
function complexityTenWithNullCoalescenceAssignment()
358+
{
359+
$value1 ??= $default1;
360+
$value2 ??= $default2;
361+
362+
switch ($condition) {
363+
case '1':
364+
if ($condition) {
365+
} else if ($cond) {
366+
}
367+
break;
368+
case '2':
369+
while ($cond) {
370+
echo 'hi';
371+
}
372+
break;
373+
case '3':
374+
break;
375+
default:
376+
break;
377+
}
378+
}
379+
380+
381+
function complexityElevenWithNullCoalescenceAssignment()
382+
{
383+
$value1 ??= $default1;
384+
$value2 ??= $default2;
385+
$value3 ??= $default3;
386+
387+
switch ($condition) {
388+
case '1':
389+
if ($condition) {
390+
} else if ($cond) {
391+
}
392+
break;
393+
case '2':
394+
while ($cond) {
395+
echo 'hi';
396+
}
397+
break;
398+
case '3':
399+
break;
400+
default:
401+
break;
402+
}
403+
}
404+
164405
?>

src/Standards/Generic/Tests/Metrics/CyclomaticComplexityUnitTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,13 @@ public function getErrorList()
4141
public function getWarningList()
4242
{
4343
return [
44-
45 => 1,
45-
72 => 1,
44+
45 => 1,
45+
72 => 1,
46+
189 => 1,
47+
237 => 1,
48+
285 => 1,
49+
333 => 1,
50+
381 => 1,
4651
];
4752

4853
}//end getWarningList()

0 commit comments

Comments
 (0)