Skip to content

Generic/NestingLevel: should nested structures be skipped? #762

Open
@rodrigoprimo

Description

@rodrigoprimo

Describe the bug

Similar to #726, I wonder if the Generic.Metrics.NestingLevel should skip nested structures. Currently, it counts the nesting level of inner functions to the "outer" function.

Code sample

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

To reproduce

Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above.
  2. Run phpcs --standard=Generic --sniffs=Generic.Metrics.NestingLevel test.php
  3. See error message displayed
--------------------------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 2 WARNINGS AFFECTING 2 LINES
--------------------------------------------------------------------------------------------------------------------------------
 3 | WARNING | Function's nesting level (7) exceeds 5; consider refactoring the function (Generic.Metrics.NestingLevel.TooHigh)
 4 | WARNING | Function's nesting level (6) exceeds 5; consider refactoring the function (Generic.Metrics.NestingLevel.TooHigh)
--------------------------------------------------------------------------------------------------------------------------------

Expected behavior

Potentially the sniff should not display an error on line 3.

Versions (please complete the following information)

Operating System not relevant
PHP version not relevant
PHP_CodeSniffer version master
Standard Generic
Install type not relevant

Please confirm

  • I have searched the issue list and am not opening a duplicate issue.
  • I have read the Contribution Guidelines and this is not a support question.
  • I confirm that this bug is a bug in PHP_CodeSniffer and not in one of the external standards.
  • I have verified the issue still exists in the master branch of PHP_CodeSniffer.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions