Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Case in switch should not increment nesting level #3

Open
bkdotcom opened this issue Oct 22, 2021 · 4 comments
Open

Case in switch should not increment nesting level #3

bkdotcom opened this issue Oct 22, 2021 · 4 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@bkdotcom
Copy link

bkdotcom commented Oct 22, 2021

I don't know what this method's cognitive-complexity should be, but I know it shouldn't be 11

public function foo()
{
    $blah = array();
    for ($i = 0, $count = \count($this->things); $i < $count; $i++) {
        switch ($this->things[$i]['method']) {
            case 'foo':
            case 'bar':
                $blah[] = $i;
                break;
            case 'baz':
                \array_pop($blah);
                break;
            case 'ding':
            case 'dong':
                if ($this->something === false) {
                    break;
                }
                foreach ($blah as $i2) {
                    $this->things[$i2]['method'] = 'foo';
                }
                break;
        }
    }
}

9 maybe?
¯\_(ツ)_/¯

@Rarst
Copy link
Owner

Rarst commented Oct 22, 2021

Think the issue is that PHP CS tokenizer considers case a level and cognitive complexity doesn't. So stuff within case gets extra +1 from nesting each, as level is taken from PHP CS token, rather than tracked independently.

No immediate idea how to track this. There is tracking for try/catch, but switch/case syntax is a lot more elaborate.

@Rarst Rarst added bug Something isn't working help wanted Extra attention is needed labels Oct 22, 2021
@Rarst Rarst changed the title wrong computed value Case in switch should not increment nesting level Oct 22, 2021
@bkdotcom
Copy link
Author

I have a solution... will create pull request soon... will also correctly handle nested trys

@bkdotcom
Copy link
Author

bkdotcom commented Oct 26, 2021

Pull Request #4

This resolves both this issue and #2

I had trouble with the SniffTest unitTest
The first half seemed to be a test of phpcs internals...
The half I simply couldn't get to work.. I'm not sure what code was being sniffed... and the parser didnn't seem to be adding the "extended info" to the tokens...
So... I took a look at how slevomat created their tests and borrowed from them

@bkdotcom
Copy link
Author

bkdotcom commented Oct 26, 2021

Alternatively, Pull request #5 has been created
It's like #4 but without modification to SniffTest
(AnalyzerTest passes, but SniffTest fails)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants