Skip to content

method.max-depth

Ensure that nesting level of a method or function does not exceed a specified limit

Options

max

The maximum nesting level allowed

Type: number

Default: 3

Examples

  • Examples of correct code for this rule using default options

A function with one nested level

php
<?php

function nestedFunction() {
	if ($condition) {
		echo 'hi';
	}
}
  • Examples of incorrect code for this rule using default options

A function with 5 nested levels

php
<?php

function nestedFunction() {
	if ($condition) {
		echo 'hi';
		switch ($condition){
			case '1':
				if ($condition === '1') {
					if ($cond) {
						echo 'hi';
					}
				}
			break;
		}
	}
}

A function with 6 nested levels

php
<?php

function nestedFunction() {
	if ($condition) {
		echo 'hi';
		switch ($condition) {
			case '1':
				if ($condition === '1') {
					if ($cond) {
						foreach ($conds as $cond) {
							echo 'hi';
						}
					}
				}
			break;
		}
	}
}

A function with 10 nested levels

php
<?php

function nestedFunction() {
	if ($condition) {
		echo 'hi';
		switch ($condition){
			case '1':
				if ($condition === '1') {
					if ($cond) {
						switch ($cond) {
							case '1':
								if ($cond === '1') {
									foreach ($conds as $cond) {
										if ($cond === 'hi') {
											echo 'hi';
										}
									}
								}
							break;
						}
					}
				}
			break;
		}
	}
}

A function with 11 nested levels

php
<?php

function nestedFunction() {
	if ($condition) {
		echo 'hi';
		switch ($condition){
			case '1':
				if ($condition === '1') {
					if ($cond) {
						switch ($cond) {
							case '1':
								if ($cond === '1') {
									foreach ($conds as $cond) {
										if ($cond === 'hi') {
											if ($cond !== 'bye') {
												echo 'hi';
											}
										}
									}
								}
							break;
						}
					}
				}
			break;
		}
	}
}

Released under the MIT License.