WordPress.PHP.DevelopmentFunctions.error_log_trigger_error : recommend wp_trigger_error() as alternative

Open
#2,515 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
50/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
php
Domain
tooling

Research direction

Start by locating the sniff that emits WordPress.PHP.DevelopmentFunctions.error_log_trigger_error and the tests covering its current warning message. Update the diagnostic to recommend wp_trigger_error() as the alternative, then run the relevant sniff tests and confirm the new wording is reported for the supplied trigger_error() example.

Written by the indexing model from the issue text.

Description

Wasn't sure if this should be reported as a bug or feature request..?

Bug Description

The current warning message is vague and does not provide helpful recommendation:

trigger_error() found. Debug code should not normally be used in production.
(WordPress.PHP.DevelopmentFunctions.error_log_trigger_error) phpcs

Minimal Code Snippet

trigger_error( 'Cannot add postmeta for negative post ID value.', \E_USER_WARNING );

Error Code

WordPress.PHP.DevelopmentFunctions.error_log_trigger_error

Environment

Question Answer
PHP version 8.3.6
PHP_CodeSniffer version 1.0.0
WordPressCS version 3.1.0
PHPCSUtils version 1.0.12
PHPCSExtra version 1.2.1
WordPressCS install type Composer global
IDE (if relevant) Visual Studio Code (Version: 1.96.2)

Full version info of Visual Studio Code

Version: 1.96.2
Commit: fabdb6a30b49f79a7aba0f2ad9df9b399473380f
Date: 2024-12-19T10:22:47.216Z
Electron: 32.2.6
ElectronBuildId: 10629634
Chromium: 128.0.6613.186
Node.js: 20.18.1
V8: 12.8.374.38-electron.0
OS: Darwin arm64 24.1.0

Additional Context (optional)

I really appreciate the warning message about using json_encode():

json_encode() is discouraged. Use wp_json_encode() instead.
(WordPress.WP.AlternativeFunctions.json_encode_json_encode)phpcs

I had no idea wp_trigger_error() even existed until I was reading the documentation for _doing_it_wrong(), yet it's exactly what I've been wanting and resolves the warnings.

Originally, I also had no idea about wp_json_encode() which is why I'm so thankful for the great messaging for that warning.

Tested Against develop Branch?

  • I have verified the issue still exists in the develop branch of WordPressCS.
    • I'm using the Composer global installation, so I'm not sure how to confirm this.
Dominant language
PHP
Stars
2.8k
Forks
521
Avg merge
5d 20h
Merged PRs (30d)
1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from WordPress/WordPress-Coding-Standards

All issues in WordPress/WordPress-Coding-Standards

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.