WordPress / WordPress/WordPress-Coding-Standards

Suggestion: new sniff to detect code inefficiencies

Open
#1,315 5 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Component: Extra Focus: Code analysis Type: Enhancement
Dominant language
PHP
Stars
2.8k
Forks
521
Avg merge
5d 20h
Merged PRs (30d)
1

Description

What's the general opinion on having a sniff to detect the following type of code patterns and throw warnings when detected ? Useful or not ?

echo sprintf( 'string %s', 'replacement'); // Should use `printf()` instead.
echo esc_html__( 'string', 'domain' ); // Should use `esc_html_e()` instead.
echo selected( $selected, $current, false ); // Should use `selected( $selected, $current )` instead.
echo get_search_form( false ); // Should use `get_search_form()` instead.

Of course, it would need to be verified that such a call is not followed by a concatenation adding more text, but that shouldn't be too hard to do.

echo sprintf( 'string %s', 'replacement') . 'something else'; // This is fine.

For a list of functions which have a $echo = true toggle as one of the parameters, Google Search will probably work better than the wp.org search.

Oh... and if you like the idea, more examples of code patterns to listen for would be welcome!

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.

Research direction

Review the proposed echo-related patterns and the examples in the issue, including cases with and without concatenation. Define which patterns and additional examples should be supported before identifying the implementation files and tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.