ilario-pierbattista/reverse-regex
Generate example strings from regular expressions in PHP—useful for test data for forms, databases, and regex validation. Includes lexer/parser and random generators, supports literals, groups, classes, ranges, and quantifiers (with some Unicode/PCRE limits).
RegexDataGenerator facade or service that extends Laravel’s Factory class, enabling syntax like:
$user = User::factory()->for($company)->create([
'phone' => RegexDataGenerator::generate('04\d{8}'),
]);
Lexer, Parser, and Generator classes are loosely coupled, allowing for custom randomness strategies (e.g., seeded generation for deterministic tests) or extended regex support via middleware.\X{####} Unicode support), critical for internationalized apps or domain-specific validation (e.g., Australian postcodes).Lexer, Parser instantiation). Can be hidden behind a fluent interface (e.g., Regex::generate('pattern')->times(10)).AppServiceProvider, exposing a global helper:
// config/regex.php
'generators' => [
'phone' => '04\d{8}',
'postcode' => '\d{4}',
];
// Helper function
function regex_generate(string $pattern, int $seed = null): string {
$lexer = new \ReverseRegex\Lexer($pattern);
$parser = new \ReverseRegex\Parser($lexer, new \ReverseRegex\Generator\Scope(), new \ReverseRegex\Generator\Scope());
$gen = $seed ? new \ReverseRegex\Random\SeededRandom($seed) : new \ReverseRegex\Random\SimpleRandom();
return $parser->parse()->getResult()->generate('', $gen);
}
beforeEach or afterEach for automated test data generation:
beforeEach(function () {
$this->testData = [
'valid_phone' => regex_generate('04\d{8}'),
'invalid_phone' => regex_generate('[^04]\d{8}'),
];
});
User::factory()->state(function (array $attributes) {
return [
'phone' => regex_generate('04\d{8}'),
];
});
| Risk | Mitigation Strategy | Impact |
|---|---|---|
| PHP 8.1+ Requirement | Laravel 10+ and PHP 8.1+ are now standard; upgrade path is well-documented. | Low (if using LTS) |
| Unsupported Regex Features | Document unsupported features (e.g., \p{L}, lookarounds) and provide workarounds. |
Medium (edge cases) |
| Performance Overhead | Benchmark generation speed; cache compiled parsers for repeated patterns. | Low (microseconds) |
| PHPStan Strictness | Adopt phpstan-baseline incrementally; exclude non-critical files if needed. |
Medium (dev workflow) |
| Low GitHub Activity | Fork and contribute fixes; treat as a vendor dependency with minimal updates. | High (long-term) |
| Thread Safety | Stateless design makes it safe for parallel test execution (e.g., Pest’s --parallel). |
None |
| Unicode Edge Cases | Test with \X{####} patterns early; fall back to manual generation if needed. |
Medium (i18n apps) |
phpstan-baseline?"fzaninotto/Faker with extensions?"Faker in test data generation with regex-driven alternatives.DataProvider methods or setUp() for deterministic test data.Illuminate\Database\Eloquent\Factories\Factory with regex macros.symfony/polyfill-mbstring (already a dependency).Lexer (used internally).phpstan and cs-check workflows to enforce quality gates.post-autoload-dump hooks to validate regex patterns in test files.| Phase | Action Items | Dependencies |
|---|---|---|
| Assessment | Audit existing test data generation (Faker, custom scripts, hardcoded values). Identify regex patterns that could be automated. | QA Team, Dev Leads |
| Proof of Concept | Implement a minimal wrapper (e.g., RegexDataGenerator service) and test with 3–5 critical patterns (e.g., phone numbers, postcodes). Compare output with current methods. |
1–2 Devs, Test Suite |
| Integration | 1. Add to composer.json and update php.ini for PHP 8.1+. 2. Create a Laravel service provider to expose helpers. 3. Replace Faker calls in factories/tests with regex equivalents. |
Laravel, Composer, CI Pipeline |
| Quality Gates | Run composer phpstan and composer cs-check; address issues incrementally. Add phpstan-baseline to .gitignore if needed. |
PHPStan, Facile.it Coding Standard |
| Testing | 1. Validate generated data against existing test cases. 2. Add edge-case tests (e.g., max-length strings, Unicode). 3. Benchmark performance in CI. | PestPHP/PHPUnit, Load Testing Tools |
| Documentation | Update internal wiki with: - Supported regex patterns. - Migration guide from Faker/custom scripts. - Troubleshooting (e.g., unsupported features). | Tech Writers, Dev Leads |
| Rollout | 1. Feature flag regex generation in factories (optional). 2. Deprecate old test data scripts. 3. Train team on new syntax (e.g., regex_generate()). |
Release Manager, DevOps |
| Component | Compatibility | Workarounds |
|---|---|---|
How can I help you explore Laravel packages today?