Automattic / Automattic/VIP-Coding-Standards
Add sniff to delete_option() + add_option
- Dominant language
- PHP
- Stars
- 261
- Forks
- 44
- Avg merge
- 19m
- Merged PRs (30d)
- 1
Description
## What problem would the enhancement address for VIP?
As explained at https://github.com/woocommerce/woocommerce/pull/27696 there is a race condition when `delete_option('foo')` is followed by `add_option('foo', '...')`.
## Describe the solution you'd like
Having a sniff that looked for a `delete_option()` call followed by an `add_option()` immediately
or at some point within the same scope, for the same option key, could flag this race condition and suggest using `update_option()` instead.
## What code should be reported as a violation?
```php
function reset_settings() {
$defaults = get_default_settings();
delete_option( 'my_settings' );
add_option( 'my_settings', $defaults );
}
```
Likely to need some consideration of an option key that is using a variable.
## What code should *not* be reported as a violation?
- `add_option()` in a different scope.
- different option key (including different variable).
Contributor guide
Research direction
Start by tracing how existing PHP_CodeSniffer sniffs represent delete_option() and add_option() calls, including their scope and option-key handling. Done means reporting matching calls in the same scope, excluding different scopes or keys, and suggesting update_option(); the issue does not name a target file or test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php, wordpress
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100