Skip to content
On this page

method.complexity

Ensures that the cyclomatic complexity of functions/methods is withing limit.

Options

max

Maximum allowed cyclomatic complexity.

Type: number

Default: 6

Examples

  • Examples of correct code for this rule using default options

A function with one level of complexity

php
<?php

function complexityZero(){
	echo "Not complex at all!";
}

function complexityOne() {
	if ( $condition ) {
	}
}

A function with five levels of complexity

php
<?php
function complexityFive() {
	if ( $condition ) {
	}

	switch ( $condition ) {
		case '1':
			break;
		case '2':
			break;
		case '3':
			break;
	}
}

A function with 6 levels of complexity with null safe operator

php
<?php
function complexitySixWithNullSafeOperator() {
	$foo = $object1->getX()?->getY()?->getZ();
	$bar = $object2->getX()?->getY()?->getZ();
	$baz = $object3->getX()?->getY()?->getZ();
}
  • Examples of incorrect code for this rule using default options

A function with 10 levels of complexity

php
<?php
function complexityTen() {
	while ( $condition === true ) {
		if ( $condition ) {
		}
	}

	switch ( $condition ) {
		case '1':
			if ( $condition ) {
			} elseif ( $cond ) {
			}
			break;
		case '2':
			while ( $cond ) {
				echo 'hi';
			}
			break;
		case '3':
			break;
		default:
			break;
	}
}

A function with 11 levels of complexity

php
<?php
function complexityEleven() {
	while ( $condition === true ) {
		if ( $condition ) {
		} elseif ( $cond ) {
		}
	}

	switch ( $condition ) {
		case '1':
			if ( $condition ) {
			} elseif ( $cond ) {
			}
			break;
		case '2':
			while ( $cond ) {
				echo 'hi';
			}
			break;
		case '3':
			break;
		default:
			break;
	}
}

A function with 20 levels of complexity

php
<?php
function complexityTwentyOne() {
	while ( $condition === true ) {
		if ( $condition ) {
		} elseif ( $cond ) {
		}
	}

	switch ( $condition ) {
		case '1':
			do {
				if ( $condition ) {
				} elseif ( $cond ) {
				}
			} while ( $cond );
			break;
		case '2':
			while ( $cond ) {
				echo 'hi';
			}
			break;
		case '3':
			switch ( $cond ) {
				case '1':
					break;
				case '2':
					break;
			}
			break;
		case '4':
			do {
				if ( $condition ) {
					if ( $cond ) {
					} elseif ( $con ) {
					}
				}
			} while ( $cond );
			break;
		default:
			if ( $condition ) {
			}
			break;
	}
}

A function with 21 levels of complexity

php
<?php
function complexityTwentyTwo() {
	while ( $condition === true ) {
		do {
			if ( $condition ) {
			} elseif ( $cond ) {
			}
		} while ( $cond );
	}

	switch ( $condition ) {
		case '1':
			if ( $condition ) {
			} elseif ( $cond ) {
			}
			break;
		case '2':
			while ( $cond ) {
				echo 'hi';
			}
			break;
		case '3':
			switch ( $cond ) {
				case '1':
					break;
				case '2':
					break;
			}
			break;
		case '4':
			do {
				if ( $condition ) {
					if ( $cond ) {
					} elseif ( $con ) {
					}
				}
			} while ( $cond );
			break;
		default:
			if ( $condition ) {
			} elseif ( $cond ) {
			}
			break;
	}
}

A function with 10 levels of complexity with ternaries

php
<?php
function complexityTenWithTernaries() {
	 $value1 = ( empty( $condition1 ) ) ? $value1A : $value1B;
	$value2  = ( empty( $condition2 ) ) ? $value2A : $value2B;

	switch ( $condition ) {
		case '1':
			if ( $condition ) {
			} elseif ( $cond ) {
			}
			break;
		case '2':
			while ( $cond ) {
				echo 'hi';
			}
			break;
		case '3':
			break;
		default:
			break;
	}
}

A function with 11 levels of complexity with ternaries

php
<?php
function complexityElevenWithTernaries() {
	$value1 = ( empty( $condition1 ) ) ? $value1A : $value1B;
	$value2 = ( empty( $condition2 ) ) ? $value2A : $value2B;
	$value3 = ( empty( $condition3 ) ) ? $value3A : $value3B;

	switch ( $condition ) {
		case '1':
			if ( $condition ) {
			} elseif ( $cond ) {
			}
			break;
		case '2':
			while ( $cond ) {
				echo 'hi';
			}
			break;
		case '3':
			break;
		default:
			break;
	}
}

A function with 10 levels of complexity with nestedternaries

php
<?php
function complexityTenWithNestedTernaries() {
	$value1 = true ? $value1A : false ? $value1B : $value1C;

	switch ( $condition ) {
		case '1':
			if ( $condition ) {
			} elseif ( $cond ) {
			}
			break;
		case '2':
			while ( $cond ) {
				echo 'hi';
			}
			break;
		case '3':
			break;
		default:
			break;
	}
}

A function with 11 levels of complexity with nested ternaries

php
<?php
function complexityElevenWithNestedTernaries() {
	$value1 = ( empty( $condition1 ) ) ? $value1A : $value1B;
	$value2 = true ? $value2A : false ? $value2B : $value2C;

	switch ( $condition ) {
		case '1':
			if ( $condition ) {
			} elseif ( $cond ) {
			}
			break;
		case '2':
			while ( $cond ) {
				echo 'hi';
			}
			break;
		case '3':
			break;
		default:
			break;
	}
}

A function with 10 levels of complexity with null coalescence

php
<?php
function complexityTenWithNullCoalescence() {
	$value1 = $value1A ?? $value1B;
	$value2 = $value2A ?? $value2B;

	switch ( $condition ) {
		case '1':
			if ( $condition ) {
			} elseif ( $cond ) {
			}
			break;
		case '2':
			while ( $cond ) {
				echo 'hi';
			}
			break;
		case '3':
			break;
		default:
			break;
	}
}

A function with 11 levels of complexity with null coalescence

php
<?php
function complexityElevenWithNullCoalescence() {
	$value1 = $value1A ?? $value1B;
	$value2 = $value2A ?? $value2B;
	$value3 = $value3A ?? $value3B;

	switch ( $condition ) {
		case '1':
			if ( $condition ) {
			} elseif ( $cond ) {
			}
			break;
		case '2':
			while ( $cond ) {
				echo 'hi';
			}
			break;
		case '3':
			break;
		default:
			break;
	}
}

A function with 10 levels of complexity with nested null coalescence

php
<?php
function complexityTenWithNestedNullCoalescence() {
	 $value1 = $value1A ?? $value1B ?? $value1C;

	switch ( $condition ) {
		case '1':
			if ( $condition ) {
			} elseif ( $cond ) {
			}
			break;
		case '2':
			while ( $cond ) {
				echo 'hi';
			}
			break;
		case '3':
			break;
		default:
			break;
	}
}

A function with 11 levels of complexity with nested null coalescence

php
<?php
function complexityElevenWithNestedNullCoalescence() {
	$value1 = $value1A ?? $value1B;
	$value2 = $value2A ?? $value2B ?? $value2C;

	switch ( $condition ) {
		case '1':
			if ( $condition ) {
			} elseif ( $cond ) {
			}
			break;
		case '2':
			while ( $cond ) {
				echo 'hi';
			}
			break;
		case '3':
			break;
		default:
			break;
	}
}

A function with 10 levels of complexity with null coalescence assignment

php
<?php
function complexityTenWithNullCoalescenceAssignment() {
	 $value1 ??= $default1;
	$value2  ??= $default2;

	switch ( $condition ) {
		case '1':
			if ( $condition ) {
			} elseif ( $cond ) {
			}
			break;
		case '2':
			while ( $cond ) {
				echo 'hi';
			}
			break;
		case '3':
			break;
		default:
			break;
	}
}

A function with 11 levels of complexity with null coalescence assignment

php
<?php
function complexityElevenWithNullCoalescenceAssignment() {
	$value1 ??= $default1;
	$value2 ??= $default2;
	$value3 ??= $default3;

	switch ( $condition ) {
		case '1':
			if ( $condition ) {
			} elseif ( $cond ) {
			}
			break;
		case '2':
			while ( $cond ) {
				echo 'hi';
			}
			break;
		case '3':
			break;
		default:
			break;
	}
}

A function with 15 levels of complexity with match

php
<?php
function complexityFifteenWithMatch()
{
	return match(strtolower(substr($monthName, 0, 3))) {
		'jan' => 31,
		'feb' => is_leap_year($year) ? 29 : 28,
		'mar' => 31,
		'apr' => 30,
		'may' => 31,
		'jun' => 30,
		'jul' => 31,
		'aug' => 31,
		'sep' => 30,
		'oct' => 31,
		'nov' => 30,
		'dec' => 31,
		default => throw new InvalidArgumentException("Invalid month"),
	};
}

A function with 10 levels of complexity with null safe operator

php
<?php
function complexityTenWithNullSafeOperator() {
	$foo   = $object1->getX()?->getY()?->getZ();
	$bar   = $object2->getX()?->getY()?->getZ();
	$baz   = $object3->getX()?->getY()?->getZ();
	$bacon = $object4->getX()?->getY()?->getZ();
	$bits  = $object5->getX()?->getY()?->getZ();
}

max

  • Examples of correct code for this rule using max option

A function with 20 levels of complexity

php
<?php
/* taqwim "taqwim/method.complexity" : {max: 20} */
function complexityTen() {
	while ( $condition === true ) {
		if ( $condition ) {
		}
	}

	switch ( $condition ) {
		case '1':
			if ( $condition ) {
			} elseif ( $cond ) {
			}
			break;
		case '2':
			while ( $cond ) {
				echo 'hi';
			}
			break;
		case '3':
			break;
		default:
			break;
	}
}

Released under the MIT License.