Skip to content
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

Merged
merged 1 commit into from
Aug 15, 2023

Conversation

norkunas
Copy link
Contributor

@norkunas norkunas commented Aug 2, 2023

Q A
Bug fix? no
New feature? yes
Tickets Fix #...
License MIT

I have over 150 components and to me it's just useless to list all component names in exception message and to look for the right name in a very long list.

Used same code to get alternatives like in Symfony codebase.

Copy link
Member

@kbond kbond left a comment

Choose a reason for hiding this comment

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

Great idea!

@norkunas
Copy link
Contributor Author

norkunas commented Aug 2, 2023

Should we list all components in case it didn't find any alternatives? 🙂

@kbond
Copy link
Member

kbond commented Aug 2, 2023

I'm thinking no. I don't think the Symfony code base does that.

src/TwigComponent/src/ComponentFactory.php Outdated Show resolved Hide resolved
src/TwigComponent/src/ComponentFactory.php Outdated Show resolved Hide resolved
Copy link
Member

@weaverryan weaverryan left a comment

Choose a reason for hiding this comment

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

levenshtein! I like this - can you rebase?

@norkunas
Copy link
Contributor Author

norkunas commented Aug 8, 2023

Rebased

Comment on lines +227 to +231
$lev = levenshtein($lowerName, $lowerType);

if ($lev <= $nameLength / 3 || str_contains($lowerType, $lowerName)) {
$alternatives[] = $type;
}
Copy link
Contributor

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?

Suggested change
$lev = levenshtein($lowerName, $lowerType);
if ($lev <= $nameLength / 3 || str_contains($lowerType, $lowerName)) {
$alternatives[] = $type;
}
if (str_contains($lowerType, $lowerName)) {
$alternatives[] = $type;
} elseif (levenshtein($lowerName, $lowerType) <= $nameLength / 3) {
$alternatives[] = $type;
}

Copy link
Contributor Author

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? 🙂


$message .= implode('", "', $alternatives).'"?';
} else {
$message .= ' And no matching anonymous component template was found.';
Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🤔 probably all available twig templates then should be iterated :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 :)

@weaverryan
Copy link
Member

This is great - thanks Tomas!

@weaverryan weaverryan merged commit 3237b1b into symfony:2.x Aug 15, 2023
34 of 38 checks passed
@norkunas norkunas deleted the exception-mkay branch August 15, 2023 11:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants