Skip to content

The preg_match always return with option array key when condition is === 1 #57

Description

@kayw-geek

Hi @shish, Thank you for adding more features to Safe.

Currently, I found an issue where the return value of the \Safe\preg_match() method must be assigned to a variable for PHPStan to correctly infer it; otherwise, it assumes that the array key might not exist.

$result = \Safe\preg_match($pattern, $string, $matches);
if ($result === 1){
    \PHPStan\Testing\assertType($type, $matches);
}

if(\Safe\preg_match($pattern, $string, $matches) === 1) {
    \PHPStan\Testing\assertType($type, $matches); // line 24
}
 ------ ------------------------------------------------------------------------------------------------------------------------------------ 
  24     Expected type array{0: string, 1: non-empty-string, 2: 'o', 3?: 'World'}, actual: array{0?: string, 1?: non-empty-string, 2?: 'o',  
         3?: 'World'}                                                                                                                        
 ------ ------------------------------------------------------------------------------------------------------------------------------------ 


                                                                                                                        
 [ERROR] Found 1 error                                                                                                  
                       

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions