Skip to content

Refactor tests for enumify: enhance Order model setup and improve path handling#12

Merged
iqbalhasandev merged 2 commits intomainfrom
refactor/enum-cast-references
Jan 25, 2026
Merged

Refactor tests for enumify: enhance Order model setup and improve path handling#12
iqbalhasandev merged 2 commits intomainfrom
refactor/enum-cast-references

Conversation

@iqbalhasandev
Copy link
Member

  • 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.
    This pull request refactors the enumify:refactor command 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 --strict option. 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:

  • The enumify:refactor command now only scans and refactors columns with enum casts defined in models, and the documentation in README.md has been updated to clarify this behavior. The --strict option has been removed from both the documentation and the command definition. [1] [2] [3] [4]

Configuration updates:

  • The config/enumify.php file now includes a models path configuration, allowing the command to locate model files for enum cast detection.

Core command logic and model cast detection:

  • Added logic to load model casts from configured model paths, extract enum casts from model files, and resolve enum class names. The command uses this information to determine which columns are eligible for refactoring. [1] [2] [3]

Refactoring and issue detection:

  • The refactoring logic now checks for enum casts before suggesting or applying changes, and issues are only recorded for columns with enum casts. The issue structure now includes a hasCast flag, and suggestion generation adapts based on cast status. [1] [2] [3] [4]

Reporting and output improvements:

  • Scan results and suggestions now indicate whether a column has an enum cast, providing clearer feedback to users during detailed output.

iqbalhasandev and others added 2 commits January 25, 2026 13:21
…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.
Copilot AI review requested due to automatic review settings January 25, 2026 07:22
@iqbalhasandev iqbalhasandev added the enhancement New feature or request label Jan 25, 2026
@iqbalhasandev iqbalhasandev merged commit 4fc73a8 into main Jan 25, 2026
24 checks passed
@iqbalhasandev iqbalhasandev deleted the refactor/enum-cast-references branch January 25, 2026 07:23
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 --strict and 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.

Comment on lines 1063 to +1067

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;
}
Copy link

Copilot AI Jan 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines +568 to +577
// 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];
}
}

Copy link

Copilot AI Jan 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
// 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

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant