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

False positive InvalidArgument for preg_replace_callback and preg_match #2126

Closed
TysonAndre opened this issue Sep 11, 2019 · 2 comments
Closed
Labels

Comments

@TysonAndre
Copy link
Contributor

TysonAndre commented Sep 11, 2019

Possibly related to changes to Psalm's handling of functions with alternates

<?php
function f(array &$configs, string $value) {
    // NOTE: Adding a return type would fix the InvalidArgument error
    // Observed: InvalidArgument - Argument 3 of preg_replace_callback expects array<array-key, string>, string provided
    // Expected: Should take closest match for callable and not emit InvalidArgument
    $value = preg_replace_callback('/{{{(\w*)}}}/', function (array $match) use ($configs) {
        $key = $match[1];
        return $configs[$key];
    }, $value, -1, $count);
}
<?php
/** @param int[]|string[]|bool[] $params */
function test($params) {
    // Related:
    // ERROR: InvalidScalarArgument - src/match.php:4:77 - Argument 2 of preg_match expects string, int|string|bool provided
    // ERROR: InvalidArgument - src/match.php:4:108 - Argument 4 of preg_match expects int(0), int(256) provided
    // Expected: Should warn about InvalidScalarArgument but not InvalidArgument. All 3 alternate signatures have 'subject' => 'string'
    // Observed: Warns about both
    if (!array_key_exists('x', $params) || preg_match('/\/ab\d+\//', $params['x'], $matches, PREG_OFFSET_CAPTURE) == 0 || !empty($params['is'])) {
    }
}
@TysonAndre TysonAndre changed the title False positive InvalidArgument for preg_replace_callback False positive InvalidArgument for preg_replace_callback and preg_match Sep 11, 2019
@muglug
Copy link
Collaborator

muglug commented Sep 11, 2019

It's that last argument that messes things up:

function f(string $value) : string {
    // InvalidArgument
    return preg_replace_callback('/a/', function () { return $_GET["a"]; }, $value, -1, $count);
}
function g(string $value) : string {
    // ok
    return preg_replace_callback('/a/', function () { return $_GET["a"]; }, $value, -1);
}

https://psalm.dev/r/1466b6d7e1

@muglug muglug added the bug label Sep 11, 2019
@muglug
Copy link
Collaborator

muglug commented Nov 5, 2020

This is fixed

@muglug muglug closed this as completed Nov 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants