Skip to content

Zend: suggest similar names for undefined function/class/method error - #22589

Draft
jorgsowa wants to merge 6 commits into
php:masterfrom
jorgsowa:feat/levenshtein-suggestions
Draft

Zend: suggest similar names for undefined function/class/method error#22589
jorgsowa wants to merge 6 commits into
php:masterfrom
jorgsowa:feat/levenshtein-suggestions

Conversation

@jorgsowa

@jorgsowa jorgsowa commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

An idea that I came up with during the implementation of case-sensitive PHP. Small DX improvement over the typos and mistakes in function/classes names.

Because this covers the case of an undefined function, it shouldn't decrease the performance of a properly working application.

Adaptive threshold: names under 8 characters must be within edit distance 1 to be suggested; names 8+ characters allow distance 2. This avoids noisy suggestions for short identifiers (a 1-character edit on a 4-letter name is a big relative change) while still catching multi-typo mistakes in longer names.

We can extend it to constants, etc., if the idea is good enough.

@TimWolla TimWolla left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Some feedback regarding the implementation.

Comment thread Zend/zend_execute.c Outdated
Comment thread Zend/zend_execute_API.c Outdated
Comment thread Zend/zend_execute.c Outdated
Comment thread Zend/zend_execute.c Outdated
Comment thread Zend/zend_execute.c Outdated
Comment thread Zend/zend_string.c Outdated
The JIT compiler had its own hand-written stub for raising "Call to
undefined function" errors that bypassed the new Levenshtein
suggestion logic, since it duplicated the error message instead of
reusing the interpreter's helper. Extract the shared logic into
zend_undefined_function_error() and call it from both the VM helper
and the JIT stub.
@jorgsowa
jorgsowa force-pushed the feat/levenshtein-suggestions branch from ad14e87 to 3c111b3 Compare August 3, 2026 21:50
@jorgsowa jorgsowa changed the title Zend: suggest similar names for undefined function/method/class errors Zend: suggest similar names for undefined function error Aug 3, 2026
@jorgsowa jorgsowa changed the title Zend: suggest similar names for undefined function error Zend: suggest similar names for undefined function/class/method error Aug 3, 2026
… constant expressions

zend_ast_evaluate_inner() (first-class callables used as default
parameter values, const initializers, etc.) still threw the plain
"Call to undefined function %s()" message, unlike the VM/JIT and
dynamic-call paths. Reuse zend_find_similar_function() there too.
"(did you mean strlen()?)" reads oddly since the () is already part of
the call syntax right before it. Use "(did you mean strlen?)" instead,
matching the class suggestion format which never had parens.
try {
test();
} catch (Error $e) {
echo $e->getMessage(), "\n";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo $e->getMessage(), "\n";
echo $e::class, ': ', $e->getMessage(), "\n";

Comment thread Zend/tests/levenshtein_suggest_class.phpt Outdated
Comment thread Zend/tests/levenshtein_suggest_function.phpt Outdated
Comment thread Zend/tests/levenshtein_suggest_method.phpt Outdated
Comment thread Zend/tests/levenshtein_suggest_class.phpt Outdated
Comment thread Zend/tests/levenshtein_suggest_function.phpt Outdated
Comment thread Zend/tests/levenshtein_suggest_method.phpt Outdated
@NickSdot

NickSdot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

I like this.

Added some nit comments. Context is the ongoing effort in #22799.

@jorgsowa
jorgsowa marked this pull request as draft August 5, 2026 21:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants