Skip to content

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

Open
@kayw-geek

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                                                                                                  
                       

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