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

Possibly Null Argument in bcdiv() scale #10103

Closed
TheDevick opened this issue Aug 12, 2023 · 4 comments · Fixed by #10106
Closed

Possibly Null Argument in bcdiv() scale #10103

TheDevick opened this issue Aug 12, 2023 · 4 comments · Fixed by #10106
Labels
bug easy problems Issues that can be fixed without background knowledge of Psalm good first issue Help wanted internal stubs/callmap

Comments

@TheDevick
Copy link
Contributor

This Code:

<?php

class Math
{
    public function __construct(
        public readonly ?int $scale = null
    ) {
    }

    /**
     * @param numeric-string $x
     * @param numeric-string $y
     *
     * @return numeric-string
     */
    public function div(string $x, string $y): string
    {
        return bcdiv($x, $y, $this->scale);
    }
}

image

In the bcdiv() docs, we can see that the scale can be Nullable

  • Psalm Version: 5.14.1
@psalm-github-bot
Copy link

Hey @TheDevick, can you reproduce the issue on https://psalm.dev ?

@TheDevick
Copy link
Contributor Author

Hey @TheDevick, can you reproduce the issue on https://psalm.dev ?

https://psalm.dev/r/336a0f7530

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/336a0f7530
<?php

class Math
{
    public function __construct(
        public readonly ?int $scale = null
    ) {
    }

    /**
     * @param numeric-string $x
     * @param numeric-string $y
     *
     * @return numeric-string
     */
    public function div(string $x, string $y): string
    {
        return bcdiv($x, $y, $this->scale);
    }
}
Psalm output (using commit 73ebe22):

ERROR: PossiblyNullArgument - 18:30 - Argument 3 of bcdiv cannot be null, possibly null value provided

@weirdan
Copy link
Collaborator

weirdan commented Aug 12, 2023

Feel free to submit the PR, the change would be around these lines:

/**
* @psalm-pure
*
* @param numeric-string $num1
* @param numeric-string $num2
* @return (PHP_MAJOR_VERSION is 8 ? numeric-string : ($num2 is "0" ? null : numeric-string))
*/
function bcdiv(string $num1, string $num2, int $scale = 0): ?string {}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug easy problems Issues that can be fixed without background knowledge of Psalm good first issue Help wanted internal stubs/callmap
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants