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

High cyclomatic complexity causing PHPStan to crash #7918

Closed
JoyceBabu opened this issue Sep 3, 2022 · 3 comments
Closed

High cyclomatic complexity causing PHPStan to crash #7918

JoyceBabu opened this issue Sep 3, 2022 · 3 comments

Comments

@JoyceBabu
Copy link

JoyceBabu commented Sep 3, 2022

Bug report

High cyclomatic complexity, with an type-inferred array is causing PHPStan to crash.

The issue can be easily resolved by specifying the type of the array, or by avoiding the cyclomatic complexity, by direct assignment without using the conditional statement.

Code snippet that reproduces the problem

Expected output

The code below is causing PHPStan demo page to crash, hence I couldn't create a URL.

<?php

class TestController
{
    /** @return array<int, string> */
    private function someFunc(): array
    {
        return [];
    }
    
    private function rand(): bool
    {
        return random_int(0, 1) > 0;
    }

    /** @return list<array<string, bool>> */
    public function run(): array
    {
        $arr3 = [];
        foreach ($this->someFunc() as $id => $arr2) {
            // Solution 1 - Specify $result type
            // /** @var array<string,bool> $result */
            $result = [
                'val1' => false,
                'val2' => false,
                'val3' => false,
                'val4' => false,
                'val5' => false,
                'val6' => false,
                'val7' => false,
            ];

            if ($this->rand()) {
                $result['val1'] = true;
            }
            if ($this->rand()) {
                $result['val2'] = true;
            }

            if ($this->rand()) {
                $result['val3'] = true;
            }

            if ($this->rand()) {
                $result['val4'] = true;
            }
            
            if ($this->rand()) {
                $result['val5'] = true;
            }

            if ($this->rand()) {
                $result['val6'] = true;
            }
            
            // Solution 2 - reduce cyclomatic complexity by replacing above statements with the following
            // $result['val1'] = $this->rand();
            // $result['val2'] = $this->rand();
            // $result['val3'] = $this->rand();
            // $result['val4'] = $this->rand();
            // $result['val5'] = $this->rand();
            // $result['val6'] = $this->rand();

            
            $arr3[] = $result;
        }

        return $arr3;
    } 
} 
@mergeable
Copy link

mergeable bot commented Sep 3, 2022

This bug report is missing a link to reproduction at phpstan.org/try.

It will most likely be closed after manual review.

@ondrejmirtes
Copy link
Member

Fixed by: phpstan/phpstan-src@0cc87f3

@github-actions
Copy link

github-actions bot commented Oct 5, 2022

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants