fix: refine theme path matching and enhance theme suggestion logic#179
Merged
fix: refine theme path matching and enhance theme suggestion logic#179
Conversation
Co-authored-by: Copilot <copilot@github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR tightens Magento theme resolution and improves CLI theme suggestions by avoiding partial theme-code matches and making suggestion matching more robust and case-insensitive.
Changes:
- Refined
ThemePath::getPath()to only match exactfrontend/<themeCode>oradminhtml/<themeCode>registrations (no substring matches). - Improved
ThemeSuggester::findSimilarThemes()to early-return on empty input and compare theme codes case-insensitively for both Levenshtein and substring matching. - Removed redundant constructor docblock noise in
ThemeSuggester.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/Service/ThemeSuggester.php | Makes theme suggestions safer (empty input) and more user-friendly (case-insensitive matching). |
| src/Model/ThemePath.php | Prevents accidental partial matches by requiring exact theme registration key matches. |
Comments suppressed due to low confidence (1)
src/Service/ThemeSuggester.php:53
strtolower($invalidTheme)andstrtolower($themeCode)are recomputed multiple times per iteration (for bothlevenshteinandstr_contains). Consider precomputing the lowercased input once before the loop, and lowercasing$themeCodeonce per iteration, to reduce repeated work and keep the matching logic easier to read.
$themes = $this->themeList->getAllThemes();
$threshold = (int) (strlen($invalidTheme) / 3);
$suggestions = [];
foreach ($themes as $theme) {
$themeCode = $theme->getCode();
$distance = levenshtein(strtolower($invalidTheme), strtolower($themeCode));
// Accept if: distance ≤ 1/3 of input length OR substring match (case-insensitive)
if ($distance <= $threshold || str_contains(strtolower($themeCode), strtolower($invalidTheme))) {
$suggestions[$themeCode] = $distance;
Co-authored-by: Copilot <copilot@github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request improves the accuracy and robustness of theme code matching and theme suggestions. The main changes include stricter matching logic in
ThemePath, better handling of empty input, and more consistent, case-insensitive comparison in the theme suggestion logic.Theme path resolution improvements:
getPathmethod inThemePath.phpnow only returns a path if the theme code matches exactly with either'frontend/' . $themeCodeor'adminhtml/' . $themeCode, instead of using a substring match. This prevents accidental partial matches and ensures more precise theme resolution.Theme suggestion robustness and accuracy:
findSimilarThemesmethod inThemeSuggester.phpnow immediately returns an empty array if the input theme name is empty, preventing unnecessary processing and possible errors.findSimilarThemesare now performed in a case-insensitive manner for both Levenshtein distance and substring checks, improving the quality and user-friendliness of suggestions.Code cleanup:
ThemeSuggester.phpfor improved code clarity.