WordPress / WordPress/WordPress-Coding-Standards
DB/PreparedSQLPlaceholders: false negatives for namespaced and method calls to sprintf()
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 2.8k
- Forks
- 521
- Avg merge
- 5d 20h
- Merged PRs (30d)
- 1
Description
Bug Description
The WordPress.DB.PreparedSQLPlaceholders sniff incorrectly treats namespaced function calls and method calls to sprintf() as if they were calls to the global sprintf() function, causing false negatives.
The sniff has special handling for the dynamic placeholder pattern using sprintf(). When it encounters a namespaced call like MyNamespace\sprintf() or a method call like $obj->sprintf(), it incorrectly applies this special handling instead of treating the call as an unknown function.
This issue only affects sprintf(). The detection of implode() and array_fill() already correctly filters out namespaced and method calls.
Minimal Code Snippet
The issue happens when running this command:
phpcs --standard=WordPress --sniffs=WordPress.DB.PreparedSQLPlaceholders test.php
... over a file containing this code:
<?php
$wpdb->prepare(
MyNamespace\sprintf(
'SELECT * FROM `table` WHERE `ID` IN (%s)',
implode( ',', array_fill( 0, count( $ids ), '%s' ) )
),
$ids
);
$wpdb->prepare(
$obj->sprintf(
'SELECT * FROM `table` WHERE `ID` IN (%s)',
implode( ',', array_fill( 0, count( $ids ), '%s' ) )
),
$ids
);
Expected behavior:
Both lines should be flagged. MyNamespace\sprintf() and $obj->sprintf() are not the global sprintf() function, so the dynamic placeholder pattern should not apply.
Actual behavior:
No errors or warnings. The sniff treats both calls as global sprintf() and applies the dynamic placeholder pattern handling.
Environment
| Question | Answer |
|---|---|
| PHP version | 8.4.19 |
| PHP_CodeSniffer version | 3.13.5 |
| WordPressCS version | develop |
| PHPCSUtils version | 1.2.1 |
| PHPCSExtra version | 1.5.0 |
| WordPressCS install type | git clone |
| IDE (if relevant) | N/A |
Tested Against develop Branch?
- I have verified the issue still exists in the
developbranch of WordPressCS.
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 with the WordPress.DB.PreparedSQLPlaceholders sniff and reproduce the report using the shown phpcs command and PHP snippet. Compare the handling of global sprintf() with the namespaced and method-call forms; done means both non-global calls are reported instead of receiving the global sprintf() special handling.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- Half a day
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 68/100