1.2.1035. Repeated Regex

Repeated regex should be centralized.

When a regex is repeatedly used in the code, it is getting harder to update. Regex that are repeated at least once (aka, used twice or more) are reported. Regex that are dynamically build are not reported.

<?php

// Regex used several times, at least twice.
preg_match('/^abc_|^square$/i', $_GET['x']);

//.......

preg_match('/^abc_|^square$/i', $row['name']);

// This regex is dynamically built, so it is not reported.
preg_match('/^circle|^'.$x.'$/i', $string);

// This regex is used once, so it is not reported.
preg_match('/^circle|^square$/i', $string);

?>

1.2.1035.1. Suggestions

  • Create a central library of regex

  • Use the regex inventory to spot other regex that are close, and should be identical.

1.2.1035.2. Specs

Short name

Structures/RepeatedRegex

Rulesets

All, Analyze, CE, CI-checks

Exakat since

0.10.9

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Features

regex

Examples

Vanilla, Tikiwiki

Available in

Entreprise Edition, Community Edition, Exakat Cloud