Skip to content

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

Draft
jorgsowa wants to merge 1 commit into
php:masterfrom
jorgsowa:feat/levenshtein-suggestions
Draft

jorgsowa wants to merge 1 commit 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
@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
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
@LamentXU123

LamentXU123 commented Aug 7, 2026 •

Copy link
Copy Markdown
Member

I am in favor of this. Just mentioning that this doesn't requires a RFC imho. Improving error message is not a BC break. We only need to send emails to internals to make sure not body rejects, which you already done.

However, since we are very close to the soft feature freeze. @php/release-managers-86 Do you think this qualifies as a post- soft-feature-freeze feature?

@jorgsowa
jorgsowa force-pushed the feat/levenshtein-suggestions branch from 160adb0 to d8633b9 Compare September 14, 2026 18:39
When a function, method, class, interface or trait cannot be found,
append a "(did you mean X?)" hint naming the closest existing match by
Levenshtein distance:

  Call to undefined function strlenn() (did you mean strlen?)
  Call to undefined method Calculator::addd() (did you mean add?)
  Class "ArrayObjekt" not found (did you mean ArrayObject?)

Names shorter than 3 characters are not matched. The allowed distance
is 1, or 2 for names of 8 or more characters. Matching is
case-insensitive and the suggestion uses the declared casing.

Function suggestions cover direct and dynamic calls, constant
expressions and the opcache JIT. For unqualified calls in a namespace,
names in the current namespace are tried first, falling back to global
names, mirroring function lookup.
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.

4 participants