Skip to content

Safe\preg_replace fails PHPStan check #94

@acabal

Description

@acabal

Given the following minimal test case:

<?php
require __DIR__ . 'vendor/autoload.php';

use function Safe\preg_replace;

$x = preg_replace('/foo/', 'bar', 'baz');
$y = stripos($x, 'foo');
?>

When running phpstan analyse -l 7 on the above, it outputs: Parameter #1 $haystack of function stripos expects string, array|string given.

This is because preg_replace() can return either a string or an array depending on what the third parameter is. In this case, the third parameter is a string, so it should return a string and we should be OK. In fact without Safe, PHPStan does not report an error. But when using Safe, PHPStan seems to get confused about the third parameter and reports a type error even though the code is in fact correct.

I'm not sure if this is a Safe issue or a PHPStan issue but because the two projects are closely related in function I thought I'd report it here first to see.

Metadata

Metadata

Assignees

No one assigned

    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