Skip to content

preg_match's referenced matches not considered updated #5365

@ondrejmirtes

Description

@ondrejmirtes

Discussed in #5361

Originally posted by hollodotme July 22, 2021

Code

<?php declare(strict_types = 1);

$matches = [];
$pattern = '#^C\s+(?<productId>\d+)$#i';
$subject = 'C 1234567890';

$found = (bool)preg_match( $pattern, $subject, $matches ) && isset( $matches['productId'] );

PHPStan Error

LINE   ERROR
-------------------------------------
7      Result of && is always false.

See: https://phpstan.org/r/dc55b6b9-5285-4b3c-895f-bb036dd1e2d5

IMO this is a false positive as the second condition isset($matches['productId']) is true after evaluation of the preg_match function.

The $matches array was changed by preg_match() via reference.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions