WordPress / WordPress/WordPress-Coding-Standards
WP/AlternativeFunctions: remove invalid STDIN/STDOUT/STDERR resource handling
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 2.8k
- Forks
- 521
- Avg merge
- 5d 20h
- Merged PRs (30d)
- 1
Description
The WordPress.WP.AlternativeFunctions sniff suggests WP functions that should be used instead of native PHP functions. In the case of file_get_contents(), file_put_contents(), fopen(), and readfile(), this recommendation depends on the parameter values.
The sniff currently bows out when a resource is passed as the first parameter (STDIN, STDOUT, STDERR), but this is incorrect because these functions only accept string filenames, not resources. Code like fopen(STDIN, 'w') results in a fatal error: "Uncaught TypeError: fopen(): Argument #1 ($filename) must be of type string, resource given" (https://3v4l.org/vkbB4).
There are a few code examples, like the one above, in the sniff tests. They were introduced in #1655. Unless I'm missing something, I don't see a reason for the sniff to have special handling for invalid code. If that is indeed the case, this condition can be removed:
Removing the condition above means that the sniff will start triggering a warning for the code sample presented in this issue, which I believe is the correct behavior.
Initially discussed in https://github.com/WordPress/WordPress-Coding-Standards/pull/2581#discussion_r2330482665
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in WordPress/Sniffs/WP/AlternativeFunctionsSniff.php at the condition around line 358, then review the sniff tests containing STDIN, STDOUT, and STDERR examples. Remove the special handling for resources and update the affected tests so invalid resource arguments produce the expected warning.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100