-
-
Notifications
You must be signed in to change notification settings - Fork 312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[TwigComponent] Improve exception message when component not found #1031
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -217,6 +217,32 @@ private function isAnonymousComponent(string $name): bool | |
*/ | ||
private function throwUnknownComponentException(string $name): void | ||
{ | ||
throw new \InvalidArgumentException(sprintf('Unknown component "%s". The registered components are: %s. And no matching anonymous component template was found', $name, implode(', ', array_keys($this->config)))); | ||
$message = sprintf('Unknown component "%s".', $name); | ||
$lowerName = strtolower($name); | ||
$nameLength = \strlen($lowerName); | ||
$alternatives = []; | ||
|
||
foreach (array_keys($this->config) as $type) { | ||
$lowerType = strtolower($type); | ||
$lev = levenshtein($lowerName, $lowerType); | ||
|
||
if ($lev <= $nameLength / 3 || str_contains($lowerType, $lowerName)) { | ||
$alternatives[] = $type; | ||
} | ||
} | ||
|
||
if ($alternatives) { | ||
if (1 === \count($alternatives)) { | ||
$message .= ' Did you mean this: "'; | ||
} else { | ||
$message .= ' Did you mean one of these: "'; | ||
} | ||
|
||
$message .= implode('", "', $alternatives).'"?'; | ||
} else { | ||
$message .= ' And no matching anonymous component template was found.'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! Just tried it out, it's super useful! Do you think we can apply the Levenshtein to look also for AnonymousComponent? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔 probably all available twig templates then should be iterated :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I'm not using anonymous components, then I'll leave it for you as that was your feature :) |
||
} | ||
|
||
throw new \InvalidArgumentException($message); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about low cost check first instead of doing both?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you going to get this exception all the time? 🙂