Refactor tests for enumify: enhance Order model setup and improve path handling#12
Conversation
…h handling - Added a temporary Order model with enum cast for testing. - Created a dedicated Models directory for better organization. - Updated file path handling to use consistent concatenation style. - Improved test cases for enum refactoring, including handling of edge cases and backup functionality. - Ensured proper cleanup of temporary files after tests. - Fixed missing newline at the end of MixedCaseStatus.php file.
There was a problem hiding this comment.
Pull request overview
Refactors enumify:refactor to only refactor hardcoded enum values when the target column is cast to an enum in a Laravel model, and updates tests/docs/config accordingly.
Changes:
- Added model-cast discovery (
enumify.paths.models) and gated refactoring on detected enum casts. - Updated refactor suggestions/output and improved context extraction for reporting.
- Refactored tests/docs to remove
--strictand to set up a temporary model for cast-based detection.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| tests/Feature/RefactorCommandTest.php | Adds a temp Order model with enum casts and updates test config to provide model paths; removes --strict test. |
| src/Commands/RefactorCommand.php | Implements model cast loading/parsing and changes scanning/refactor eligibility to require enum casts. |
| config/enumify.php | Adds paths.models configuration used for model scanning. |
| README.md | Documents cast-only refactor behavior and removes --strict option. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| foreach ($enumData['cases'] as $caseName => $caseValue) { | ||
| if ($caseValue === $value) { | ||
| if ($isStrict) { | ||
| $enumLower = mb_strtolower($enumData['name']); | ||
| $columnLower = mb_strtolower($column); | ||
| if (! str_contains($enumLower, $columnLower) && ! str_contains($columnLower, $enumLower)) { | ||
| continue; | ||
| } | ||
| } | ||
| // Only refactor if column has an enum cast - no cast means no refactoring | ||
| if (! $castInfo['hasCast'] || ! $castInfo['enumClass']) { | ||
| return; | ||
| } |
There was a problem hiding this comment.
checkAndAddIssue() now returns early unless the matched column has an enum cast in a model. This makes the existing validation scanning unreachable: scanValidationRules() calls checkAndAddIssue() with column 'validation', which will never have a model cast, so Rule::in([...]) findings are silently dropped. Consider handling type === 'validation' separately (skip the cast gate for this type) or removing the validation pattern entirely if it’s no longer supported under the new cast-only behavior.
| // Fall back to any model with this column cast | ||
| foreach ($this->modelCasts as $modelClass => $casts) { | ||
| if (isset($casts[$column])) { | ||
| $enumClass = $casts[$column]; | ||
| $enumName = isset($this->enums[$enumClass]) ? $this->enums[$enumClass]['name'] : class_basename($enumClass); | ||
|
|
||
| return ['hasCast' => true, 'enumClass' => $enumClass, 'enumName' => $enumName]; | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
getColumnEnumCast() falls back to “any model with this column cast” when it can’t infer a model from context. In real apps with multiple models sharing a column name (e.g., many status casts pointing to different enums), this can pick the wrong enum and apply an incorrect refactor. Safer approach: require a model match and skip when the model can’t be determined, or only fall back when exactly one unique enum class exists for that column across all models; otherwise treat it as ambiguous and skip/report.
| // Fall back to any model with this column cast | |
| foreach ($this->modelCasts as $modelClass => $casts) { | |
| if (isset($casts[$column])) { | |
| $enumClass = $casts[$column]; | |
| $enumName = isset($this->enums[$enumClass]) ? $this->enums[$enumClass]['name'] : class_basename($enumClass); | |
| return ['hasCast' => true, 'enumClass' => $enumClass, 'enumName' => $enumName]; | |
| } | |
| } | |
| // Fall back only when there is exactly one unique enum class for this column across all models | |
| $enumClasses = []; | |
| foreach ($this->modelCasts as $modelClass => $casts) { | |
| if (isset($casts[$column])) { | |
| $enumClass = $casts[$column]; | |
| $enumClasses[$enumClass] = true; | |
| } | |
| } | |
| if (count($enumClasses) === 1) { | |
| $enumClass = array_key_first($enumClasses); | |
| $enumName = isset($this->enums[$enumClass]) ? $this->enums[$enumClass]['name'] : class_basename($enumClass); | |
| return ['hasCast' => true, 'enumClass' => $enumClass, 'enumName' => $enumName]; | |
| } | |
| // Ambiguous (multiple enum classes) or no enum cast found for this column |
This pull request refactors the
enumify:refactorcommand to only process columns that have enum casts defined within Laravel models, ensuring safer and more accurate refactoring of hardcoded enum values. It introduces logic to detect enum casts in models, updates documentation to clarify this behavior, and removes the previous--strictoption. The changes also enhance suggestions and reporting to reflect whether a column is eligible for refactoring based on its cast status.Command behavior and documentation changes:
enumify:refactorcommand now only scans and refactors columns with enum casts defined in models, and the documentation inREADME.mdhas been updated to clarify this behavior. The--strictoption has been removed from both the documentation and the command definition. [1] [2] [3] [4]Configuration updates:
config/enumify.phpfile now includes amodelspath configuration, allowing the command to locate model files for enum cast detection.Core command logic and model cast detection:
Refactoring and issue detection:
hasCastflag, and suggestion generation adapts based on cast status. [1] [2] [3] [4]Reporting and output improvements: