Skip to content

Commit 1f1abdc

Browse files
committed
feat: add enumify:refactor command for scanning and detection
Key capabilities: - **Scan & Detect**: Finds hardcoded enum values in `where` clauses, array assignments, comparisons, and validation rules. - **Auto-Fix**: Refactors hardcoded strings to proper Enum references with `--fix`. - **Key Normalization**: Converts enum keys to UPPERCASE (e.g. `case Active` -> `case ACTIVE`) and updates all references in the codebase via `--normalize-keys`. - **Interactive Mode**: Guided prompts for safe refactoring with `--interactive`. Changes: - Added `RefactorCommand` with support for scanning, fixing, and reporting (JSON/CSV/MD). - Added `refactor` configuration to \[config/enumify.php\](cci:7://file:///Users/iqbal/Sites/Open%20Source/laravel-enumify/config/enumify.php:0:0-0:0). - Registered command in `EnumifyServiceProvider`. - Added feature tests covering all modes. - Updated README with detailed usage documentation.
1 parent 616db69 commit 1f1abdc

5 files changed

Lines changed: 1501 additions & 34 deletions

File tree

README.md

Lines changed: 111 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,33 @@
66
[![NPM Version](https://img.shields.io/npm/v/@devwizard/vite-plugin-enumify.svg?style=flat-square)](https://www.npmjs.com/package/@devwizard/vite-plugin-enumify)
77
[![NPM Downloads](https://img.shields.io/npm/dt/@devwizard/vite-plugin-enumify.svg?style=flat-square)](https://www.npmjs.com/package/@devwizard/vite-plugin-enumify)
88

9-
**Auto-generate TypeScript enums and maps from Laravel PHP enums, with Vite integration.**
9+
**Auto-generate TypeScript enums from Laravel PHP enums. Refactor hardcoded values and normalize enum keys.**
1010

11-
Laravel Enumify keeps frontend TypeScript enums in sync with backend PHP enums automatically. It generates files before Vite compiles, so imports never fail and no runtime fetching is needed.
11+
Laravel Enumify keeps frontend TypeScript enums in sync with backend PHP enums automatically. It also scans your codebase for hardcoded enum values and can refactor them to use proper enum references. Includes tools for normalizing enum case names to UPPERCASE.
1212

1313
## Features
1414

15-
- 🔄 **Automatic Sync** – Runs during `npm run dev` and `npm run build` via the Vite plugin
16-
- 🧭 **Wayfinder-Level DX** – One install command to scaffold everything
17-
- 🏷️ **Labels Support**`label()` or static `labels()` become TS maps
18-
- 🎨 **Custom Methods** – Public zero-arg scalar methods become TS maps
19-
- 📦 **Barrel Exports** – Optional `index.ts` for clean imports
20-
-**Smart Caching** – Only regenerate changed files using hashes
21-
- 🔒 **Git-Friendly**`.gitkeep` and strict `.gitignore` patterns supported
15+
- 🔄 **Automatic Sync** – Runs during `npm run dev` and `npm run build` via the Vite plugin
16+
- 🧭 **Wayfinder-Level DX** – One install command to scaffold everything
17+
- 🏷️ **Labels Support**`label()` or static `labels()` become TS maps
18+
- 🎨 **Custom Methods** – Public zero-arg scalar methods become TS maps
19+
- 📦 **Barrel Exports** – Optional `index.ts` for clean imports
20+
-**Smart Caching** – Only regenerate changed files using hashes
21+
- 🔒 **Git-Friendly**`.gitkeep` and strict `.gitignore` patterns supported
22+
- 🔧 **Refactor Command** – Scan and fix hardcoded enum values in your codebase
23+
- 🔠 **Key Normalization** – Convert enum keys to UPPERCASE and update all references
2224

2325
## Requirements
2426

25-
- PHP 8.2+
26-
- Laravel 10, 11, or 12
27-
- Node.js 18+
28-
- Vite 4, 5, 6, or 7
27+
- PHP 8.2+
28+
- Laravel 10, 11, or 12
29+
- Node.js 18+
30+
- Vite 4, 5, 6, or 7
2931

3032
## Package Links
3133

32-
- [Composer](https://packagist.org/packages/devwizardhq/laravel-enumify)
33-
- [NPM](https://www.npmjs.com/package/@devwizard/vite-plugin-enumify)
34+
- [Composer](https://packagist.org/packages/devwizardhq/laravel-enumify)
35+
- [NPM](https://www.npmjs.com/package/@devwizard/vite-plugin-enumify)
3436

3537
## Installation
3638

@@ -48,10 +50,10 @@ php artisan enumify:install
4850

4951
This will:
5052

51-
- Create `resources/js/enums/`
52-
- Create `resources/js/enums/.gitkeep`
53-
- Print the `.gitignore` lines to add (and offer to append them)
54-
- Offer to publish the config file
53+
- Create `resources/js/enums/`
54+
- Create `resources/js/enums/.gitkeep`
55+
- Print the `.gitignore` lines to add (and offer to append them)
56+
- Offer to publish the config file
5557

5658
### 3) Configure Vite
5759

@@ -304,11 +306,11 @@ This ensures your enums are fully localized on the frontend while respecting Rea
304306

305307
Enumify will convert methods into TypeScript maps when they meet these rules:
306308

307-
- Public, non-static, zero-argument methods only
308-
- Return types must be `string`, `int`, `float`, `bool`, or nullable/union combinations of those
309-
- Methods without return types or unsupported return types are skipped
310-
- Map naming: `EnumName + MethodName` (pluralized for non-boolean methods)
311-
- Boolean methods also generate a helper function
309+
- Public, non-static, zero-argument methods only
310+
- Return types must be `string`, `int`, `float`, `bool`, or nullable/union combinations of those
311+
- Methods without return types or unsupported return types are skipped
312+
- Map naming: `EnumName + MethodName` (pluralized for non-boolean methods)
313+
- Boolean methods also generate a helper function
312314

313315
Labels are handled separately using `label()` or `labels()`.
314316

@@ -342,6 +344,81 @@ php artisan enumify:sync --format=json
342344
php artisan enumify:sync --quiet
343345
```
344346

347+
### enumify:refactor
348+
349+
Scan your codebase for hardcoded enum values and refactor them to use proper enum references. This command also supports normalizing enum case names to UPPERCASE and updating all references throughout your application.
350+
351+
#### Available Options
352+
353+
| Option | Short | Description |
354+
| ------------------ | ----- | ---------------------------------------------------------------- |
355+
| `--fix` | `-f` | Apply refactoring changes to files |
356+
| `--dry-run` | `-d` | Preview changes without modifying files |
357+
| `--enum=` | `-e` | Target a specific enum class by short name (e.g., `OrderStatus`) |
358+
| `--path=` | `-p` | Limit scan to a specific directory (e.g., `app/Models`) |
359+
| `--interactive` | `-i` | Run in interactive mode with guided prompts |
360+
| `--json` | `-j` | Output results in JSON format |
361+
| `--backup` | | Create backups before applying changes |
362+
| `--include=` | | File patterns to include (e.g., `*.php`) |
363+
| `--exclude=` | | Paths or patterns to exclude from scanning |
364+
| `--strict` | | Strict matching (column name must match enum context) |
365+
| `--report=` | | Export report to file (formats: `json`, `csv`, `md`) |
366+
| `--detailed` | | Show detailed output with code context |
367+
| `--normalize-keys` | | Convert enum keys to UPPERCASE and fix all references |
368+
369+
#### Scanning for Hardcoded Values
370+
371+
```bash
372+
# Scan and display hardcoded enum values
373+
php artisan enumify:refactor
374+
375+
# Preview what changes would be made
376+
php artisan enumify:refactor --dry-run
377+
378+
# Apply fixes with backup
379+
php artisan enumify:refactor --fix --backup
380+
381+
# Target a specific enum
382+
php artisan enumify:refactor --enum=OrderStatus
383+
384+
# Limit scan to a directory
385+
php artisan enumify:refactor --path=app/Services
386+
387+
# Export a markdown report
388+
php artisan enumify:refactor --report=refactor-report.md
389+
```
390+
391+
#### Key Normalization (UPPERCASE)
392+
393+
The `--normalize-keys` flag converts enum case names from any case format to UPPERCASE and updates all references in your codebase:
394+
395+
- `case Active``case ACTIVE`
396+
- `case pending``case PENDING`
397+
- `case InProgress``case IN_PROGRESS`
398+
399+
```bash
400+
# Preview key normalization changes
401+
php artisan enumify:refactor --normalize-keys --dry-run
402+
403+
# Apply key normalization with backup
404+
php artisan enumify:refactor --normalize-keys --fix --backup
405+
```
406+
407+
#### Interactive Mode
408+
409+
Run the command interactively with guided prompts:
410+
411+
```bash
412+
php artisan enumify:refactor --interactive
413+
```
414+
415+
This mode allows you to:
416+
417+
- Choose between scan, preview, apply, or normalize modes
418+
- Select which enums to check
419+
- Specify the directory to scan
420+
- Confirm changes before applying
421+
345422
## Configuration
346423

347424
Publish the config file:
@@ -382,18 +459,18 @@ return [
382459

383460
## Generated Output
384461

385-
- `resources/js/enums/*.ts` – one file per enum
386-
- `resources/js/enums/index.ts` – barrel exports (optional)
387-
- `resources/js/enums/.enumify-manifest.json` – hashes, timestamps, versions
462+
- `resources/js/enums/*.ts` – one file per enum
463+
- `resources/js/enums/index.ts` – barrel exports (optional)
464+
- `resources/js/enums/.enumify-manifest.json` – hashes, timestamps, versions
388465

389466
The generator uses atomic writes and skips unchanged files for speed.
390467

391468
## Local Development (Monorepo)
392469

393470
This repo contains two packages:
394471

395-
- `packages/laravel-enumify` (Composer)
396-
- `packages/vite-plugin-enumify` (NPM)
472+
- `packages/laravel-enumify` (Composer)
473+
- `packages/vite-plugin-enumify` (NPM)
397474

398475
### Composer path repository
399476

@@ -434,14 +511,14 @@ Release tip: tag releases after merging to `main`, then publish to Packagist and
434511

435512
Suggested pipelines:
436513

437-
- PHP: `composer test` and `composer test-coverage`
438-
- Node: `pnpm run build && pnpm run typecheck`
514+
- PHP: `composer test` and `composer test-coverage`
515+
- Node: `pnpm run build && pnpm run typecheck`
439516

440517
## Troubleshooting
441518

442-
- **Missing enums folder**: run `php artisan enumify:install` or ensure `resources/js/enums/.gitkeep` exists.
443-
- **Imports fail during build**: ensure the Vite plugin is enabled and runs before `laravel()`.
444-
- **Enums not discovered**: check `config/enumify.php` paths and include/exclude filters.
519+
- **Missing enums folder**: run `php artisan enumify:install` or ensure `resources/js/enums/.gitkeep` exists.
520+
- **Imports fail during build**: ensure the Vite plugin is enabled and runs before `laravel()`.
521+
- **Enums not discovered**: check `config/enumify.php` paths and include/exclude filters.
445522

446523
## Changelog
447524

config/enumify.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,24 @@
108108
// Exclude enums matching these patterns
109109
'exclude' => [],
110110
],
111+
112+
/*
113+
* |--------------------------------------------------------------------------
114+
* | Refactor Command Configuration
115+
* |--------------------------------------------------------------------------
116+
* |
117+
* | Configuration for the enumify:refactor command.
118+
* |
119+
*/
120+
'refactor' => [
121+
// Default paths to exclude from scanning
122+
'exclude' => [
123+
'vendor',
124+
'node_modules',
125+
'storage',
126+
'.git',
127+
'bootstrap/cache',
128+
'public',
129+
],
130+
],
111131
];

0 commit comments

Comments
 (0)