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

str_replace does not allow a null replacement #3224

Closed
greg0ire opened this issue Apr 25, 2020 · 10 comments
Closed

str_replace does not allow a null replacement #3224

greg0ire opened this issue Apr 25, 2020 · 10 comments

Comments

@greg0ire
Copy link
Contributor

Working php code Psalm is not happy with: https://psalm.dev/r/89bd6bab25
I would fix it but I could not find where Psalm gets its info from.

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/89bd6bab25
<?php

echo str_replace('a', null, 'abc');
Psalm output (using commit e1c6fcc):

ERROR: NullArgument - 3:23 - Argument 2 of str_replace cannot be null, null value provided to parameter with type array<array-key, mixed>|string

@greg0ire greg0ire mentioned this issue Apr 25, 2020
16 tasks
@weirdan
Copy link
Collaborator

weirdan commented Apr 25, 2020

Looks like a sufficiently bad code that should be fixed rather than allowed.

@greg0ire
Copy link
Contributor Author

greg0ire commented Apr 25, 2020

I agree that it's bad in that it would not have crossed my mind to write it that way, but it feels like Psalm is overstepping here... this looks like something that phpcs would forbid, but not Psalm. Psalm is here to find bugs, and this works perfectly, doesn't it?

@weirdan
Copy link
Collaborator

weirdan commented Apr 25, 2020

That's debatable. On one hand 'Psalm is not PhpCS' is established position, I had the issues reported by me closed with that justification. On the other hand Psalm rejects a lot of perfectly working code (see all those Mixed* issues), so it's not here to only prevent bugs. It's here to make you write better code, and does this mainly by enforcing type correctness. And you're passing null (which is distinct type in PHP) where string[]|string is expected.

Besides, it's easy to fix: https://psalm.dev/r/1d16bdfe2a

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/1d16bdfe2a
<?php

echo str_replace('a', (string)null, 'abc');
Psalm output (using commit e1c6fcc):

No issues!

@greg0ire
Copy link
Contributor Author

And you're passing null (which is distinct type in PHP) where string[]|string is expected.

Again, I did not write this :P , but if I had to fix it, I would use echo str_replace('a', '', 'abc');

I'm afraid that if I change this, people are going to go pedantic and tell me that nothing in the docs says that replacement as to be string[]|string

On the other hand Psalm rejects a lot of perfectly working code (see all those Mixed* issues), so it's not here to only prevent bugs.

I didn't know that was the case, closing, then, thank you.

greg0ire added a commit to greg0ire/dbal that referenced this issue Apr 25, 2020
@weirdan
Copy link
Collaborator

weirdan commented Apr 25, 2020

I'm afraid that if I change this, people are going to go pedantic and tell me that nothing in the docs says that replacement as to be string[]|string

Apart from the function description which reads 'Replace all occurrences of the search string with the replacement string' (https://www.php.net/str_replace).

If that doesn't work to convince them, invoke @Ocramius 🤣

@greg0ire
Copy link
Contributor Author

Well the repo at hand is doctrine/dbal, so … should work I guess :P

@Ocramius
Copy link
Contributor

should be fixed: use a (string) cast on that null, and pretend to never have seen that null also ever worked.

@greg0ire
Copy link
Contributor Author

It's fixed, see the linked commit above ;)

greg0ire added a commit to greg0ire/dbal that referenced this issue Apr 26, 2020
greg0ire added a commit to greg0ire/dbal that referenced this issue Apr 28, 2020
greg0ire added a commit to greg0ire/dbal that referenced this issue Apr 29, 2020
greg0ire added a commit to greg0ire/dbal that referenced this issue May 3, 2020
greg0ire added a commit to greg0ire/dbal that referenced this issue May 3, 2020
greg0ire added a commit to greg0ire/dbal that referenced this issue May 4, 2020
greg0ire added a commit to greg0ire/dbal that referenced this issue May 8, 2020
greg0ire added a commit to greg0ire/dbal that referenced this issue May 11, 2020
greg0ire added a commit to greg0ire/dbal that referenced this issue May 18, 2020
greg0ire added a commit to greg0ire/dbal that referenced this issue May 24, 2020
greg0ire added a commit to greg0ire/dbal that referenced this issue May 26, 2020
greg0ire added a commit to greg0ire/dbal that referenced this issue May 27, 2020
greg0ire added a commit to greg0ire/dbal that referenced this issue May 27, 2020
greg0ire added a commit to greg0ire/dbal that referenced this issue May 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants