WordPress / WordPress/WordPress-Coding-Standards

Some regex best practices

Open
#632 1 comment 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

With the regex functions, there are some best practices we could advise on /often made "mistakes" which we could warn people about.

I'm thinking along the lines of:

  • Use of / as delimiter -> advise to use other delimiter
  • Use of variables in the regex -> use preg_quote()
  • Use of g modifier -> Not used in PHP use preg_match_all() function instead
  • Use of s and m modifier in one regex -> they don't go together, review regex

Additionally the sniff could potentially check for:

  • non-PCRE syntaxes used in the regex
  • modifiers which are not implemented/available in PHP

This sniff would go into the extra ruleset.

Opinions ?

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

Start by reviewing the extra ruleset, which the issue identifies as the intended home for the sniff. The scope is not decided: clarify which regex practices and syntax or modifier checks are required, then define how completion will be verified; no file or test is named.

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.